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 = 02 score = score + 13 score = score + 14 score = score + 105 print(score)
Variables
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.
Use Next and Back to move one line at a time. The Variables panel shows the value stored in score after each line runs.
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.
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
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?
Can a variable's value change while a program is running?
How many values can a variable hold at once?
What happens to a variable's previous value when it is updated?
In what order does score = score + 1 happen?
What must be allocated before a program can use a variable?
Declaring a variable means…
Initialising a variable means…
Which does Python use: declaration, initialisation, or both?
Why does using a variable before initialising it cause an error?
Why give a variable a meaningful name, like name rather than x?
What does consistent naming make easier?
How should a Python variable name be written?
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
- Data type: Real
- File handling: open, read, write, close
- Inputs, outputs, assignments
- Iteration (count- and condition-controlled loops)
- Local and global variables
- Operators (arithmetic and Boolean)
- Random number generation
- Records
- Sequence
- SQL: SELECT, FROM, WHERE
- Sub programs: functions
- Sub programs: procedures
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.