Function Scope

Learn about variable scope, global variables, and what happens when we pass mutable and immutable variables as function parameters.

The scope of a function means the extent to which the variables and other data items made inside the function are accessible in code. In Python, the function scope is the function’s body. Whenever a function runs, the program moves into its scope. Once the function has ended, it moves back to the outer scope.

Data lifecycle

In Python, data created inside the function cannot be used from the outside unless it is being returned from the function. Variables in a function are isolated from the rest of the program. When the function ends, they are released from memory and cannot be recovered.

The following code will never work:

Get hands-on with 1400+ tech skills courses.