Home · Academy · Robotics & Coding · Algorithms · Variables: Storing Information

Variables: Storing Information

Learn to store and update information with named variables, including counters and accumulators.

LESSON COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

Operators and Comparisons: We learn ways to add, subtract and compare the values stored in variables.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration25–35 min
PrerequisiteLoops
ContentStandard lesson · 1,384 words
Last updated

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.

Meaningful naming table
Weak nameBetter name
xscore
adistance
ntry_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:

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.

  1. Create an accumulator called total with a starting value of 0.
  2. Add three scores to the total variable in turn (for example 70, 85, 100).
  3. Create a new variable called average and divide total by 3.
  4. 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

Check questions

  1. What is a variable and what is it for?
  2. What does the line score ← score + 10 do?
  3. What is the difference between a counter and an accumulator?
  4. What is the difference between = and == in Python?
  5. In the line distance = 25, what type of information does distance hold?

Answers

  1. A variable is a box that stores information under a name. It lets us read and update that information later through the name.
  2. It takes the value in the score box, adds 10, and puts the result back into the score box; in other words, it increases the score by 10.
  3. 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.
  4. = performs assignment, that is, it puts a value into a variable. == asks whether two things are equal to each other (comparison).
  5. distance holds 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.

Start QuizBack to Algorithms
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.