🌍 Python Global vs Local Variables – Explained Simply | TechTown.in

As you dive deeper into Python, you’ll come across situations where the scope of a variable matters a lot. Ever made a variable inside a function and wondered why it disappears outside it? Or tried changing a variable from inside a function and failed?

That’s because of the concept of global and local variables in Python. In this guide, we’ll break it down in a simple, beginner-friendly way.


🧠 What Is a Variable’s Scope?

The scope of a variable determines where in your code it is accessible.

  • 🔒 A local variable is declared inside a function and only exists within that function.
  • 🌍 A global variable is declared outside all functions and is accessible throughout the program.

🔹 Example: Local Variable

def greet():
    name = "TechTown"
    print("Hello", name)

greet()
print(name)  # ❌ Error: name is not defined

Here, name is a local variable. It exists only within greet().


🔹 Example: Global Variable

name = "TechTown"

def greet():
    print("Hello", name)

greet()
print(name)  # ✅ Works fine

Now name is defined outside the function, so it’s globally accessible.


📢 Changing Global Variable Inside a Function

By default, if you assign a value to a variable inside a function, Python treats it as local, even if a global variable with the same name exists.

❌ Example (Unexpected Behavior):

x = 5

def change():
    x = 10  # This creates a new local variable
    print("Inside:", x)

change()
print("Outside:", x)  # Output: 5 (not 10)

The global x is not modified.


✅ To Modify a Global Variable: Use global Keyword

If you really want to change a global variable from inside a function, use the global keyword:

x = 5

def change():
    global x
    x = 10
    print("Inside:", x)

change()
print("Outside:", x)  # Output: 10

Now, the change affects the global version of x.


🔁 Global Variables in Real Projects

While global variables can be helpful for constants or shared values, overusing them can lead to bugs and confusion in large programs.

💡 Best Practice: Use function parameters and return values instead of relying heavily on global variables.


⚠️ Summary: Global vs Local in Python

FeatureLocal VariableGlobal Variable
Declared inInside a functionOutside all functions
ScopeOnly within that functionAccessible everywhere
Modifiable inside functionNo (unless declared global)Yes, using global keyword
LifespanTemporary (until function ends)Exists as long as the program runs

🧩 Common Mistakes to Avoid

  • ❌ Trying to change global variables without global
  • ❌ Assuming variables are automatically global
  • ❌ Overusing global variables instead of passing values

🏁 Final Thoughts

Understanding the difference between global and local variables in Python is crucial for writing bug-free, modular, and maintainable code. Start by keeping most of your variables local, and use global only when absolutely needed.

Practice by writing functions, playing with scopes, and observing how values behave — you’ll master it in no time!


🔗 Learn more smart Python concepts at TechTown.in