Global and local variables in python
In python language, the variable we define and declare
outside from the function is known as global variable. That’s a clear meaning
that global variable can be accessed inside or outside of a function.
Let’s see by an example
# This
function uses global variable a
def
cod():
print (a)
# Global
scope
a =
"I use codedudle"
cod()
Output:
I use codeddudle
Here the variable ‘a’ is defined as the string “I use codedudle” before
we call the function cod(). The statement in cod() is the statement of print.
So, there is no local a, the value from the global a will be used.
What will happen if a variable with the same name is defined inside the
scope of function as well then, it will print the value given inside the
function only and not the global value.
Lets see by an example
# This
function has a variable with
# name
same as a.
def
cod():
a =
"nice blog."
print (a)
# Global
scope
a =
"I use codedudle"
cod()
print (a)
Output:
nice blog.
I use codedudle
Let’s dive a little deeper, what
will happen if we change the value of ‘a’ inside of the function cod()? Will it
affect the global ‘a’ as well?
We will see in the following piece of code:
def
cod():
print (a)
# This
program will NOT give an error
# if we
comment below line.
a =
"nice blog"
print (a)
# Global
scope
a =
"love to read codedudle"
cod()
print (a)
Output:
UnboundLocalError: local variable 'a' referenced before assignment
To make the above program work properly, we need to use
“global” keyword. We can only need to use the global keyword in a function if we
want to do assignments / change them. Now Global is not needed for printing and
accessing. Why? Python “assumes” that we want a local variable due to the
assignment to ‘a’ inside of cod(), so the first print statement throws this
error message. Any variable that is changed or created inside of a function is
local, if it hasn’t been declared as a global variable. To tell the Python, that we
want to use the global variable, we have to use the keyword “global”, as can be seen in
the following example:
# This
function modifies the global variable 's'
def
cod():
global a
print (a)
a =
"best blog for python is codedudle"
print (a)
# global
Scope
a =
"Hello world !"
cod()
print (a)
Output:
Hello world !
best blog for python is codedudle
best blog for python is codedudle
Lets see an brief example
a = 4
# Uses of global because there is no local 'a'
def
cod():
print
('Inside cod() : ', a)
#
Variable 'a' is redefined as a local
def
dud():
a = 5
print
('Inside dud() : ',a)
# Global
scope
print
('global : ',a)
cod()
print
('global : ',a)
dud()
Output:
global : 4
Inside cod() : 4
global : 4
Inside dud() : 5
#python list
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai
#python computer science
#learn python step by step
2 Comments
Ekdam mast hai 🙈
ReplyDeleteThis article is a great article that I have seen in my python programming career so far,
ReplyDeletehire python developers in US
If you have any doubt, Please let me know.