Global variables are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function.
1) Local Variables
Local variables are those which are initialized inside a function and belong only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable. Consider the following code:
def functLocalVar(): y = "local" functLocalVar() print(y) # This code will generate an Error : NameError: name 'y' is not defined # As Local variable cannot be accessed outside the function boundary
2) Global Variables
In Python, a variable declared outside of the function or in global scope is known as a global variable. This means that a global variable can be accessed inside or outside of the function. Consider the following code:
def functGlobalVar(): print("x inside:", x) x = "global" functGlobalVar() print("x outside:", x) Output : x inside: global x outside: global
In the above code, we created x as a global variable and defined a functGlobalVar() to print the global variable x. Finally, we call the functGlobalVar() which will print the value of x. The variable x is defined as the global variable and can be used both inside the function as well as outside the function.
3) Same Name Local and Global Variable
There can be a scenario when a variable with the same name initialized inside a function as well as globally. Now the question arises, will the local variable will have some effect on the global variable or vice versa, and what will happen if we change the value of a variable inside of the function ? Let us study the following example:
# Global scope s = "I love Python" def funct(): s = "Me too." print(s) funct() print(s) # Out of the code will be Me too. I love Python
In the above example the variable s is first defined outside the function and assigned a value. After that a function funct() is defined that also contains a variable s in it. The variable s defined outside function is global variable and variable s defined inside the function is local variable. Inside the function if a variable with same name as global variable is defined then that local variable will be accessible in that function. Outside the scope of that function the global variable s will be accessible as shown in the output of the above mentioned code. Now consider this code:
# Global scope s = "I love Geeksforgeeks" def funct(): print(s) s = "Me too." funct() print(s) # This will generate the following error UnboundLocalError: local variable 's' referenced before assignment
As we have defined a local variable s in the function so global variable will not be accessible . Also we have not given a value to s , so Python will generate error.
4) Global Keyword
We can use the global keyword in a function if we want to do assignments or change the global variable. global is not needed for printing and accessing. Any variable which is changed or created inside of a function is local if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword “global”, as can be seen in the following example:
x = "global " def funct(): global x y = "local" x = x * 2 print(x) print(y) funct() # The output of above code will be: global global local
5) Nonlocal Variables
Nonlocal variables are used in nested functions whose local scope is not defined. This means that the variable can be neither in the local nor the global scope. Let’s see an example of how a nonlocal variable can be used in Python.
def outer(): x = "local" def inner(): nonlocal x x = "nonlocal" print("inner:", x) inner() print("outer:", x) outer() # The output of the code will be inner: nonlocal outer: nonlocal
Note : The variable x is declared as non local in the inner() function, so it is neither global nor local. But this variable will not be accessible outside the nested function. If we change the value of a nonlocal variable, the changes appear in the local variable.