GCSE · Computer Science · Edexcel · Spec 1CP2
Variables and constants in programs
A maths teacher would say score = score + 1 is impossible. A computer happily runs it millions of times a minute. Let's see why both of them are right.
Computer Science · Algorithms
Trace table — dry run the code
Step through the program one line at a time. Watch what happens in the score column, then spot the column that never changes.
Ready when you are — step through one line at a time.
Predict, then check
Same shop program, written two ways. Commit to an answer before you look.
Version A types 0.2 on each of 5 lines that work out tax. Version B starts with TAX = 0.2 and uses TAX on those 5 lines. The tax rate changes. How many lines must be edited in each version?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Named boxes in memory: one keeps changing, one never should.
What you need to know
- A variable is a named piece of data in the computer's memory that a program can read and change.
- A variable holds one value at a time. Assign a new value and the old one is overwritten.
- So score = score + 1 means: take what's in score, add one, put it back.
Have a goYour friend Sam insists score = score + 1 is impossible, because nothing can equal itself plus one. What is the line really telling the computer to do?
Take the value in score, add 1, and store the result back in score.
It is an instruction to update the box, not a claim that two things are equal, so the old value is read first and then overwritten.
- A constant also sits in a memory location, but its value cannot change while the program runs.
- Declaration states a variable's name and data type, like int age in C. Initialisation gives it a starting value, like age = 22.
- Python has no declaration step. It only uses initialisation.
Have a goYou need to create a variable called age holding 22 in Python. Which is the whole job: age = 22, or a name-and-type line followed by age = 22?
age = 22 on its own.
Python does not use variable declaration, only initialisation, so assigning the starting value is the only step.
- Use a variable before it has been initialised and you get an error. In Python it is a NameError.
- Name variables meaningfully: name = 'Gerry' beats x = 'Gerry'. Python style is lowercase with underscores, like first_name.
Have a goA Python variable stores a player's best score. Write a name for it in Python style.
best_score
Python names are lowercase with an underscore between words, and the name says what is stored.
- Visual Basic and C# can declare an immutable constant. Python can't, so SPEED = 10 in capitals is only a naming convention.
- A constant with a clear name, like TAX, means a changed tax rate needs only one edit.
The big picture
A variable is named data in memory that holds one value at a time and is overwritten when you assign a new one. A constant's value does not change while the program runs. Along the way: declaration versus initialisation, the NameError, good Python names, and why constants save you work.
Key points
Worked example
Problem
What does this program print, and what is in lives after each line? lives = 3 lives = lives - 1 lives = lives - 1 print(lives)
⚠ Watch out
Reading score = score + 1 as a maths equation that can never be true, instead of as an instruction: read the old value, add one, store the result back.
Memory hook
score = score + 1 is not maths. It is: take what's in the box, add one, put it back.
Check yourself
Cover the screen. A program starts with TAX = 0.2 and score = 0, then runs score = score + 1 three times. What is in each at the end?
Flashcards
(13)What is a variable?
How many values can a variable hold at once?
In plain words, what does score = score + 1 do?
What is a constant?
What is declaration?
What is initialisation?
Which of declaration and initialisation does Python use?
What error does Python give if you print name before assigning it?
What is the Python style for variable names?
Why choose meaningful, consistent variable names?
How does Python mark a constant?
Which languages can declare an immutable constant?
Why use a constant like TAX instead of typing 0.2 everywhere?
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
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
- Reading, writing, analysing and refining programs
- Single entry/exit code blocks
- String manipulation
- Structural components of programs
- Structured data types
- User input handling
- Validation – length check
- Validation – presence check
- Validation – range check
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.