Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python Variable Scope (Complete Guide for Beginners)

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:

  1. Local Scope
  2. Enclosing Scope
  3. Global Scope
  4. 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:

  • x is local to my_function
  • It cannot be used outside the function

❌ Local Variable Outside Function

def test():
y = 20

print(y)

❌ Error occurs:

  • y is 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 x tells Python to use the global variable
  • Value of x is 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:

  • x belongs 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

 



Post a Comment

0 Comments