Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Scope & Lifetime of Variables

Lesson 36 of 50 in the free Python Programming notes on Siksha Sarovar, written by Rohit Jangra.

Scope and Lifetime

Scope

Scope determines where in the program a variable is visible/accessible.

ScopeDescription
LocalDefined inside a function; accessible only within that function
GlobalDefined at the top level of a module; accessible everywhere in that module
EnclosingVariables of an outer (enclosing) function, visible to nested inner functions
Built-inNames pre-defined by Python itself (print, len, etc.)

This is called the LEGB rule — Python looks up names in order: Local → Enclosing → Global → Built-in.

x = 100    # global scope

def show():
    x = 10   # local scope -- a DIFFERENT variable, shadows the global one
    print("Inside function:", x)

show()                  # Inside function: 10
print("Outside:", x)     # Outside: 100 (global x is untouched)

Local Variable

def calc():
    result = 50   # local -- exists only during this call
    print(result)

calc()
# print(result)   # NameError: result is not defined outside the function

global keyword

To modify a global variable from inside a function, use the global keyword.

counter = 0

def increment():
    global counter
    counter += 1

increment()
increment()
print(counter)   # 2

Without global, Python treats counter += 1 as creating a new local variable and raises UnboundLocalError.

Enclosing scope — nested functions

def outer():
    msg = "hello"
    def inner():
        print(msg)   # accesses the enclosing function's variable
    inner()

outer()   # hello

Lifetime of Variables

  • Local variables are created when the function is called and destroyed when it returns — their lifetime = the function call's duration.
  • Global variables live as long as the program (module) is running.
def demo():
    temp = "I exist only during this call"
    print(temp)

demo()
demo()   # each call creates a fresh, independent 'temp'