GCSE · Computer Science · OCR · Spec J277
Constants
A checkout program needs loads of values. Some change with every beep of the scanner; others must stay put until it stops. Can you tell which is which?
Computer Science · Programming
Sort the checkout's values
Pick a value, then pick the box it belongs in. The test is always the same: can this value change while the program is running?
Still to sort
Constant (0)
Needed by the program, but it won't change during execution.
Where the line is: If the value stays the same from the moment the program starts to the moment it stops, it's a constant. If anything in the run can alter it, it isn't.
Variable (0)
A named value the program can access and change as it runs.
Where the line is: A variable's value gets updated during the run, and each update overwrites the value it held before.
A shop's checkout program needs every value below. Decide which ones should be constants and which should be variables — commit first, then read why.
Computer Science · Algorithms
Trace table — dry run the code
Step through this Python game code. Before each step, guess which column will change.
Ready when you are — step through one line at a time.
Predict, then check
Same SPEED as the trace table. This time somebody tries to change it.
SPEED = 10 sits at the top of a Python program. Much further down, someone writes SPEED = 25. What happens when that line runs?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Every program needs values. The big question is which ones are allowed to change while it runs.
What you need to know
- A variable is a named piece of data in memory that your program can access and change.
- Update a variable and its old value is overwritten: the new value replaces it.
Have a goA variable called score holds 40. Then the line score = 55 runs. What's in score now, and what happened to the 40?
55. The 40 has been overwritten.
A variable keeps only its latest value: the update replaces 40 with 55, so the 40 isn't stored anywhere in score any more.
- A constant holds a fixed value that cannot be changed while the program is running.
- Both live in memory locations, holding values the program needs to run.
- Choose a constant when the program needs a value that won't change during execution.
Have a goYour mate is building a football app. He insists the number of goals in the match should be a constant, "because once a goal's scored, it's scored". Constant or variable?
Variable.
Staying scored isn't the test. The goal count goes up while the program is running, and a value that changes during execution can't be a constant.
- In languages such as Visual Basic and C#, a declared constant is immutable: it can't be changed during execution.
- Python doesn't have constants. A common naming convention is to write the identifier in capitals, like SPEED = 10.
- Capitals don't make the value immutable, so take care not to write code that changes it.
- Constants are usually defined at the top, with meaningful identifiers, so readers know they exist and what they're for.
- Use the named constant throughout, and when the tax rate changes, one edit to the code does the job.
Have a goA program has the tax rate 0.2 typed straight into five different calculations, with no constant. The rate changes. How many edits does the code need?
Five — one for every place 0.2 was typed.
One edit is only enough when a named constant is used throughout. Without one, every typed copy of the value has to be found and changed.
The big picture
A variable is a named piece of data in memory that the program can access and change; each update overwrites the old value. A constant also holds a value the program needs in memory, but that value cannot be changed during execution. Visual Basic and C# can declare a constant that is immutable; Python has no constants, so capitals (SPEED = 10) are only a naming convention. Define constants at the top with meaningful identifiers and use them throughout, so a new tax rate needs one edit.
Key points
Worked example
Problem
A student wrote: "A constant is just a variable with its name in capital letters, so its value can never be changed." Improve this answer.
⚠ Watch out
Thinking that writing an identifier in capitals, like SPEED, locks its value in Python. Python doesn't have constants: capitals are only a naming convention, so the value can still be changed and you have to take care not to.
Memory hook
Variables vary. Constants stay put. And in Python, capitals are a promise, not a padlock.
Check yourself
Cover the page. Name one value a quiz app needs as a constant and one as a variable, and say why. Then: what do capitals in a Python identifier actually do?
Flashcards
(13)What is a variable?
What happens to a variable's previous value when it's updated?
What is a constant?
Where are constants and variables both kept while a program runs?
When should a programmer use a constant rather than a variable?
What does 'immutable' mean for a declared constant?
Give an example of a language where a declared constant is immutable.
Does Python have constants?
How do Python programmers show that a value should be treated as a constant?
Does a capitalised identifier stop Python changing the value?
Where are constants commonly defined, and why there?
Why give a constant a meaningful identifier, like TAX_RATE?
A named tax-rate constant is used throughout a program. The rate changes. How much code needs changing?
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 OCR GCSE Computer Science topics
- 1D arrays
- 2D arrays
- Basic string manipulation (concatenation, slicing)
- Boolean operators AND, OR, NOT
- Casting
- Data type: Boolean
- Data type: Character and string
- Data type: Integer
- Data type: Real
- File handling: open, read, write, close
- Inputs, outputs, assignments
- Iteration (count- and condition-controlled loops)
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 3 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.