Scope of variables in Python
 

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.

 

Previous Introduction to Python functions
Next Python List Data Type
 

About author

You might also like

Programming 0 Comments

Introduction to Python functions

This post was last updated on March 6th, 2021 at 08:20 amA Python function is a self-contained module of code that performs a specific task. It takes inputs, processes them,

Programming 0 Comments

Using Python break and continue keywords

The break and continue are keywords used in Python to alter the normal flow of loops. A loop iterate over a line or block of code repeatedly until the given

Programming 0 Comments

Python enumerate() function

Sometimes you may want to track the position of items in an iterable. You can do this in various ways, for example, you can create a counter variable and initialize

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply