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.

6 of 6 still to sort.

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.

Watch out: Don't sort by how important a value is. The tax rate and the basket total both matter. Only one of them changes while the program runs.

Computer Science · Algorithms

Trace table — dry run the code

Step through this Python game code. Before each step, guess which column will change.

1SPEED = 10
2distance = 0
3distance = distance + SPEED
4distance = distance + SPEED
5distance = distance + SPEED
6print(distance)
SPEED
distance
Output
Press Start to run the first line.
·

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?

Same program, two ways

0.2 typed into each calculationvsTAX_RATE defined once at the top

Both versions add tax to food, drinks and delivery. Now imagine the tax rate changes.

Focus

What the code looks like

0.2 typed into each calculation

food_tax = food * 0.2, drinks_tax = drinks * 0.2, delivery_tax = delivery * 0.2

TAX_RATE defined once at the top

TAX_RATE = 0.2 at the top, then food * TAX_RATE, drinks * TAX_RATE, delivery * TAX_RATE

The insight

Same answers when it runs. The difference only shows up when somebody has to change or read the code.

The tax rate changes

0.2 typed into each calculation

Hunt down and edit 0.2 in all three calculations.

TAX_RATE defined once at the top

Edit one line: TAX_RATE = … and no other changes are needed.

Reading the code

0.2 typed into each calculation

A bare 0.2 doesn't say what it's for.

TAX_RATE defined once at the top

The meaningful identifier TAX_RATE says exactly what the value is.

Spotting the fixed values

0.2 typed into each calculation

Buried inside the calculations.

TAX_RATE defined once at the top

Listed at the top of the program.

Your turn

Write it, then mark it

A shop's checkout program uses the tax rate in several different calculations. Explain why the programmer should store the tax rate as a constant. [3 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1A variable is named data in memory that the program can change; each update overwrites the old value.
2A constant holds a value the program needs that cannot be changed during execution.
3Visual Basic and C# let you declare a constant, which is then immutable.
4Python has no constants: capitals such as SPEED are a naming convention and don't stop the value changing.
5Define constants at the top with meaningful identifiers and use them throughout, so a change like a new tax rate needs one edit.

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?
A named piece of data stored in a computer's memory that the program can access and change.
What happens to a variable's previous value when it's updated?
It's overwritten — replaced by the new value.
What is a constant?
A value that cannot be changed during the execution of a program.
Where are constants and variables both kept while a program runs?
In memory locations, each holding a value the program needs.
When should a programmer use a constant rather than a variable?
When the program needs the value but the value won't change during execution.
What does 'immutable' mean for a declared constant?
It is not possible for its value to be changed during program execution.
Give an example of a language where a declared constant is immutable.
Visual Basic or C#.
Does Python have constants?
No. Some programming languages, such as Python, do not have constants.
How do Python programmers show that a value should be treated as a constant?
By the naming convention of writing the identifier in capitals, e.g. SPEED = 10.
Does a capitalised identifier stop Python changing the value?
No. It doesn't make the value immutable, so take care not to write code that changes it.
Where are constants commonly defined, and why there?
At the top of the program, so that people reading the code are aware of them.
Why give a constant a meaningful identifier, like TAX_RATE?
It makes the constant's purpose clear to people reading the code.
A named tax-rate constant is used throughout a program. The rate changes. How much code needs changing?
A single change; no other changes need to be made.

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 posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.