GCSE · Computer Science · Edexcel · Spec 1CP2
Variables
A program says name is 'Sam', then later 'Andeep', then displays name. Which one appears? Guess, then watch it run.
Computer Science · Algorithms
Trace table — dry run the code
Before you press Next, guess what this program will display. Then step through it one line at a time and check.
Ready when you are — step through one line at a time.
Computer Science · Variables
Which names are worth using?
Imagine each name is a variable in a Python program. Sort each one by what it tells a reader and whether it follows Python's convention.
Still to sort
Meaningful and follows Python's convention (0)
Shows what it holds, in lowercase, with underscores between words if needed.
Where the line is: Both tests pass: the name tells the reader what the variable holds, and it is written lowercase with underscores.
Meaningful but breaks the convention (0)
You can tell what it holds, but the style is not lowercase with underscores.
Where the line is: The name passes the first test (it tells you what it holds) but fails the second (Python's convention).
Not meaningful (0)
A reader cannot tell what it holds.
Where the line is: Fails the first test, so the style barely matters: a reader still has no idea what the variable holds.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Named boxes of data that a program fills, reads and overwrites, one line at a time
What you need to know
- A variable is a named piece of data stored in a computer's memory. A program can access it and change it.
- A variable holds one value at a time. Give it a new value and the old one is overwritten.
Have a goYour friend swears: 'I wrote mood = "happy", then mood = "tired", so mood is holding both.' Is your friend right?
No. mood holds only "tired".
A variable holds one value at a time, so the new assignment overwrote "happy".
- score = score + 1 means: take the value score holds now, add 1, and store the result back in score.
Have a goA variable called coins holds 7. What does it hold after the line coins = coins + 1?
8
The right side is worked out from what coins holds now (7), then the result is stored back, overwriting the old value.
- A variable needs a memory location and a starting value before the program can use it.
- Declaring a variable means stating its name and data type. Initialising it means giving it a starting value.
- Python only initialises. Languages such as Visual Basic and C declare the data type as well.
- Use a variable before it has a value and you get an error. In Python that is a NameError.
Have a goPredict it: line 1 is print(age) and line 2 is age = 22. What happens when the program runs?
An error: Python raises a NameError.
Line 1 uses age before any line has given it a value, so there is nothing for it to display.
- Code runs line by line, in the order written, so a variable holds the last value assigned to it.
- Meaningful names show what a variable holds. user_age is helpful; x, y or z are not.
- A naming convention is a standard for names in a language. Python uses lowercase with underscores, like first_name.
The big picture
A variable is a named piece of data in memory. It holds one value at a time, and a new assignment overwrites the old value. Programs run line by line, so a variable must be given a value before the line that uses it. Good variable names say what they hold and follow the language's naming convention.
Key points
Worked example
Problem
What does this program display? level = 1, then level = level + 1, then print(level), then level = 5, then print(level).
⚠ Watch out
Thinking a variable keeps every value it has been given, or that you can use it before it has one. Each new assignment overwrites the old value, and a variable must be initialised before the line that uses it, otherwise Python raises a NameError.
Memory hook
One name, one box, one value. A new value goes in, the old value goes out. (The box is just a picture: the real thing is a memory location holding a named piece of data.)
Check yourself
A program sets rank = 4, then rank = 1, then displays rank. What appears, and why? Use the words 'one value' and 'overwritten'.
Flashcards
(13)What is a variable?
How many values can a variable hold at once?
A variable called price holds 5. The program then runs price = 8. What does price hold?
In n = n + 1, what is worked out first?
What must a variable have before a program can use it?
What does declaring a variable mean?
What does initialising a variable mean?
Does Python declare variables?
What happens if a program uses a variable before it has been initialised?
Why does the order of lines in a program matter?
Which value does a variable hold at any moment?
Why give variables meaningful names?
What is a naming convention, and what is Python's for variables?
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
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.