Change language

Global and Local Variables in Python

# This function uses the global variable s

def f (): 

  print

  
# Global area

s = "I love Geeksforgeeks"

f ()

Exit:

 I love Geeksforgeeks 

If a variable with the same name is defined inside the scope of a function, then it will print the value given only inside the function, not the global value.

# This function has a variable with
# the name is the same as s.

def f (): 

s = " Me too. "

  print

  
# Global scope

s = "I love Geeksforgeeks"  
f ()

print s

Output:

 Me too. I love Geeksforgeeks. 

The s variable is defined as the string "I love Geeksforgeeks" before we call f (). The only statement in f () is the "print s" statement. Since there are no local s, the value from the global s will be used.

The question is, what happens if we change the value of s inside f ()? Will this affect global s? We verify this in the following code snippet:

def f (): 

print s

 

# This program will NOT show an error

# if we comment below the line.

s = "Me too."

 

print s

 
# Global scope < / code>

s = "I love Geeksforgeeks"  
f ()

print s

Output:

 Line 2 : undefined: Error: local variable ’s’ referenced before assignment 

To make the above program work, we need to use the" global "keyword. We only need to use the global keyword in a function if we want to make assignments / change them. global is not required for printing and access. Why? Python "assumes" that we need a local variable because of the assignment to s inside f (), so the first print statement gives this error message. Any variable that is changed or created within a function is local unless it has been declared as a global variable. To tell Python that we want to use a global variable, we must use the "global" keyword, as seen in the following example:

# This function modifies the global variable & # 39; s & # 39;

def f ( ):

global s

print s

s = " Look for Geeksforgeeks Python Section "

  print

 
< code class = "comments"> # Global Scope

s = "Python is great!"  
f ()

print s

Now there is no ambiguity. 
Output:

 Python is great! Look for Geeksforgeeks Python Section. Look for Geeksforgeeks Python Section. 

Good example

a = 1

  
# Uses global because there is no local»

def f ():

print ’Inside f ():’ , a

 
# Variable & # 39; a & # 39; overridden as local

def g (): 

a = 2

print ’Inside g ():’ , a

 
# Uses the global keyword to change the global "a"

def h (): 

global a

a = 3

print ’Inside h (): ’ , a

 
# Global area

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 

This article is contributed by Shwetanshu Rohatgi . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method