GCSE · Computer Science · Edexcel · Spec 1CP2
Global vs local variables
Quick bet: number = 5 in the main program, number = 10 inside a subprogram. After it runs, what does the main program print?
Computer Science · Algorithms
Trace table — dry run the code
Step through both versions. Before each Next, guess which column is about to change.
Ready when you are — step through one line at a time.
Your turn: predict, then check
Fresh program, same trap. Commit to an answer before you look.
triple() has no global line, so amount is not a global variable inside it. What does this program output, line by line? amount = 100 def triple(n): amount = n * 3 print(amount) triple(100) print(amount)
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
One name, two different variables - and the one word that makes them the same.
What you need to know
- The scope of a variable is where in a program it can be accessed and modified: either global scope or local scope.
- A local variable only exists in the part of the program where it is declared, so the main program cannot access one made inside a subprogram.
- A global variable can be accessed and modified from anywhere in the program, even inside subprograms.
Have a goMaya says: "A local variable is just a global variable that got shy." Using what you have read so far, correct her in one sentence.
A local variable can only be reached inside its subprogram; a global one can be reached from anywhere in the program.
The difference is where the variable can be accessed (its scope), not shyness, size or how it is stored.
- In Python, assigning to a name inside a subprogram makes a new local variable, unless you declare it global.
Have a goThe main program sets level = 1. A subprogram with no global line then runs level = 9. Which variable did that line change: the main program's level, or a new one?
A new local level. The main program's level is still 1.
Assigning to a name inside a subprogram makes a new local variable unless it is declared global.
- A local variable can share its name with a global one, but they are two different variables.
- Writing global number inside the subprogram makes that line update the global variable instead.
Have a goSame program as before, but now the subprogram starts with global level before level = 9. After it runs, does the main program's level still hold 1?
No. It now holds 9.
The global declaration makes that line update the global variable instead of creating a local one.
- Global variables are generally bad practice: their value is hard to track, and functions using them are harder to test alone.
- Instead, pass values in as parameters, return the result, and store it in the main program.
The big picture
A variable's scope is the part of a program where you can reach it. Local variables live inside one subprogram, globals can be reached anywhere, and in Python an assignment inside a subprogram makes a new local variable unless you say global.
Key points
Worked example
Problem
Work out which variables are global and which are local, then say what this program outputs. level = 3 def show(): bonus = level * 2 print(bonus) show() print(level)
⚠ Watch out
Thinking a variable with the same name is the same variable everywhere. Inside a subprogram, an assignment like number = 10 makes a new local variable unless it is declared global, so the main program's number is not touched.
Memory hook
Same name, two variables. Inside a subprogram, assigning makes a new local one - unless you write global first.
Check yourself
The main program sets lives = 3. A subprogram with no global line runs lives = 1. What does the main program's lives hold afterwards, and which word would change that?
Flashcards
(13)What is the scope of a variable?
What is a local variable?
What is a global variable?
Python: a variable is assigned inside a subprogram. Local or global?
What does writing global number inside a subprogram do?
The main program sets number = 5. A subprogram (no global line) sets number = 10. What does the main program then print, and why?
Same program, but the subprogram starts with global number. What is printed inside and outside?
Can the main program print a variable that exists only inside a function?
If a local and a global variable share a name, are they the same variable?
Give two reasons global variables are generally seen as bad practice.
What does it mean to test a function "in isolation"?
What can you use instead of a global variable to share a value with a subprogram?
A subprogram is called as increase_score(score). Why might the main program's score not change?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
- Code readability techniques
- Converting algorithms to programs
- CSV file I/O
- Decomposition and abstraction in programming
- Evaluating program fitness and efficiency
- Functions vs procedures
- Identifying and correcting program errors
- Primitive data types
- Programming – condition-controlled repetition
- Programming – count-controlled repetition
- Programming – iteration over data structures
- Programming – selection
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 21 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.