Global and Local variable functions
In the world of programming if you want to perform any operation then variables are the first ones to come into the picture. Variables are something that occupies memory and are used to store different data values for further use in a program.
Now coming to the point, there can be several types of variables such as Global, Local and even Nonlocal variables in python. We wont be discussing much about Nonlocal variables here.
GLOBAL VARIABLES
What do we mean by Global?
It simply means that something that is not restricted to some particular scope.
- That means Global variables are variables that are not restricted to a particular function of the program.
- These are variables that can be actually used inside or outside any function of the program.
- Hence, the scope of a global variable is global, and they are not declared within any particular function.
- Global variables are generally created at the beginning of a program i.e at the top of a program.
CREATING AND ACCESSING GLOBAL VARIABLE
The declaration of a global variable will be outside all the blocks of the program.
For example:
x = "CodingNinjas"
def main():
print("x inside:", x)
#calling main function
main()
print("x outside:", x)
OUTPUT FOR GLOBAL VARIABLES:
x inside: CodingNinjas
y outside: CodingNinjas
x is the global variable in the above Python code.
What gives us such confidence?
- To output the value of x within the main, we created a main() function.
- After that, the print statement outside the main will output the value of x.
- And they both produce exactly the same results.
Thus, x is a global variable because its value is independent of any function and can be used anywhere in the code.
So now we know how to create global variables in Python, but what if we want to modify the value of a global variable in Python?
MODIFY GLOBAL VARIABLE IN PYTHON
a = 15
print("value before modification",a)
#modify a global value
def modify():
#let's change the global variable a
a = a*2
print("after modification, value in function",a)
modify()
print("after modification, value outside the function",a)
OUTPUT FOR MODIFY GLOBAL VARIABLE
value before modification 15
Traceback (most recent call last):
File "main.py", line 9, in <module>
modify()
File "main.py", line 6, in modify
a = a*2
UnboundLocalError: local variable 'a' referenced before assignment
Here, the value before modification will be printed as 15 but since we tried to modify the value of a global variable inside the function it will give an error because the same named variable 'a' acts like a Local Variable to that function and so we can't modify this way.
So, for modification we will here use the global keyword in python as:
a = 15
print("value of 'a' before modifying=",a)
#modify a global value
def modify():
#make the use of a global keyword to change the value actually
global a
a=5
print("after modification,value of'a'in function=",a)
modify()
print("after modification,value of 'a'outside the function=",a)
OUTPUT FOR GLOBAL KEYWORD:
value of 'a' before modifying= 15
after modification,value of'a'in function= 5
after modification,value of 'a'outside the function= 5
From the output, it is clear that now the value is modified, and so, for modifying the value of a global variable outside the context of a particular function, we use the global keyword in python.
LOCAL VARIABLES
While modifying the global variable without using the global keyword, you must have encountered the word "local variable". What does it mean by local?
Something whose scope is restricted to a particular scope that is local and are called local variables.
- Local variables have a local scope that is restricted to a particular block in which they are declared.
- They are basically declared within the definition of a particular function.
- They cannot be accessed outside the block in which they are declared.
CREATING AND ACCESSING LOCAL VARIABLE
Whatever variables we use in a function are local to that function. So, we declare the local variable in the block where we want to use it, i.e within the boundary of that function, and thus, it's scope gets finished within that block only.
def fun1():
x=18
print("Value of 'x' is local to fun1() and x=",x)
def fun2():
y=20
print("Value of 'y' is local to fun2() and y=",y)
fun1()
fun2()
OUTPUT FOR LOCAL VARIABLE:
Value of 'x' is local to fun1() and x=18
Value of 'y' is local to fun2() and y=20
Try to access 'X in fun1() and 'y' in fun2()-
def fun1():
x=18;
print("Value of 'y' is",y)
def fun2():
y=20;
print("Value of 'x' is",x)
fun1()
fun2()
Here, we will get an error. From this it's clear that a local variable can be accessed within it's scope only.
MODIFYING LOCAL VARIABLES IN PYTHON
Modifying local variables in Python is an easy peasy task as shown below:
def fun1():
#scope of x is local to fun1
x=18
print("Value of 'x' before modification is=",x)
#try to modify x
x=x+20
print("Value of 'x' after modification is=",x)
fun1()
OUTPUT FOR MODIFYING LOCAL VARIABLES:
Value of 'x' before modification is= 18
Value of 'x' after modification is= 38
So, just wonder what would happen if there is a local as well as global variable of the same name in the same program?
#declaring a global variable
p =8;
def fun():
#local variable with the same name as global variable
p=18;
print("value of p=",p)
fun()
OUTPUT:
value of p= 18
Hence, when a local variable and global variable have the same name, the priority is given to a local variable of that function, which only gets printed.
Comments
Post a Comment