Introduction to Python Global Variable
In general, Variables in any programming language can be defined as an element used to store a value under a certain name. This variable is 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, it is by default a local variable. And similarly, if a variable is declared outside a function, 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 = a = "value for variable" def function_name(): global a statement(s)
Examples of Python Global Variable
Let us consider an example to understand better the working of Global variables.
a = 1 def add_func(): print(a) add_func()
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 a local variable. So to avoid such problems, we use global variables.
a = 1 def add_func(): a = a +1 print (a) add_func()
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 def add_func(): global a a = a+1 print("Inside the function",a) add_func() 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 def variable_func(): a =10 print("Local variable value", a) variable_func() 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.
def out_func(): x = 20 def in_func(): global x x = 25 print("Before in_func() value of x is: ", x) in_func() print("After in_func() value of x is: ", x) out_func() print("The original value of x is or changed value of x is: ", x)
The above example for global variables in a 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 a 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 the 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.
t= 100 def func(): 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) func() 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 has been modified both the times insides the function. So when globals() are declared it referred to a global variable but not the variable with the keyword global, so it will not hide the local variable inside the function.
Global variables in Python programming language have their working, which is useful for accessing or getting the variable’s changed value 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 –