Global and Local Variables in Python
×

Global and Local Variables in Python

117

Introduction

In Python, variables can have different scopes depending on where they are declared. Understanding the distinction between global and local variables is crucial for writing effective and error-free code. This article explores these concepts with clear examples.

Local Variables

A local variable is defined within a function and is accessible only within that function's scope. It is created when the function starts and destroyed when the function ends.

def greet():
    message = "Hello, World!"
    print(message)

greet()
print(message)  # This will raise an error

Output:

Hello, World!
NameError: name 'message' is not defined

In the above example, message is a local variable inside the greet function. Attempting to access it outside the function results in a NameError.

Global Variables

A global variable is defined outside any function and is accessible throughout the program, including inside functions.

status = "Active"

def show_status():
    print("Status:", status)

show_status()
print("Outside Function:", status)

Output:

Status: Active
Outside Function: Active

Here, status is a global variable accessible both inside and outside the show_status function.

Variable Shadowing

If a variable with the same name is defined both globally and locally, the local variable shadows the global one within its scope.

value = 10

def modify_value():
    value = 20
    print("Inside Function:", value)

modify_value()
print("Outside Function:", value)

Output:

Inside Function: 20
Outside Function: 10

In this case, the local value inside modify_value does not affect the global value.

Modifying Global Variables Inside Functions

To modify a global variable inside a function, you must declare it as global within the function.

count = 0

def increment():
    global count
    count += 1

increment()
print("Count:", count)

Output:

Count: 1

Without the global declaration, attempting to modify count inside the function would result in an UnboundLocalError.

Conclusion

Understanding the difference between global and local variables in Python is essential for managing variable scope and avoiding common errors. Always be cautious when modifying global variables within functions, and use the global keyword judiciously to maintain code clarity and integrity.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat