Python Local Variables(with examples)

Python Local Variables

Hello Techies,

This blog is based on Python Local Variables. In this blog, you will learn about the use of Python local variables with the help of multiple examples.

Below are the points we are covering in this blog:

  • What are Python local variables?
  • How do you create Local Variables in Python?
  • Python local variable referenced before assignment error?

What are Python local variables?

A variable is declared in a function called a local variable. The local variable scope is limited to the function it was created for. This means that the local variable value is only available in the function, but not outside of that function.

Key points of Python Local Variables

  • Python local variables are declared within the function.
  • Local variables can be accessed with the help of a statement in the function in which it is declared.
  • Python Local variables are deleted as soon as any task is completed and they release the occupied memory space.
  • You can specify local variables with the same name in different functions because they are only identified by the declared function.

How do you create Local Variables in Python?

Let us see an example of how to create local variables in Python.

Example 3: Create a Local Variable in Python

Normally we declare a variable within a function to create a local variable.

def my_func():
    x = "local variable"
    print(x)

my_func()

Output is:

local variable

Python local variable referenced before assignment error

Python has a simple rule for local variables, if a variable is assigned to a function, then the variable will be considered as local variables and you can access the variable inside the function only.

Python has two variable scopes: Local variables and Global Variables. Global variables are accessible throughout the program; Local variables are accessible only to the function in which they are originally defined.

Let’s take a look at how to fix this error.

Examples: Python local variable referenced before assignment error

Var1 = 1
Var2 = 0

def function():
    if Var2 == 0 and Var1 > 0:
        print("Result One")
    elif Var2 == 1 and Var1 > 0:
        print("Result Two")
    elif Var1 < 1:
        print("Result Three")
    Var1 = - 1

function()

Output is:

UnboundLocalError: local variable 'Var1' referenced before assignment

According to Python’s rule, If any variable is defined outside of a function, it is known as a global variable, and the variable defined in the function is known as a local variable. The output shows an error because Python treats Var1 as a local variable and Var1 is not defined in the function().

To resolve this error we will use 2 ways

Solution 1: In this solution, we are defining Var1 and Var2 inside the function.

def function():
    Var1 = 1
    Var2 = 0
    if Var2 == 0 and Var1 > 0:
        print("Result One")
    elif Var2 == 1 and Var1 > 0:
        print("Result Two")
    elif Var1 < 1:
        print("Result Three")
    Var1 = - 1

function()

Output is:

Result One

Solution 2: Using the Global Keyword

Var1 = 1
Var2 = 0


def function():
    global Var1, Var2
    if Var2 == 0 and Var1 > 0:
        print("Result One")
    elif Var2 == 1 and Var1 > 0:
        print("Result Two")
    elif Var1 < 1:
        print("Result Three")
    Var1 = - 1


function()

Output is:

Result One

That’s all! We’ve fixed a local variable error in our code.

Check the official site for more information.


FAQ on Python Local Variable

  1. Does Python have local variables?

    Yes, In Python, If a variable defined in the function is known as a local variable.

  2. How do you declare a local variable in Python?

    Any variables modified or created under the function are local variables if not declared as global variables.

  3. What is an UnboundLocalError in Python?

    When a local variable is referenced before assigning, the UnboundLocalError occurs.

  4. How do I fix local variables before assignment in Python?

    You can solve this error by making sure you declare a local variable before you specify a value. If the variable you want to access is declared globally, you can use the keyword “global” to change its value or you can declare that variable (local variable) in the function.

Leave a Comment