One-sentence summary
A variable is a labelled box that lets us store a piece of information under a name, so we can look it up and change it later using that name.
Why does it matter?
A program often needs to remember things: how many points a player has collected, the distance a robot just measured, how many times it has turned. If we cannot store this information, the program has to start from scratch at every step.
Variables solve exactly this. We attach a piece of information to a name, then use that name to read or change it. Once we understand variables, our programs no longer just take steps; they keep track of what is happening and make decisions based on it.
What is a variable?
You can think of a variable as a box with a label on it. We put a piece of information inside the box and write a name on top. Later, when we say that name, the computer looks in the right box.
Suppose you are playing a game and want to track your score. You open a box called score and put 0 inside it. As you earn points, you make the number in that box grow.
Assigning a value
Putting information into a variable is called assignment. In pseudocode we can write it like this:
score ← 0
The arrow means "put the value on the right into the box on the left." In Python the same job is done with the = sign:
score = 0
Note: this = is not a statement of equality; it is an assignment. It is more accurate to read it as "put 0 into the score box" than as "score equals 0."
Updating a value
We can change what is inside a box whenever we like. The new value replaces the old one.
score ← 0
score ← score + 10
The second line says: "take the value in the score box, add 10, and put the result back into the score box." So score becomes 10.
Meaningful naming
We choose the name of a variable ourselves, but a good name helps a lot. The name should clearly describe what is inside the box.
| Weak name | Better name |
|---|---|
x | score |
a | distance |
n | try_count |
If you write x, you will not remember what it means when you look at your own code three days later. If you write distance, the name explains itself. Good names make code readable for others and for your future self.
Counter and accumulator
There are two very common patterns for variables. Both work together with loops.
Counter
A counter is a variable that keeps track of how many times something happens. It goes up by one each time.
counter ← 0
repeat 5 times
counter ← counter + 1
When the loop ends, counter is 5. In a robot this could count how many obstacles it saw or how many laps it made.
Accumulator
An accumulator is a variable that adds up and collects numbers. Unlike a counter, it adds a changing amount at each step instead of 1.
total ← 0
total ← total + 8
total ← total + 12
Here total becomes 0, then 8, then 20. Adding up the prices of items in a basket is a good example of this.
Simple types
We do not have to store only numbers in a variable. The three most common types are:
- Number: like
score = 0ordistance = 12.5. We can do calculations with these. - Text: like
name = "Ada". It is written in quotation marks and made of characters. - True/False: like
game_over = False. It can take only two values: true (True) or false (False).
The true/false type is especially useful for making decisions: it holds situations such as whether a robot's button was pressed or whether the game is over.
Everyday example: A score counter
Imagine a collecting game. Each star is worth 10 points. The player collects 3 stars. Let us keep the score in a variable.
score = 0
score = score + 10 # first star
score = score + 10 # second star
score = score + 10 # third star
print("Total score:", score)
This program prints Total score: 30. We can write the same thing more briefly with a loop:
score = 0
for star in range(3):
score = score + 10
print("Total score:", score)
Here score is an accumulator. Each time the loop runs, it grows the value in the box.
Robotics example: Storing a measured distance
Imagine a robot measuring the distance to the obstacle in front of it. The robot stores this measurement in a variable, then looks at it to make a decision.
distance = 25 # centimetre value read from the sensor
if distance < 10:
print("Too close, stop!")
else:
print("Path is clear, move on.")
When the sensor takes a new measurement, we update the distance variable; the program stays the same, but the decision is made with the new value. Thanks to the variable, the robot can remember what it "saw."
Mini practice
Add up the exam scores of three students in a class and find the average.
- Create an accumulator called
totalwith a starting value of 0. - Add three scores to the
totalvariable in turn (for example 70, 85, 100). - Create a new variable called
averageand dividetotalby 3. - Print the result to the screen.
After writing your own solution, try this: change the scores and see that the program updates the average correctly.
Common mistakes
Using a variable before defining it
Before you use a variable for the first time, you must assign a value to it. Looking into an empty box causes an error.
score = score + 10 # ERROR: score is not defined yet
The correct way is to create the box first by writing score = 0.
Confusing = with ==
In Python, = performs assignment (put into the box), while == asks whether two things are equal. These two are often mixed up.
score = 10 # assignment: put 10 into the score box
score == 10 # question: is score equal to 10?
When comparing values in a condition, we use ==. We will look at this in more detail in the next lesson.
Using the same name for different purposes
If you open a variable for the score and then use the same name for the distance, you will get confused and make mistakes. Give each box a single job; use different names for different information.
Lesson summary
- A variable is a box that stores information under a name and can be updated later.
- Assignment puts the value on the right into the variable on the left; in Python this is done with
=. - Meaningful names make code readable and understandable.
- A counter counts something one by one; an accumulator adds up changing amounts.
- Variables can hold different types of information, such as numbers, text and true/false.
Check questions
- What is a variable and what is it for?
- What does the line
score ← score + 10do? - What is the difference between a counter and an accumulator?
- What is the difference between
=and==in Python? - In the line
distance = 25, what type of information doesdistancehold?
Answers
- A variable is a box that stores information under a name. It lets us read and update that information later through the name.
- It takes the value in the
scorebox, adds 10, and puts the result back into thescorebox; in other words, it increases the score by 10. - A counter increases its value one by one at each step (it tracks how many times something happens). An accumulator adds a changing amount at each step to build up a total.
=performs assignment, that is, it puts a value into a variable.==asks whether two things are equal to each other (comparison).distanceholds a number (25). We can do calculations with this number, for example compare whether it is less than 10.
Source and verification note
For “Variables: Storing Information”, verification focuses on whether the relationship between What is a variable? and Updating a value remains consistent across examples. The algorithms in this lesson are checked by tracing sample inputs by hand and comparing them with expected outputs. Pseudocode is used to make the reasoning sequence visible without tying it to one programming language.
Next lesson
Operators and Comparisons: We learn ways to add, subtract and compare the values stored in variables.