📦
Coding🚀 Ages 7-10Beginner 6 min read

Variables: Boxes for Information

A primary-school coding lesson on variables: learn how a variable is a labeled box that stores information, how to change it, with Scratch and Python examples and a quiz.

Key takeaways

  • A variable is a named box that holds a piece of information
  • You can put a number, word, or other value inside it
  • The value can change while the program runs
  • You use the variable's name to read or change what is inside

A box with a name

A variable is like a labeled box. You put a piece of information inside, and you give the box a name so you can find it later.

Imagine a box with the word score written on the front. Inside, you keep a number. When you want that number, you just ask for score.

That's all a variable is: a name and a value stored together.

Making a variable

In Python, you make a variable by giving it a name and a value with an equals sign:

score = 0

This makes a box called score and puts the number 0 inside it.

We can put words inside too:

name = "Sam"

Now the box called name holds the word Sam. We write words inside quotation marks so the computer knows it's text.

Reading what's inside

To see what a variable holds, we use its name:

name = "Sam"
print(name)

This prints Sam, not the word "name". The computer looks inside the box and shows the value.

Changing the value

Here's the clever part: a variable can change. That's why it's called a variable — its value can vary.

score = 10
score = 25
print(score)

The first line puts 10 in the box. The second line replaces it with 25. So this prints 25. The box only keeps the newest value.

We can even use the old value to make a new one:

score = 10
score = score + 5
print(score)

This reads 10 from the box, adds 5, and puts 15 back. It prints 15. This is how a game adds points!

Variables in Scratch

Scratch has variables too. In the Variables block group, you click Make a Variable and give it a name, like score. Scratch then gives you blocks such as:

set score to 0
change score by 1

The set block puts a value in the box. The change block adds to it. If you put change score by 1 inside a loop, the score climbs higher each time the loop runs. You can show the variable on screen with a checkbox, so players watch their score go up.

Why variables matter

Variables let a program remember things: a score, a name, a number of lives, a high score. Without them, a program would forget everything the moment it happened.

Almost every program uses variables. They even help power artificial intelligence, where computers store and update huge numbers of values as they learn. If your code ever remembers the wrong value, that's a great time to practice debugging.

Quick quiz

Test yourself and earn XP

What is a variable?

After this code, what is inside score? ``` score = 10 score = 25 ```

What does this print? ``` name = "Sam" print(name) ```

Can a variable's value change while a program runs?

FAQ

Because its value can vary, or change. You can put a new value in the box any time, and the variable remembers the newest one.

Lots of things: a number like 10, a word like Sam, true or false, and more. Different kinds of values are called data types.