Explanation of global and local variables in a simple way
Scope means –to which extent a code or data would be known or accessed.
There are three types of variables with the view of scope.
Global Scope- Name declared in the main program or outside the function . It is usable inside the whole program.
Local Scope- Name declared in function body. It is usable within the function.
There are three types of variables with the view of scope.
Local variable – accessible only inside the functional block where it is declared.
Global variable – variable which is accessible among whole program using global keyword.
Non local variable – accessible in nesting of functions,using nonlocal keyword.
def fun1():
x = 100
def fun2():
nonlocal x
x=20
print(x)
fun2()
print(x)