T
Tech Town
Log InSign Up Free
← Back to Blog

June 19, 2025 · admin

Global Variables


๐ŸŒ 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