In Python, variable scope defines where a variable can be accessed in a program. In simple words, scope tells us:
“Where a variable is visible and usable.”
Understanding scope is very important because it helps avoid errors and makes your code more predictable.
🔹 What is Variable Scope in Python?
Variable scope refers to the region of a program where a variable exists and can be used.
If a variable is created inside a function, it may not be accessible outside it.
🔹 Types of Variable Scope in Python
Python has 4 main types of scope:
- Local Scope
- Enclosing Scope
- Global Scope
- Built-in Scope
🔹 1. Local Scope
A variable declared inside a function is called a local variable.
It can only be used inside that function.
🔹 Example of Local Scope
def my_function():
x = 10
print("Inside function:", x)
my_function()
🔸 Output:
Inside function: 10
🔍 Explanation:
-
xis local tomy_function - It cannot be used outside the function
❌ Local Variable Outside Function
def test():
y = 20
print(y)
❌ Error occurs:
-
yis not defined outside the function
🔹 2. Global Scope
A variable declared outside all functions is called a global variable.
It can be used anywhere in the program.
🔹 Example of Global Scope
x = 100
def show():
print("Inside function:", x)
show()
print("Outside function:", x)
🔸 Output:
Inside function: 100
Outside function: 100
🔹 3. Modifying Global Variables
To modify a global variable inside a function, we use the global keyword.
🔹 Example: Using global keyword
x = 50
def change():
global x
x = 200
change()
print(x)
🔸 Output:
200
🔍 Explanation:
-
global xtells Python to use the global variable -
Value of
xis updated inside function
🔹 4. Enclosing Scope (Nested Functions)
When a function is inside another function, the outer function’s variable is in the enclosing scope.
🔹 Example of Enclosing Scope
def outer():
x = 10
def inner():
print(x)
inner()
outer()
🔸 Output:
10
🔍 Explanation:
-
xbelongs to outer function - Inner function can access it
🔹 Modifying Enclosing Scope (nonlocal)
To modify enclosing variables, use nonlocal.
🔹 Example: nonlocal keyword
def outer():
x = 10
def inner():
nonlocal x
x = 20
inner()
print(x)
outer()
🔸 Output:
20
🔹 5. Built-in Scope
Built-in scope contains Python’s predefined functions and keywords.
Examples:
-
print() -
len() -
range() -
type()
🔹 Example of Built-in Scope
print(len("Python"))
🔸 Output:
6
🔹 LEGB Rule in Python Scope
Python follows this order to find variables:
L → Local
E → Enclosing
G → Global
B → Built-in
🔍 Explanation:
Python searches variables in this order.
🔹 Example of LEGB Rule
x = "Global"
def outer():
x = "Enclosing"
def inner():
x = "Local"
print(x)
inner()
outer()
🔸 Output:
Local
🔹 Real-Life Example: Banking System
balance = 1000 # global variable
def deposit(amount):
global balance
balance += amount
deposit(500)
print("Balance:", balance)
🔸 Output:
Balance: 1500
🔹 Why Variable Scope is Important
✔ Prevents variable conflicts
✔ Makes code organized
✔ Improves debugging
✔ Controls data access
✔ Essential for large programs
🔹 Common Mistakes
❌ Using local variable outside function:
def test():
a = 10
print(a)
❌ Forgetting global keyword:
x = 10
def change():
x = 20 # creates new local variable
🚀 Conclusion
Python variable scope defines where variables can be accessed and modified. Understanding scope helps you write cleaner, safer, and more professional code.
Mastering scope is essential for:
- Functions
- OOP (classes)
- Large applications
- Real-world software development


0 Comments