Year 10 (ages 14–15, Grade 9 in the US) · Computing · GCSE programming · Lesson 10
Local and global variables, and structured programs
Variable scope in Python: local variables inside subroutines and global variables in the main program, why local variables are preferred, and how to structure a program with a main section, comments and meaningful names.
Exam boards: this lesson matches the AQA and Pearson Edexcel GCSE computing courses. Check which course your school follows.

8-minute video lesson
Teaching, worked examples and 20 practice questions solved step by step.
Watch free for a monthWhat your child will learn
- Explain local and global scope
- Predict output involving scope
- Explain why local variables are preferred
- Structure a program clearly
The method
- Local: created inside a subroutine; only exists there.
- Global: created in the main program; visible everywhere.
- Prefer parameters and return values over globals.
- Structure: comments, names, subroutines, main section.
Key words: scope, local variable, global variable, comment, maintainability.
Worked examples
Worked examples from the lesson, with every step.
Question 1
What happens? c:def make(): c: x = 10 c:make() c:print(x)
- Step 1: x is local to make().
- Step 2: x doesn't exist outside → NameError
Answer: NameError
Question 2
What is output? c:score = 5 c:def show(): c: print(score) c:show()
- Step 1: score is global.
- Step 2: A subroutine can read a global.
Answer: 5
Question 3
Rewrite to avoid a global: a subroutine that adds 1 to lives.
- Step 1: c:def lose(lives):
- Step 2: c: return lives - 1
Answer: pass lives in; return the new value