Global and local variables
A global variable can be reached anywhere in the code, a local only in its code block, where it was defined.

Scope of local and global variables
Local variables
def function(localVar1): # Definition of a local variable for the parameter in the function header
localVar2 = "I am a second local variable." # Definition of a 2nd local variable
print(localVar1 + "\n" + localVar2) # Works fine, because the local variable can be accessed within the function.
function("I am a local variable.") # Call of the function with passing a string as parameter
The following would not work here, because
localVar1 and localVar2 are local variables which cannot be used outside of the function, because outside they are not defined. Therefore, they cannot be accessed from outside of their scope.print(localVar1)
print(localVar2)
Global variables
Global variables are defined outside any function, respective code block and are therefore accessible everywhere in the source code:
globalVar = "I am a global variable." # Declaration and initialisation
# of global variable
def myFunction(): # Definition of a function
print(globalVar) # Output of the global variable
# End of myFunction()'s definition
myFunction() # call of myFunction()
print(globalVar) # Output of the global variable
If a variable is defined outside a function, it can be accessed globally (everywhere). Therefore the output of the above program is:
I am a global variable.
I am a global variable.
Supplement:
If an assignment of the global variable within the function is necessary, this global variable must be declared within the function with the keyword global:
globalVar = "I am a global variable." # Declaration and initialisation
# of global variable
def myFunction():
global globalVar # Declaration of global variable
globalVar = "New string" # Assignment to global variable
print(globalVar) # Output of the global variable
myFunction() # call of myFunction()
print(globalVar) # Output of the global variable
This works fine. The output will be:
New string
New string
Otherwise - without global (re)declaration - a local variable with the same name would be declared:
globalVar = "I am a global variable." # Declaration and initialisation
# of global variable
def myFunction():
global globalVar # Declaration of global variable
globalVar = "I'm in fact local." # Declaration and initialisation of local variable
print(globalVar) # Output of the global variable
myFunction() # call of myFunction()
print(globalVar) # Output of the global variable
The output would be:
I'm in fact local.
I am a global variable.
Why not just use global variables. There wouldn't be a need function parameters?
- In larger programs you can easily lose the overview because of thousands of global variables and possibly use variables that you already use for other things.
- Global variables need memory for the whole runtime of the program, even if they are no longer used. The memory of local variables is released after the function is finished.
Therefore normally global variables have to be avoided. With very runtime-critical systems (real-time systems), however, it can be useful to use global variables, because time-consuming copying of values when passing parameters is avoided.