Global Local and Nonlocal Variables in Python

Global Local and Nonlocal Variables in Python

You have also learned about variables in Python in previous posts. If you haven’t read them, click here and learn about variables in Python.

But In this post, you will learn everything about Global Variable in python, Local Variable in python, Nonlocal Variable in python and also how and where to use them in Python programs.

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *