Local-Global variables
The global keyword is used to create global variables from a no-global scope, e.g. inside a function and local variables can be created by just declaring them
x = "global"
def foo():
print("x inside:", x)
foo()
print("x outside:", x)
# Output
x inside: global
x outside: global
Non local variables
The nonlocal keyword is used to work with variables inside nested functions, where the variable should not belong to the inner function.Use the keyword nonlocal to declare that the variable is not local.
def myfunc1():
x = "John"
def myfunc2():
nonlocal x
x = "hello"
myfunc2()
return x
print(myfunc1())
# Output
hello
