Day 9 45 min beginner

Variable Scope

Understand where your variables live and how to access them

Learning Objectives

  • Understand the difference between Local and Global scope
  • Learn how to use the 'global' keyword
  • Avoid common pitfalls with naming variables

Where does a variable live?

In Python, not all variables are accessible from everywhere in your code. The region where a variable is defined and can be accessed is called its Scope.

Local Scope

A variable created inside a function belongs to the local scope of that function. It only exists while the function is running.

local_scope.py
1
2
3
4
5
6
def my_function():
    x = 10  # Local variable
    print(x)

my_function()
# print(x) # ERROR! x is not defined outside the function

Global Scope

A variable created in the main body of a Python file is a global variable and belongs to the global scope. Global variables are available from within any scope, next to global or local.

global_scope.py
1
2
3
4
5
6
7
x = 300 # Global variable

def my_function():
    print(x) # Accessing global variable

my_function()
print(x)

Naming Conflicts

If you operate with the same variable name inside and outside of a function, Python will treat them as two separate variables: one local and one global.

conflicts.py
1
2
3
4
5
6
7
8
x = 50

def my_function():
    x = 20 # This creates a NEW local variable x
    print(f"Local x: {x}")

my_function()
print(f"Global x: {x}")

The global Keyword

If you need to modify a global variable from inside a function, you must use the global keyword.

global_keyword.py
1
2
3
4
5
6
7
8
9
count = 0

def increment():
    global count
    count += 1
    print(f"Internal count: {count}")

increment()
print(f"External count: {count}")
Use Global Sparingly
Relying too much on global variables can make your code harder to debug and understand. It’s usually better to pass values into functions using parameters and get results back using return.

Interactive Practice

Think about a video game. The player_score might be global, but a bonus_points variable inside a “calculate_level_end” function should probably be local.

flowchart TD Global[Global Scope: level_name, player_name] Global --> Function[Function Scope: enemy_hp, time_left] Function --> Hidden[Cannot see Global level_name?] Hidden -- No --> Access[Function CAN see Global] Global -- Can see Function items? --> No[No, Global CANNOT see local items]

Quiz

Complete this quiz with a minimum score of 80% to mark Day 9 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!