Python Global Local and Nonlocal Variables

Python local, global and nonlocal variable; Through this tutorial, i am going to show you about local, global and nonlocal variables in Python.

Python Global Local and Nonlocal Variables

  • Global variables python
  • Local variables python
  • NonLocal variables python

Global variables python

In Python programming, the variable is declared outside the function. It is called a global variable. In Python or in any programming language, the global variable is used both inside and outside the function.

Let’s take an example. In this example, we will create a global variable as well as see how to use it:

Example: python create a global variable

a = "global"
def foo():
    print("a inside :", a)
foo()
print("a outside:", a)

Output

a inside : global
a outside: global

Explanation of the above program

In this example, we have declared a global variable. Whose name is a.

After that we created a function called foo () function.

When we have created the function. After that, we called the global variable outside and inside the function. As you can see in the given program.

Note: But what we have created in the global variable program. You cannot modify it inside the function. If you have to modify inside the function with the global variable declaration, then you will have to use the global keyword.

Local variables python

In Python programming, the variable inside the function is declared. It is called a local variable. In Python or in any programming language, the local variable is used within the function itself. This cannot be used outside the function.

Example 1: Access local variable outside function python

Let’s take an example of the local variable. And in this example, accessing the local variable outside the function, let’s see:

def foo():
    y = "local"
foo()
print(y)

If you are a local variable that is defined inside the function. If you are using it outside the function, it will throw an error. Which it will be:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
    print(y)
NameError: name 'y' is not defined

Note: Simply this means that you cannot access the python local variable outside the function.

Example 2: Access local variable outside function python

We have seen in the above example that the local variable cannot be used outside the function.

Now we start another example and will see how the python local variable is used inside the function:

def foo():
    y = "local"
    print(y)
foo()

Output

local

Explanation of above example 2

Like Sam Upper, we wrote a program. And in this program created a function named foo () function.

Then define a local variable inside this foo () function. And also assign a value to this local variable.

When called this function in the last, this property was executed and we got the output that you can see the equipment.

You have read about global and local variables in this post. And also how to create and use the global variable and local variable. Got it with the example too.

NonLocal variables python

You have read about global and local variables in this post. And also how to create and use the global variable and local variable. Got it with the example too.

But now you will know what are the nonlocal variables in python. And how to use it in Python programming.

It is used when we work with the nested functions and we need to use a function which is declared in the outer function. the nonlocal variable is declared using the nonlocal keyword.

Example: python nonlocal variable

def outer():
    x = "local"
    
    def inner():
        nonlocal x
        x = "nonlocal"
        print("inner:", x)
    
    inner()
    print("outer:", x)
outer()

Output

inner: nonlocal
outer: nonlocal

Recommended Python Tutorials

Recommended:-Python Data Types
Recommended:-Functions in Python

Leave a Comment