Variables which are created inside a function are called local variables. Variables which are created outside a function are called global variables. If both local and global variables have same name, then inside the function local variable will be executed. In the following program game is a global variable since it is defined outside function. On execution of the program the global variable is called inside the function.
game = "football"
def function_name():
print("They are playing " + game)
function_name()
Output :
They are playing football
In the following program variable game is both inside and outside the function. On execution of program, only local variable is executed inside the function. However outside of the function global variable is executed.
game = "football"
def function_name():
game = "Cricket"
print("They are playing " + game)
function_name()
print("They are playing " + game)
Output :
They are playing Cricket They are playing football
Keyword global
Variables with the keyword global are used in global scope. If global keyword is put to the variable name inside a function, that variable is also used on global scope. In the following program keyword global is put in the variable name game which is inside a function. On execution of the program it is used as a global variable.