by: Cobena Isaac

Python Variable Scope

Python Variable Scope

In Python, variable scope defines where in your code a variable can be accessed or modified. Think of scope as the visibility range of a variable — where it lives and where it can be seen.

Local Variable

A local variable is created inside a function and can only be used within that function. It exists temporarily and disappears once the function completes.

1
2
3
4
5
6
def greet():
    message = "Hello, World!"  # local variable
    print(message)

greet()             # Output: Hello, World!
# print(message)    # Error: 'message' is not defined
  • message is local to the greet() function.
  • Trying to use it outside the function causes an error because it doesn’t exist in that scope - (i.e., It cannot be accessed outside the function where it was created)

Global Variable

A global variable is defined outside any function and can be accessed anywhere in your program — both inside and outside functions.

1
2
3
4
5
6
7
name = "Alice"  # global variable

def show_name():
    print(name)  # Can access global variable

show_name()      # Output: Alice
print(name)      # Output: Alice
  • name is available throughout your entire program
  • Global variables exist as long as your program runs
  • If a variable name is the same locally and globally, the local version wins inside the function.

Analogy: Local variables are like private notes inside a function, while global variables are public announcements everyone can read.

Modifying Global Variables Inside a Function

Inside a function, you are not allowed to change a variable that was created outside of it, unless you use the special word global to get permission, (i.e., By default, Python does not allow you to modify a global variable from inside a function unless you use the special keyword).

1
2
3
4
5
6
7
8
count = 0  # global variable

def increase():
    global count  # Permission to modify global variable
    count += 1

increase()
print(count)  # Output: 1

Best Practice: Use global variables carefully — too many of them can make your code harder to debug.

Enclosing Scope (Nested Functions)

When you define a function inside another function, the inner function can access variables from the outer function—this is called enclosing scope.

1
2
3
4
5
6
7
8
9
def outer():
    outer_var = "I am outside"
    
    def inner():
        print(outer_var)
    
    inner()

outer()  # Output: I am outside
  • The variable outer_var belongs to the outer function (outer()).
  • The inner function inner() can still access it — like a child borrowing something from the family living room.

Analogy: Think of a family in a house. A child in their bedroom (inner function) can use their own toys (local variables) and also access shared items in the living room (enclosing scope).

Built-in Scope

Python has a built-in scope containing reserved names like print(), len(), and range() that are always available unless you accidentally override them.

1
print(len("Hello"))  # Output: 5

Avoid using variable names that shadow built-ins.

1
2
len = 10  # Bad idea - overrides built-in len()
print(len("Hi"))  # Error: 'int' object is not callable

Try It Yourself

1
2
3
4
5
6
7
8
9
10
11
12
13
14
x = "global"

def outer():
    x = "enclosing"
    
    def inner():
        x = "local"
        print("Inner:", x)
    
    inner()
    print("Outer:", x)

outer()
print("Global:", x)

Output:

1
2
3
Inner: local
Outer: enclosing
Global: global

Key Takeaways:

  • Local variables: Exist only inside their function
  • Global variables: Accessible throughout your entire program.
  • Enclosing scope: Inner functions can access outer function variables.
  • Built-in scope: Reserved names and functions provided by Python.
  • Use global carefully if you need to modify global variables inside functions.

Now that you understand variable scope, continue to Python Constant to learn how to work with different data types and structures in Python.

Hope you found this tutorial helpful!.

Thanks for reading!