← Python

Functions – Scope

Variables defined inside a function are local and cannot be accessed outside it

What it does: Functions can read global variables without the 'global' keyword. 'global count' declares that count refers to the global variable. Local variables only exist inside the function they're created in. Global variables are defined at the top level and accessible everywhere. x is local to func() — it disappears when the function ends. Inside func, x='local' creates a local x that shadows the global. Each function has its own local scope — same names don't conflict. NameError means Python can't find the variable name — it's out of scope.

Common mistakes: x is local to func it can't be accessed outside. Print inside the function. x is local and dies when func ends. Use return to get the value out.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What is a local variable?
A variable created inside a functionA variable outside all functions
Matches
❌ def func(): x = 10 print(x)
Error: x is local, not visible outside
Applying
Meanings
Term
Scope
Meaning
Where a variable can be accessed - local or global.
Practice
x = 10
Testing
Gaps
# x is a variable
Check Answer
Spots
3
4print(x)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Ermine' badge — Tundra Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What is a local variable?
A. A variable created inside a function ✓
B. A variable outside all functions
Local variables only exist inside the function they're created in
2. What is a global variable?
A. A variable inside a function
B. A variable created outside all functions ✓
Global variables are defined at the top level and accessible everywhere
3. def func():\n x = 5\nprint(x)\nWhat happens?
A. NameError x doesn't exist outside func ✓
B. Prints 5
x is local to func() — it disappears when the function ends
4. x = 10\ndef show():\n print(x)\nshow()\nWhat prints?
A. Error
B. 10 ✓
Functions can read global variables if no local variable has the same name
5. When does a local variable stop existing?
A. When the function finishes ✓
B. When the program ends
Local variables are created when a function is called and destroyed when it returns

Match the Pairs

def func(): x = 10 print(x) Error: x is local, not visible outside
count = 0 def add_one(): count += 1 Error: Can't modify global without 'global'
x = 5 def show(): print(x) show() Valid: Read global inside function
count = 0 def add(): global count count += 1 Ok: 'global' lets you modify it
def func(): y = 99 func() print(y) Output: NameError
msg = 'Hi' def greet(): print(msg) greet() Output: Hi

Key Term

Scope
Where a variable can be accessed - local or global.~~Local variable

Fill in the Blanks

1. def func(): x = 5 # x is a local variable
2. x = 10 # x is a global variable def func(): print(x)
3. def func(): y = 5 func() print(y) # causes NameError
4. x = 'outside' def func(): x = 'inside' print(x) # prints 'inside'
5. def add(a, b): result = a + b return result

Spot the Error

Question 1
A def func():
B x = 5
C
D print(x)
Line D — x is local to func it can't be accessed outside. Print inside the function.
Question 2
A def func():
B x = 5
C
D func()
E print(x)
Line E — x is local and dies when func ends. Use return to get the value out.
Question 3
A def add(a, b):
B result = a + b
C
D add(3, 5)
E print(result)
Line E — result is local to add. Return the value and store it outside.
Question 4
A name = 'Alice'
B def change():
C name = 'Bob'
D
E change()
F print(name) # expects Bob
Line C — The local name inside change doesn't affect the global name. Use return.
Question 5
A def greet():
B msg = 'Hello'
C
D def show():
E print(msg)
Line E — msg is local to greet show can't see it. Pass it as a parameter.
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?