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.

1TAX = 0.2
2score = 0
3score = score + 1
4score = score + 1
5price = 50
6tax_due = price * TAX
7score = 10
8print(score)
TAX
score
price
tax_due
Output
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Two steps, two jobs

DeclarationvsInitialisation

Before a program can use a variable, a memory location has to be allocated for it. These are the two steps people mix up.

Focus

What it does

Declaration

States the variable's name and defines its data type

Initialisation

Assigns the variable its starting value

The insight

Declaration is about what the variable is called and what type of data it holds. Initialisation is about what is in it first.

Example

Declaration

Dim age As Integer (Visual Basic) or int age (C)

Initialisation

age = 22

What the program gets

Declaration

A named memory location set aside for the variable

Initialisation

A value sitting in that variable, ready to use

In Python

Declaration

Not used. Python does not use variable declaration

Initialisation

The only step. age = 22 does the job on its own

Computer Science · Python

Which line makes Python stop?

This Python program crashes. Pick the line that causes the error.

A short program — which line goes wrong?

Computer Science · Python

What do the capitals actually do?

A Python programmer writes SPEED = 10 at the top of their program.

Which idea is closest to what you think the capital letters do?
How sure are you?

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?

Computer Science · Python

Give them better names

x = "Gerry" y = "Hughes" x holds a player's first name. y holds their last name.

Here is a short program with unhelpful variable names. Rewrite both variable names in Python style, then explain why meaningful, consistent names help. [5 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.

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

1A variable is named data in memory holding one value at a time. A new assignment overwrites the old value.
2A constant's value cannot change during execution. Python marks constants with CAPITALS by convention only.
3Declaration states name and data type. Initialisation assigns the starting value. Python uses initialisation only.
4A variable used before it is initialised causes an error; in Python it is a NameError.
5Meaningful lowercase_with_underscores names make code easier to read, modify and debug.
6A named constant like TAX needs only a single change when the value changes.

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?
A named piece of data stored in the computer's memory that a program can access and change.
How many values can a variable hold at once?
One. When a new value is assigned, the previous value is overwritten.
In plain words, what does score = score + 1 do?
Takes the value in score, adds one, and stores the result back in score.
What is a constant?
A value that cannot be changed during the execution of a program. It sits in a memory location like a variable.
What is declaration?
Stating the name of a variable (or constant) and defining its data type, for example int age in C.
What is initialisation?
Assigning a variable its starting value, for example age = 22.
Which of declaration and initialisation does Python use?
Only initialisation. Python does not use variable declaration.
What error does Python give if you print name before assigning it?
NameError: name 'name' is not defined.
What is the Python style for variable names?
Lowercase, with an underscore separating words, such as first_name.
Why choose meaningful, consistent variable names?
The code is easier to read and understand, and easier to modify and debug.
How does Python mark a constant?
Capital letters, such as SPEED = 10, usually near the top of the program. It is only a convention: the value is not immutable.
Which languages can declare an immutable constant?
Some can, such as Visual Basic and C# (Const Speed As Integer = 10). Python does not have constants.
Why use a constant like TAX instead of typing 0.2 everywhere?
If the rate changes only one change to the code is needed, which saves time and reduces errors.

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 Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.