avatarJim McAulay🍁 I'm nobody. Are you a nobody too?

Summarize

Local And Global In Python

Local,Global and Nonlocal Simplified

All variables in Python are global except for variables within functions.

All variables within functions are local except when they are explicitly made global or nonlocal.

A nonlocal variable is used in a function with nested loops. It behaves as if it were global within the function and local Outside of the function.

A variable in python is very simple. It is just the name for a thing that is not a constant, a function or an operator.

In most computer languages you must declare a variable and state what type of thing it will contain. In python, that step happens under the hood.

A variable may contain a string, an integer, a float, a list, a tuple, a set or a dictionary.

A function is a piece of reusable code.

Here is an example:

def simple_function():
     y = 8
     print (y)
simple_function () 
#print (y) This  does not work.

The variable “y” is local it exists only with the function.

When you call the function it prints “8”.

If you try to print “y” outside of the function you will get an error message telling you that the computer does not recognise “y”.

Here is another example:

y = 8
def simple_function():
     print ("y")
simple_function () 
print (y)

The variable “y ” is global, you can use it anywhere even within a function.

When you call the function, it prints “8”.

This time you can use “y” outside of the function.

Here is another example:

y = 8
def simple_function():
     y = 9
     print ("y")
simple_function () 
print (y)

The variable “y” is global. You can use it outside of the function.

The variable “y” inside the function remains local.

You can make a variable inside a function explicitly global.

Here is an example:

def simple_function(): 
     global y    
     y = 9
     print ("y")
simple_function () 
print (y)

When you call the function it will print “9”

Since it is global you can use it outside of the function.

Here is a nested function:

def outer_function ():
    x = 9
    
    def inner_function ():
        x = 7
        print("inner:", x)
    
    inner()
    print("outer:", x)
outer()
# print (x) This does not work.

When the inner function is called it will print “7”.

When the outer function is called it will print “9”

If you declare the second value as global both “x”s would print “7”

and you could use “x” outside of the function. Nonlocal would have the

same effect except that “x” would remain local outside the function.

Here’s an example:

def outer_function ():
    x = 9 
   
    def inner_function (): 
        nonlocal x        
        x = 7
        print("inner:", x)
    
    inner()
    print("outer:", x)
outer()
# print (x) this does not work.

This story is published in a Few Words, Medium’s publication that only accepts stories under 500 words.

If you have a few meaningful words to say and want to be a writer in our publication, visit here.

Python Programming
Recommended from ReadMedium