GCSE · Computer Science · OCR · Spec J277

Variables

In maths, score = score + 1 is impossible: nothing equals itself plus one. In a program, it works perfectly well. So what is that = sign really doing?

Computer Science · Variables

Watch the box change

Five lines of Python and one variable called score. Step through and keep your eye on the Variables panel: it shows what is stored in score right now, and nothing else.

Before you press Next: what number do you think line 5 will print?

1 score = 0
2 score = score + 1
3 score = score + 1
4 score = score + 10
5 print(score)

Variables

score=0

Output

 

Step 1: A memory location is allocated for score's value, and 0 goes in. Giving a variable its starting value like this is called initialising it.

1 / 5

Use Next and Back to move one line at a time. The Variables panel shows the value stored in score after each line runs.

Step 1 of 5: A memory location is allocated for score's value, and 0 goes in. Giving a variable its starting value like this is called initialising it..

Watch out: The panel never shows two values for score. If you expected a growing list like 0, 1, 2, that's the idea this program is here to break.

Computer Science · Reading an update

What does score = score + 1 actually do?

A program reaches the line score = score + 1. Just before this line runs, score holds 4.

Which is closest to what you think happens?
How sure are you?

Declaring vs initialising

Declaring a variablevsInitialising a variable

These two words get merged all the time. Read the insight on each row.

Focus

What you do

Declaring a variable

State the variable's name and what data type its value will use

Initialising a variable

Assign the variable its starting value

The insight

Declaring describes the box. Initialising puts the first value in it.

What gets stated

Declaring a variable

A name and a data type, for example: a variable called lives that will hold a whole number

Initialising a variable

A name and a starting value, for example: lives = 3

In Python

Declaring a variable

Not used. Python does not use variable declaration

Initialising a variable

Used. This is how a Python variable gets started: lives = 3

Easy mix-up

Declaring a variable

Calling lives = 3 a declaration

Initialising a variable

lives = 3 assigns a starting value, so it is initialising

Computer Science · Finding the bug

Which line causes the error?

This program is meant to keep track of a player's lives and points in a game. It causes an error. Which line is responsible?

A student's program — which line goes wrong?

Computer Science · Naming variables

Good name, or needs work?

Pick a name, then choose where it belongs.

Still to sort

Meaningful and follows the Python convention (0)

Says what it holds, all lowercase, with underscores between words if needed.

Where the line is: A name has to pass both tests. Meaningful but written with capitals still needs work, and so does lowercase but meaningless.

Needs improving (0)

Either doesn't say what it holds, or breaks the lowercase-with-underscores convention.

6 of 6 still to sort.

Each name below is for a variable in a Python game program. Judge it on two things: does it say what it holds, and does it follow the Python convention?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

A variable is a labelled spot in memory. Put a new value in and the old one is gone. By the end you'll be able to say exactly what a variable holds at any line of a program.

What you need to know

  • A variable is a named piece of data stored in the computer's memory.
  • A program can access the value in a variable and change it while it runs.
  • Before a program can use a variable, a memory location must be allocated for its value.
  • A variable holds one value at a time.
  • Updating a variable overwrites the previous value. In score = score + 1, the old score is replaced, not kept.
  • Have a goscore holds 7. Then the line score = score + 3 runs. What does score hold now, and what has happened to the 7?

    10. The 7 has been overwritten, so it's gone.

    7 + 3 is worked out and 10 is stored. A variable holds one value at a time, so it can't keep the 7 alongside the 10.

  • Declaring a variable means stating its name and the data type its value will use.
  • Initialising a variable means assigning its starting value, as in lives = 3. Python doesn't use declaration, only initialisation.
  • Have a goYour friend's Python program starts with age = 15, and they proudly announce that this line 'declares' age. Would you let them have that word?

    No. That line initialises age.

    age = 15 assigns a starting value, which is initialising. Declaring means stating a name and data type, and Python doesn't use declaration at all.

  • Using a variable before it has been initialised causes an error, because the variable doesn't exist yet.
  • Choose meaningful names, such as name rather than x, so code is easier to read and understand.
  • Python names are lowercase, with underscores between words if needed: age, first_name. Consistent naming makes code easier to modify and debug.
  • Have a goA variable stores a player's best time. Which name would you pick: bt, BestTime or best_time?

    best_time

    bt isn't meaningful, and BestTime breaks the lowercase convention. best_time says what it holds and joins the words with an underscore.

The big picture

A variable is a named piece of data in memory that a program can access and change, once a memory location has been allocated for its value. It holds one value at a time: in score = score + 1, the right-hand side is worked out and the result overwrites the old value. Declaring states a variable's name and data type; initialising assigns its starting value, and Python only initialises. Using a variable before initialising it causes an error. Meaningful, consistent names (lowercase with underscores in Python) make code easier to read and debug.

Key points

1A variable is a named piece of data in memory that a program can access and change; a memory location is allocated for its value before use.
2A variable holds one value at a time, and each update overwrites the previous value.
3Declaring states a variable's name and data type; initialising assigns its starting value. Python only initialises.
4Using a variable before initialising it causes an error, because the variable does not exist yet.
5Meaningful, consistent names make code easier to read, understand, modify and debug; Python names are lowercase with underscores between words.

Worked example

Problem

Trace this Python program line by line. Line 1: coins = 5. Line 2: cost = 2. Line 3: coins = coins - cost. Line 4: cost = cost + 1. Line 5: coins = coins - cost. Line 6: print(coins). What is printed, and what does each variable hold at the end?

⚠ Watch out

Reading = as 'equals', the way it works in maths. In a program, score = score + 1 is an instruction: work out score + 1 using the current value, then store the answer in score, overwriting the old value.

🧠

Memory hook

A variable is one labelled box. New value in, old value out, and the old one is gone for good.

✓

Check yourself

Cover the page. Explain the difference between declaring and initialising a variable, and say which one Python uses. Then write a one-line Python program that would cause an error, and explain why it does.

Flashcards

(13)
What is a variable?
A named piece of data stored in a computer's memory, which can be accessed and changed by a program.
Can a variable's value change while a program is running?
Yes. Its value can change during the execution of the program.
How many values can a variable hold at once?
One.
What happens to a variable's previous value when it is updated?
It is overwritten, so it is no longer stored.
In what order does score = score + 1 happen?
score + 1 is worked out first, using the current value; then the result is stored in score.
What must be allocated before a program can use a variable?
A memory location for the variable's value.
Declaring a variable means…
stating its name and what data type will be used for its value.
Initialising a variable means…
assigning its starting value.
Which does Python use: declaration, initialisation, or both?
Only initialisation. Python does not use variable declaration.
Why does using a variable before initialising it cause an error?
The program is trying to use a variable that does not yet exist.
Why give a variable a meaningful name, like name rather than x?
It makes the code easier to read and understand.
What does consistent naming make easier?
Modifying and debugging the code.
How should a Python variable name be written?
In lowercase, with an underscore separating words if required, e.g. first_name.

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.