HOME

What-are-local-and-global-variables-in-python-by-Gagan-Khanna

understanding the nature of Global and local Variables in Python

What-are-local-and-global-variables-in-python-by-Gagan-Khanna

What-are-local-and-global-variables-in-python-by-Gagan-Khanna

understanding the nature of Global and local Variables in Python , Mar-24-2026

1. Local Variables

A Local variable is defined inside a function. Its existence is confined to that function's "block." Once the function finishes executing, the local variable is destroyed, and its data is no longer accessible.

Example:

Python
def greet():
    message = "Hello, Local Scope!"  # This is a local variable
    print(message)

greet()
# print(message)  # This would raise a NameError
  • Scope: Limited to the function greet().

  • Accessibility: Cannot be accessed from outside the function.


2. Global Variables

A Global variable is defined outside of any function, usually at the top level of the script. These variables are accessible from anywhere in the code—both inside and outside of functions.

Example:

Python
total_score = 100  # This is a global variable

def show_score():
    print(f"Your current score is: {total_score}")

show_score()
print(f"Global access: {total_score}")
  • Scope: The entire program.

  • Accessibility: Accessible by any function in the file.


3. The global Keyword

By default, if you try to modify a global variable inside a function, Python treats it as creating a new local variable with the same name. To modify the actual global variable, you must use the global keyword.

Example:

Python
count = 0

def increment():
    global count  # Tells Python to use the global 'count'
    count += 1
    print(f"Inside function: {count}")

increment()
print(f"Outside function: {count}")

4. Key Differences at a Glance

FeatureLocal VariableGlobal Variable
DeclarationInside a function.Outside all functions.
LifetimeExists only while the function runs.Exists until the program ends.
AccessibilityOnly within that specific function.Throughout the entire script.
ModificationDirect access within the function.Requires the global keyword to modify.

    a = 5
print("I am variable named a at the top of the program my value is", a)

def myfun():
    # This function simply reads the global variable 'a'
    print("the value of a is", a)

myfun()

def myfun2():
    # This creates a NEW local variable 'a' that only exists inside this function
    a = 4
    print("i am value of a inside myfun2", a)

myfun2()
print("Outside myfun2, the global a is still", a)

def myfun3():
    global a  # This tells Python we are talking about the 'a' at the very top
    a = 3     # This actually CHANGES the global variable
    print("I used the global keyword. The value of a is now", a)

myfun3()
print("After myfun3, the global a has been changed to", a)


Output

I am variable named a at the top of the program my value is 5

the value of a is 5

i am value of a inside myfun2 4

Outside myfun2, the global a is still 5

I used the global keyword. The value of a is now 3

After myfun3, the global a has been changed to 3

Comments 0


What-are-local-and-global-variables-in-python-by-Gagan-Khanna

While global variables might seem convenient, overusing them can make debugging difficult because any part of your program can change their value unexpectedly. It is generally better to use Local variables and pass data between functions using arguments and return values.


Popular

  • Local Variables In Python
  • Global Variables In Python
  • Scope Of Variables In Python
  • Difference Between Local And Global Variables In Python

Esmio...

Teachlive Flagship Program of Esmiohosting.com

Aaja.in

Boost via Reels & Images

Citylead

Publish Business Articles

Quick...

Professional Help for Growth

Tags

Local Variables In Python Global Variables In Python Scope Of Variables In Python Difference Between Local And Global Variables In Python


Topics You may be intrested in










































































































Teachlive is a platform dedicated to both teachers and students, providing free resources to help spread knowledge and benefit students in their educational journey. Under guidence of Seema Khanna ,at Teachlive, we offer much more than just academic support; we help students develop critical life skills, offer parental guidance, and keep everyone informed about the latest changes in the Indian education system.


Please Take a while to visit our sponsors website Shopeze.in - Home Decoration Items