Variables can only reach the area in which they are defined, which is called scope. Think of it as the area of code where variables can be used. Python supports global variables (usable in the entire program) and local variables.
By default, all variables declared in a function are local variables. To access a global variable inside a function, it’s required to explicitly define ‘global variable’.
Python Programming Bootcamp: Go from zero to hero
Below we’ll examine the use of local variables and scope. This will not work:
but this will:
Let’s examine this further:
Calling functions in functions
We can also get the contents of a variable from another function:
If a variable can be reached anywhere in the code is called a global variable. If a variable is known only inside the scope, we call it a local variable.
If you are new to Python programming, I highly recommend this book.