Python Global variables are those which are not defined inside any function and have a global scope whereas Python local variables are those which are defined inside a function and their 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. Python Local Variables Local variables in Python are those which are initialized inside a function and belong only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable. Creating local variables in Python Defining and accessing local variablesdef f(): # local variable s = "I love Geeksforgeeks" print(s) # Driver code f()
OUTPUT I love Geeksforgeeks
Can a local variable be used outside a function? If we will try to use this local variable outside the function then let’s see what will happen.def f(): # local variable s = "I love Geeksforgeeks" print("Inside Function:", s) # Driver code f() print(s)
OUTPUT NameError: name 's' is not definedPython Global Variables
These are those which are defined outside any function and which are accessible throughout the program, i.e., inside and outside of every function. Let’s see how to create a Python global variable. Create a global variable in Python Defining and accessing Python global variables.# 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
Example If a variable with the same name is defined inside the scope of the function as well then it will print the value given inside the function only and not the global value.# 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
The global Keyword
We only need to use the global keyword in a function if we want to do assignments or change the global variable. global is not needed for printing and accessing. Python “assumes” that we want a local variable due to the assignment to s inside of f(), so the first statement throws the error message. Any variable which is changed or created inside of a function is local if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword “global”, as can be seen in the following example:
Example 1: Using Python global keyword
# 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
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