Introduction to Python Global Variable
In general, Variables in any programming language can be defined as an element used for storing a value under a certain name, and this variable used in the memory as an identifying object in the memory unit. It can be of two types, global variables and local variables, in which global variables can be accessed outside the function throughout the program and local variables can be accessed only inside the function. But in python, if a variable is declared inside a function then it is by default a local variable. And similarly, if a variable is declared outside a function then it is by default a global variable.
What are Global Variables?
Global variables are the variables that are outside the function or program but they can be made a global variable for those variables which are inside the function using the keyword “Global”.
a = “value for variable”
Examples of Python Global Variable
Let us consider an example to understand better the working of Global variables.
a = 1
In the above example, the variable “a” is by default a global variable when you print the value of “a” or call a function then it prints 1 as the output. But if we want to access the values that are changing for the variable “a” then when we call the function add_func() we will get an error saying the variable “a” is local variable. So to avoid such problems we use global variables.
a = 1
a = a +1
This will give an error saying variable “a” is a local variable.
Modified programs of the above code to avoid this error use a global keyword.
a = 1
a = a+1
print(“Inside the function”,a)
print(“In main after a change in the value of the variable”, a)
In this modified program above the initial value of variable “a” is 1 when in function add_func() we increment it by1 then the value of a variable inside the function becomes 2, so now the value of variable “a” outside the function changes from 1 to 2.
In the below program it has both local and global variables.
a = 5
print(“Local variable value”, a)
print(“Global variable value”, a)
In this above example which contains both local and global variables, but it is not using a global keyword because the variable “a” which is declared outside function variable_func() which contains value “5” So this variable is by default is considered as a global variable. The variable “a” when declared inside the variable_func() function which is assigned with value “10” this variable is local variable by default again. So the print statement inside the function prints the value of local variable whereas print statement outside the function body prints the value of a global variable such as 10 and 5 respectively.
Global variables can be used in nested function also this can be shown by the below example.
x = 20
x = 25
print("Before in_func() value of x is: ", x)
print("After in_func() value of x is: ", x)
print("The original value of x is or changed value of x is: ", x)
The above example for global variables in nested function, from the above program, we have declared global variable inside the in_func(), whereas the variable “x” in out_func() its value remains same as it is not global variable for in_func() so there is no effect of keyword global which was declared inside the in_func() for variable “x” which is in out_func(). Later before and after calling the in_func() the value of the variable “x” remains 20 as it takes it as a local variable. Similarly, the value of variable “x” outside the out_func() will take the value as 25 because we have created a global variable inside the in_func() using a global keyword. Therefore if we make any changes to the variable “x” inside the in_func() function, so that reflects the change in variable value even outside the in_func() i.e out_func().
In Python, to access both local and global variables inside a function we need to use globals() function which returns a list of items in the current program and can be used to access or modify the global variable without using the global keyword. This can be shown in the below example.
list_of_items = globals()
list_of_items[‘t’] = 15
t = 22
print(“Local value of t is:”, t)
print(“The value of t is:”, t)
print(“Change in the value of t is:”, t)
In the above example, the local and global variable is declared with the same name “t” and it is been modified both the time’s insides the function. So when globals() are declared it referred to a global variable but not the variable with keyword global so it will not hide the local variable inside the function.
Global variables in Python programming language have its working which is useful for accessing or getting the changed value of the variable the global variables can be declared. If the variable is declared outside the function it is global by default but if we want the variable inside the function also to be global then we have to use a global keyword before the variable which is declared inside the function. Both local and global variables can be declared and used in various ways as shown in the above examples.
This is a guide to Python Global Variable. Here we discuss a brief overview on Python Global Variable and its Working along with its Code Implementation. You can also go through our other suggested articles to learn more –