Global variables are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function.
def f():
# local variable
s = "I love Geeksforgeeks"
print(s)
# Driver code
f()
Output
I love Geeksforgeeks
If we will try to use this local variable outside the function then let’s see what will happen.
Example:
def f():
# local variable
s = "I love Geeksforgeeks"
print("Inside Function:", s)
# Driver code
f()
print(s)
Output
NameError: name 's' is not defined
# This function uses global variable s
def f():
print("Inside Function", s)
# Global scope
s = "I love Geeksforgeeks"
f()
print("Outside Function", s)
Output
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks
The variable s is defined as the global variable and is used both inside the function as well as outside the function.
Note: As there are no locals, the value from the globals will be used.
Now, what if there is a variable with the same name initialized inside a function as well as globally. Now the question arises, will the local variable will have some effect on the global variable or vice versa, and what will happen if we change the value of variable inside of the function f()? Will it affect the globals as well? We test it in the following piece of code:
# This function has a variable with
# name same as s.
def f():
s = "Me too."
print(s)
# Global scope
s = "I love Geeksforgeeks"
f()
print(s)
Output:
Me too.
I love Geeksforgeeks.
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.
The question is, what if we try to change the value of a global variable inside the function. Let’s see it using the below example.
Example:
# This function uses global variable s
def f():
s += 'GFG'
print("Inside Function", s)
# Global scope
s = "I love Geeksforgeeks"
f()
Output
UnboundLocalError: local variable 's' referenced before assignment
To make the above program work, we need to use the “global” keyword. Let’s see what this global keyword is.
# This function modifies the global variable 's'
def f():
global s
s += ' GFG'
print(s)
s = "Look for Geeksforgeeks Python Section"
print(s)
# Global Scope
s = "Python is great!"
f()
print(s)
Output
Python is great! GFG
Look for Geeksforgeeks Python Section
Look for Geeksforgeeks Python Section
Now there is no ambiguity.
Example 2: Using global and local variables
a = 1
# Uses global because there is no local 'a'
def f():
print('Inside f() : ', a)
# Variable 'a' is redefined as a local
def g():
a = 2
print('Inside g() : ', a)
# Uses global keyword to modify global 'a'
def h():
global a
a = 3
print('Inside h() : ', a)
# Global scope
print('global : ', a)
f()
print('global : ', a)
g()
print('global : ', a)
h()
print('global : ', a)
Output
global : 1
Inside f() : 1
global : 1
Inside g() : 2
global : 1
Inside h() : 3
global : 3
|
Explore Courses for Class 9 exam
|