In this tutorial, we will learn about global and local variables and their scope in Python with the help of examples. Depending upon their scope, there are two types of variables in Python that are as follows:
- Global variable
- Local variable
Let’s understand one by one with the help of example programs.
Global Variable in Python
A global variable is a variable that is defined in the main part of the code but outside any function. In other words, a variable defined outside all functions in a program is called global variable in Python. It will be visible everywhere in the entire Python code. That is, we can access a global variable throughout the Python program.
We can use them anywhere, even inside a function or outside the function or also inside any file that imports that file.
Example 1: Creating and Accessing Global Variable
Let’s create a Python program in which we will create a global variable in the main code and access it inside the function and outside the function.
x = 20 # a global variable.
# Creating a user-defined function named my_function.
def my_function():
# Accessing the global variable inside the function.
print("Inside function: ", x)
# Calling the function.
my_function()
# Accessing the global variable from outside the function.
print("Outside function: ", x)
Output:
Inside function: 20 Outside function: 20
In the above code, we have created a global variable x in the main code area and assigned it the value 20. After that, we have defined a function named mu_function(). Inside the function, we have accessed the global variable to print the value of x.
Outside the function, we again accessed the global variable x and called the function. You can see that a global variable can be accessed successfully inside a function and outside the function.
Example 2: Creating a Local Variable with the Same Name as a Global Variable
If we create a variable with the same name as the name of a global variable inside a function, the Python interpreter will treat this variable as local. In this case, we can only access it inside the function, not outside the function. However, the global variable will remain with the same name and the original value as it is.
Let’s write a program where we will create a variable inside a function with the same name as the global variable.
x = 20 # a global variable.
def my_function():
# Creating a variable with the same name as the name of global variable.
x = 20 * 30 # Here, interpreter will treat as a local variable.
print("Inside function: ", x)
# Calling the function.
my_function()
# Accessing the global variable from outside the function.
print("Outside function: ", x)
Output:
Inside function: 600 Outside function: 20
Here, Python creates a new local variable with the same name as a global variable name inside the function. That’s why when we accessed it from inside and outside the function, we gained two different values, as you can see in the output.
Global Keyword in Python
Python provides a global keyword to create a global variable inside a function. If we declare a variable with a global keyword inside the function, the variable belongs to the global scope. We can also use the global keyword if we want to change the value of a global variable inside the function.
To declare a global variable, we need to write a global keyword before the variable name inside the function. Let’s take an example of it.
Example 3: Use of Global Keyword Inside Function
name = 'John' # a global variable.
def my_function1():
global name
name = 'Bob' # Here, changing the value of global variable inside the function.
print("Inside function: ", name)
def my_function2():
print("Inside another function: ", name)
# Calling the functions.
my_function1()
my_function2()
# Accessing the global variable from outside the functions.
print("Outside function: ", name)
Output:
Inside function: Bob Inside another function: Bob Outside function: Bob
As you can see in the output, the preceding code printed the changed value of the global variable name, which is defined inside the function and accessed inside functions and also outside functions.
Example 4: Modify a Global Variable Using the global Keyword
city = 'New York' # a global variable.
def my_function1():
global city
city = 'Dhanbad'
print("Inside my_function1: ", city)
def my_function2():
global city
city = 'Sydney'
print("Inside my_function2: ",city)
# Calling the function.
my_function1()
my_function2()
print("Outside function: ", city)
Output:
Inside my_function1: Dhanbad Inside my_function2: Sydney Outside function: Sydney
In this example:
- city = ‘New York’ creates a global variable named city with a value ‘New York’. The global variable can be accessed from anywhere in the program.
- The my_function1() declares city as a global variable using the global keyword.
- The statement city = ‘Dhanbad’ changes the value of the global variable from “New York” to “Dhanbad”. Therefore, the print(“Inside my_function1:”, city) statement displays the updated value Dhanbad.
- The function my_function2() also declares city as global.
- The statement city = ‘Sydney’ changes the same global variable from “Dhanbad” to “Sydney”. Therefore, the print(“Inside my_function2:”, city) statement displays the new value Sydney.
- After both functions finish execution, the global variable city still contains the last assigned value, which is “Sydney”.
- The print(“Outside function:”, city) statement prints Sydney, showing that the global variable has been permanently modified.
- Since both functions use the global keyword, they modify the same global variable instead of creating separate local variables.
Important Note
- The global keyword allows a function to modify a global variable.
- Without the global keyword, assigning a value to city inside a function would create a local variable instead of modifying the global one.
- Changes made to a global variable using the global keyword remain effective throughout the program after the function call.
Local Variable in Python
A local variable is a variable that is defined inside the body of the function or in the local scope. It is accessible only within the function where it is defined and exists in the memory as long as the function is executing.
In other words, local variables inside a function have a local scope and are destroyed from the memory as the execution of the function completes.
An example of creating a local variable inside the function is as follows:
def showMe():
localVariable = "I am a local variable";
In this example, we have defined a variable, localVariable, having the value “I am a local variable”. It is a local variable, as it is contained within a specific function. When the execution of a function completes, the Python interpreter destroys the local variables from the memory.
Moreover, the variables declared in the parameter list of the function definition are always local variables. They contain values we pass into the function when we call it.
Example 5: Attempt to Access Local Variable from Outside Function
Let’s create a Python program where we will try to access the local variable from outside the function.
def my_func():
city = 'Dhanbad'
print(city)
# Calling the function.
my_func()
print(city)
Output:
File "C:\Python Project\EscapeSequence.py", line 6, in print(city) NameError: name 'city' is not defined Dhanbad
The output shows an error as we are trying to access a local variable y from outside the function, i.e., in a global scope. This is because the local variable is invisible outside the function. But the local variable is visible inside the function or local scope and prints the value of local variable as output on the console.
Example 6: Global and Local Variables in Python
Let’s write a Python program in which we will declare both local and global variables, access them, and display their messages.
# Declare a global variable and assign it a value.
message1 = "This is a global variable, and we can access it anywhere in the program."
# Create a function named display.
def display():
# Declare a local variable and assign it a value.
message2 = "This is a local variable, and we can access it only inside the function body."
print(message2) # Accessing local variable from inside the function.
print(message1); # Accessing the global variable.
display() # Calling function.
Output:
This is a global variable, and we can access it anywhere in the program. This is a local variable, and we can access it only inside the function body.
Global and Local Variables with Same Name in Python
If we define a local variable or functional parameter inside the body of a function with the same name as the global variable, a local variable takes precedence over a global variable. In this case, the local variable hides the global variable.
Example 7: Local Variable Hides the Global Variable
Let’s write a Python program where we will declare two global variables and two local variables within a function. One of the global and local variables will share the common name student2.
# Declare global variables.
student1 = "John"
student2 = "Larry"
# Create a function named showMe.
def showMe():
student2 = "Harry" # Here, local variable is sharing the same name as global variable.
student3 = "Deep"
print(student2, " ",student3)
showMe(); # calling function.
print(student1, " ", student2)
Output:
Harry Deep John Larry
In the above code, when we have called the variable student2 from the function, the value of local variable has been displayed. But when we called student2 from the global scope, the value of global variable was displayed on the console.
Thus, having two variables with the same name is always confusing. Therefore, it is the best practice to use unique names for all variables to avoid confusion.
Difference between Local and Global Variables in Python
There are the following differences between local and global variables in Python. They are as follows:
1. Definition:
- A variable that is declared inside a function is a local variable.
- A variable that is declared outside the function is a global variable.
2. Accessibility:
- Accessibility of a local variable is only within the function in which it has been defined.
- Accessibility of a global variable is throughout the program. All functions defined in the program can easily access the global variable.
3. Existence:
- The local variable is created when the function (in which it is defined) begins executing, and it is destroyed when the execution of the function is complete.
- The existence of a gobal variable remains throughout the entire program execution.
4. Reliable:
- Programmers prefer to define local variables because they are reliable. Values cannot be changed from outside the function.
- Programmers rarely prefer, as any function can change the values of global variables.
In this tutorial, you have learned about global and local variables in Python with different example programs. I hope that you will have understood the basic points of global and local variables and practiced all programs.



