What are local variables and global variables in Python with example?

A local variable in Python is a variable that is declared inside a function and is only accessible within that function’s scope. Here’s an example:

def my_function():
    # Defining a local variable
    local_var = 5
    print("Inside the function:", local_var)

# Calling the function
my_function()

# Trying to access the local variable outside the function will result in an error
# Uncommenting the line below will cause a NameError
# print("Outside the function:", local_var)Code language: Python (python)

In this example, local_var is a local variable defined inside the my_function() function. It’s only accessible within the scope of that function. If you try to access the local_var variable outside of the function, you will get a NameError because the variable is not defined in that scope.

Local variables are useful for encapsulating data within a specific function’s scope and are not visible or accessible to other parts of the code. This helps in avoiding unintended conflicts or variable name clashes in larger programs.

What are global variables in Python with example?

In Python, a global variable is a variable that is declared outside of any function and can be accessed and modified from anywhere in the code. Here’s an example of how you can define and use a global variable:

# Defining a global variable
global_var = 10

def print_global():
    # Accessing the global variable inside a function
    print("Inside the function:", global_var)

def modify_global():
    # Modifying the global variable inside a function
    global global_var
    global_var = 20
    print("Inside the function:", global_var)

# Accessing the global variable outside any function
print("Outside the function:", global_var)

# Calling the functions
print_global()
modify_global()
print("Outside the function after modification:", global_var)Code language: Python (python)

In this example, global_var is a global variable that is defined outside of any function. The print_global() function demonstrates how you can access the global variable within a function without any explicit declaration. The modify_global() function shows how you can modify a global variable within a function using the global keyword.

Keep in mind that using global variables extensively can lead to code that is harder to maintain and debug, as they can introduce unexpected behavior due to their unrestricted scope. It’s generally recommended to use global variables sparingly and consider other approaches like function parameters and return values to encapsulate data within specific scopes.

Read More;

  • I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

    View all posts

Leave a Comment