Scope of variables in Python


A variable in Python is used to store a value in the memory. The value of a variable can be changed later in the program. Python is a dynamically typed programming language so we do not need to declare a variable before using it. Instead, it is created when you first assign a value to it.

The scope of a variable indicates where you can use a declared variable and where not in the Python program. This is important to understand the scope of variables for a better understanding of language.

Local variables

A variable declared inside a function or block is known as a local variable. It can be used only inside that function or block in which they are declared. Now see the example below –

def my_func():
       x=10
       print(x)
my_func()
print(x)

Where x is a local variable that is declared inside my_func(). Now when you execute this program you will see –

Since x is initialized inside the my_func() its scope will be limited to this function so when we are calling my_func() it prints the value of x. And the next statement which is given outside the function definition produces NameError: name ‘x’ is not defined.

Global variables

The variable declared outside a function or block of code in a Python program is known as a global variable. It can be accessed anywhere in the program. Now on modifying the above program like given below –

x=20
def my_func():
      x=10
      print(x)
my_func()
# This will print the value of global variable x
print(x)

Now when you call my_func() this will print the value of x defined within the function. The print statement given outside the function will print the value of global variable x. You see the output in the given image –

Using global keyword

If you want to create a global variable from inside of a function or block then you can do this by using the global keyword. Now see the example below –

def my_func():
       global x
       x=10
       print(x)
my_func()
print(x)

X is now a global variable you can access it from the outside of the function. You can see the output which is given in the image below.

Python nonlocal variables

The nonlocal variables are used in the nested function, we use nonlocal keyword to declare a variable that is not local. Let’s understand the Python nonlocal variable with the help of the given example.

def my_func1():
    x = 20
    def my_func2():
      x = 10
      print(x)
# Calling my_func2() will print the value of x which is inside my_func2()
    my_func2()
# The given statement will return the value of x which is outside of my_func2()
    return x
print(my_func1())

When you execute this program first my_func1() will be called. This will call the inner function my_func2() which will print the value of x given inside it.  Next, the value of x inside my_func1() will be returned and printed. You can see the output of this in the given image –

Now, what happens when you declare x as a non-local variable, see the example below-

def my_func1():
    x = 20
    def my_func2():
      nonlocal x
      x = 10
      print(x)
# Calling my_func2() will print the value of x which is inside my_func2()
    my_func2()
# The given statement will return the last assigned value of x
    return x
print(my_func1())

Now when you run this code you will see –

Now you know what is the scope of a variable in Python. If you have a query then write us in the comments below.

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.