In this tutorial, we will learn about types of 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:
- 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 function or outside the function or also inside any file which imports that file.
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 with a 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.
If we create a variable with the same name as the name of global variable inside a function, 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 a value of 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 on it.
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 global variable name, which is defined inside the function, and accessed inside functions and also outside function.
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
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 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 local variable";
In this example, we have defined a variable localVariable having a value “I am local variable”. It is a local variable, as it has 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 function definition are always local variables. They contain values we pass into the function when we call it.
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 becaise 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.
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, 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 variable student2 from the function, the value of local variable has displayed. But when we called student2 from the global scope, the value of global variable has 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 difference between local and global variables in Python. They are as:
- A variable that is declared inside a function is a local variable.
- A variable that is declared outside the function is a global variable.
- 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.
- The local variable creates when the function (in which it has defined) begins executing and it destroyed when the execution of function is complete.
- The existence of a gobal variable remains throughout the entire program execution.
- Programmers more prefer to define local variable 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, we have told about global and local variables in Python with different example programs. Hope that you will have understood the basic points of global and local variables and practiced all programs.
Thanks for reading!!!
Next ⇒ Memory management in Python⇐ Prev Next ⇒