Python Local and Global Variables

Python Local and Global Variables

Welcome to a new chapter on our “Learn Python with me” tutorial series. In the previous chapters, we discussed Python functions in detail. We will continue the same discussion in this chapter as well by discussing Python Local and Global Variables.


Find link to all the Python Chapters of our tutorial series here: Learn Python


Python Local and Global Variables

Local Variable:

When you declare a variable inside a function, it becomes a local variable. And the scope of this local variable will be limited to only within the function in which it exists. In other words, it means that you cannot access this variable outside this function.

#python program to display local variable in function
def samplefunction():
    p=1 #this is local variable
    p=p+1
    print(p) #displays output
print("Let us call the sample function now")
samplefunction()
print("Let us try accessing local variable p now")
print(p) #this print statement will create error
#because p has a scope of within the samplefunction()

Output

Let us call the sample function now
2
Let us try accessing local variable p now
Traceback (most recent call last):
  File "/home/aatmann/Python_VisualStudio/test.py", line 9, in <module>
    print(p) #this print statement will create error
NameError: name 'p' is not defined

In the program mentioned above, we declared a variable ‘p’ inside the samplefunction(), and its scope was within the limit of this function. That’s why there were no issues when we added the print(p) statement within the boundary of the samplefunction(), and 2 was therefore displayed in the output.

However, it resulted in errors when we tried accessing the variable ‘p’ outside the function. This program is the best example to illustrate the concept of Python Local Variables.

Now let us discuss Python Global Variables.

Global Variables:

Just as the name sounds, when you declare a variable in a function as global, it will be available within the function and outside the function as well. And the Global Variables in Python are declared using the global keyword. For example, let us consider the example program provided below.

#python program to display the global variables inside a function
def check_function():
    global p #this is global variable
    p=1
    print("Inside the function")
    print("Display the initial value: ",p)
    p=p+1
    print("Display the modified value: ",p)
check_function()
print("Outside the function")
print("Global variable: ",p)
"""You can see that it displays the most recent
value of 'p' even outside the function """

Output

Inside the function
Display the initial value:  1
Display the modified value:  2
Outside the function
Global variable:  2

In the program mentioned above, we declared a global variable ‘p’ inside the function. It went through some changes and displayed the modified value using the print() statement defined within the check_function().

However, accessing this global variable even outside this function displayed the most recently updated variable ‘p’ value. This way, you can use the variables inside and outside the function by adding a global variable.

globals() function:

If a global variable name declared inside any function is also used to name another variable outside the function, it might confuse the programmers. Python has globals(), a built-in function that will avoid confusion in situations like this. Let us look at an example program to understand more about the globals() function.

#python program to illustrate the concept of globals() function
#it is used when global and local variables have the same name
p=1 #this is a global variable declared outside the function
def function_1():
    p=2 #this is a local variable
    a=globals()['p'] #store the global variable p into x
    print("Inside the function")
    print("Local variable: ",p)
    print("Global variable: ",a)

#let us call the function now
function_1()
print("Outside the function")
print("Print Global variable: ",p)

Output

Inside the function
Local variable:  2
Global variable:  1
Outside the function
Print Global variable:  1

In the program mentioned above, you can notice that we referred to the global variable p using the globals()[‘p’]  in the code.

We hope you have understood the concept of Python Local and Global Variables.

Source

Scroll to Top