GCSE · Computer Science · Edexcel · Spec 1CP2

Variables

A program says name is 'Sam', then later 'Andeep', then displays name. Which one appears? Guess, then watch it run.

Computer Science · Algorithms

Trace table — dry run the code

Before you press Next, guess what this program will display. Then step through it one line at a time and check.

1name = "Sam"
2name = "Andeep"
3print(name)
4score = 0
5score = score + 1
6print(score)
name
score
Output
Press Start to run the first line.
·

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

Computer Science · Variables

Declaring or initialising?

Three languages, three lines, all about a variable called age. For each line, tick every property it has, then check.

Dim age As Integer
int age
age = 22

Computer Science · Variables

Find the line that breaks the program

This program is meant to greet a player and then show their points. One line makes Python stop with an error. Which line is it?

A student's program — which line goes wrong?

Computer Science · Variables

Which names are worth using?

Imagine each name is a variable in a Python program. Sort each one by what it tells a reader and whether it follows Python's convention.

Still to sort

Meaningful and follows Python's convention (0)

Shows what it holds, in lowercase, with underscores between words if needed.

Where the line is: Both tests pass: the name tells the reader what the variable holds, and it is written lowercase with underscores.

Meaningful but breaks the convention (0)

You can tell what it holds, but the style is not lowercase with underscores.

Where the line is: The name passes the first test (it tells you what it holds) but fails the second (Python's convention).

Not meaningful (0)

A reader cannot tell what it holds.

Where the line is: Fails the first test, so the style barely matters: a reader still has no idea what the variable holds.

6 of 6 still to sort.

Exam line: Meaningful names show what a variable holds. A naming convention is a standard for naming things in a chosen language, and following it makes code easier to read, modify and debug.

Computer Science · Variables

What is a variable, really?

A short program runs two lines: lives = 3, and then lives = 2.

Which of these is closest to what you think a variable is?
How sure are you?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Named boxes of data that a program fills, reads and overwrites, one line at a time

What you need to know

  • A variable is a named piece of data stored in a computer's memory. A program can access it and change it.
  • A variable holds one value at a time. Give it a new value and the old one is overwritten.
  • Have a goYour friend swears: 'I wrote mood = "happy", then mood = "tired", so mood is holding both.' Is your friend right?

    No. mood holds only "tired".

    A variable holds one value at a time, so the new assignment overwrote "happy".

  • score = score + 1 means: take the value score holds now, add 1, and store the result back in score.
  • Have a goA variable called coins holds 7. What does it hold after the line coins = coins + 1?

    8

    The right side is worked out from what coins holds now (7), then the result is stored back, overwriting the old value.

  • A variable needs a memory location and a starting value before the program can use it.
  • Declaring a variable means stating its name and data type. Initialising it means giving it a starting value.
  • Python only initialises. Languages such as Visual Basic and C declare the data type as well.
  • Use a variable before it has a value and you get an error. In Python that is a NameError.
  • Have a goPredict it: line 1 is print(age) and line 2 is age = 22. What happens when the program runs?

    An error: Python raises a NameError.

    Line 1 uses age before any line has given it a value, so there is nothing for it to display.

  • Code runs line by line, in the order written, so a variable holds the last value assigned to it.
  • Meaningful names show what a variable holds. user_age is helpful; x, y or z are not.
  • A naming convention is a standard for names in a language. Python uses lowercase with underscores, like first_name.

The big picture

A variable is a named piece of data in memory. It holds one value at a time, and a new assignment overwrites the old value. Programs run line by line, so a variable must be given a value before the line that uses it. Good variable names say what they hold and follow the language's naming convention.

Key points

1A variable is a named piece of data in memory that a program can access and change.
2It holds one value at a time; a new assignment overwrites the previous value.
3A variable must be given a value before the line that uses it, or an error occurs (a NameError in Python).
4Code runs line by line in the order written, so a variable holds the last value assigned to it.
5Declaring states the name and data type; initialising gives a starting value; Python only initialises.
6Meaningful names that follow the language's convention make code easier to read, modify and debug.

Worked example

Problem

What does this program display? level = 1, then level = level + 1, then print(level), then level = 5, then print(level).

⚠ Watch out

Thinking a variable keeps every value it has been given, or that you can use it before it has one. Each new assignment overwrites the old value, and a variable must be initialised before the line that uses it, otherwise Python raises a NameError.

🧠

Memory hook

One name, one box, one value. A new value goes in, the old value goes out. (The box is just a picture: the real thing is a memory location holding a named piece of data.)

✓

Check yourself

A program sets rank = 4, then rank = 1, then displays rank. What appears, and why? Use the words 'one value' and 'overwritten'.

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 computer program.
How many values can a variable hold at once?
One. When it is updated, the previous value is overwritten.
A variable called price holds 5. The program then runs price = 8. What does price hold?
8. The new value overwrites the 5.
In n = n + 1, what is worked out first?
The right-hand side, using the value n holds now. The result is then stored back in n.
What must a variable have before a program can use it?
A memory location and a starting value.
What does declaring a variable mean?
Stating its name and what data type will be used for its value.
What does initialising a variable mean?
Assigning it a starting value.
Does Python declare variables?
No. Python only initialises (assigns). Other languages declare the data type, such as Visual Basic and C.
What happens if a program uses a variable before it has been initialised?
An error occurs. In Python it is a NameError saying the name is not defined.
Why does the order of lines in a program matter?
Code runs line by line in the order written, so a variable must be assigned before the line that uses it.
Which value does a variable hold at any moment?
The last value assigned to it.
Why give variables meaningful names?
A name that shows what the variable holds is clearer: user_age is helpful, x, y or z are not.
What is a naming convention, and what is Python's for variables?
A standard for naming things in a chosen language. In Python, variable names are lowercase with an underscore separating words if required, for example age and 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 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.