📦 Variables: Boxes for Information

4th Grade 💻 Coding

In a game there’s a score that goes up, a number of lives that goes down, and a player’s name. Where does the computer keep all that? In variables. A variable is like a box with a label: there’s a value inside, and you can peek at it, swap it out or use it in a calculation. In this lesson we build some boxes and meet the kinds of information you can put in them.

Free · US Letter · no sign-up
Google Classroom Save to Pinterest Email me

A box with a name

Imagine a shoebox with “Score” written on it in marker, and inside it a note with the number 0. Every time the player collects a coin, you open the box, read the number, add 1 and swap in a new note. That’s exactly what a variable is: a name (the label) and a value (what’s inside). The name stays the same, but the value can change — which is why it’s called a “variable.”

In Python you create a variable in one line: score = 0. The equals sign here doesn’t mean “equals” like in math; it means “put into”: put 0 into the box called score. This action is called assignment. To add a point, you write score = score + 1 — the computer reads the old value, adds 1 and puts the result back. In Scratch you create a variable with the “Make a Variable” button and use the blocks “set score to 0” and “change score by 1.”

Kinds of information

Not every box holds the same kind of thing. There are whole numbers, like 7 or -3. There are decimal numbers, like 2.5. There’s text — a letter, a word or a sentence — which in programming is called a string, because it’s a “string” of characters, like beads on a thread. And there are Boolean values, which can only be true or false. Each of these is called a data type, and the computer treats each type differently.

Here’s the difference in action: print(5 + 5) shows 10, because those are two numbers. But print("5" + "5") shows 55! The quotation marks turn 5 into text, and “adding” pieces of text means gluing them together. That’s surprising at first, and it’s also a common cause of bugs: a number the user typed arrives in the program as text, and you need to convert it to a number before you can calculate with it.

How to choose a good name

The computer doesn’t care what you call a variable — x, a1 or banana. But the people reading the code, including you a week from now, care a lot. A name like score, lives or player_name tells you right away what’s in the box. A name like x makes you guess. Experienced programmers say code is read many more times than it’s written, so good names are worth the effort.

There are also a few technical rules. In Python, a variable name can’t start with a digit, can’t contain a space (use an underscore instead), and there are words reserved for the language itself — like if, for and while — that you can’t use as names. One more important thing: Python tells uppercase and lowercase letters apart, so Score and score are two completely different variables.

Variables everywhere

Almost every program keeps track of lots of variables. In a game: the score, the lives, the level, the character’s position (x and y), its speed, and whether it’s jumping right now (true or false). In a weather app: the temperature, the name of the city and whether rain is expected. At any moment, the values in the variables describe the “state” of the program.

When you want to store lots of values of the same kind — say, the names of all the students in a class — you don’t create thirty variables. You use a list: one variable that holds many values in order. We’ll meet lists in the Python lesson. Meanwhile, try to think: what variables would you need to build a “Catch the Apple” game?

Key facts

  • A variable is a named spot in memory that stores a value you can change.
  • In Python, score = 0 means “put 0 into the variable score” — an action called assignment.
  • Common data types: whole number, decimal number, string (text) and Boolean.
  • print("5" + "5") shows 55, because adding pieces of text glues them together.
  • In Python, score and Score are two different variables.

Quick quiz

  1. What is a variable?

  2. What does the line score = 0 mean in Python?

  3. If score was 4, what happens after score = score + 1?

  4. What does print("5" + "5") show?

  5. In programming, what is text like "Hello" called?

  6. Which variable name is not allowed in Python?

  7. Why is it better to choose a name like lives instead of x?

  8. Where would you store the names of all the students in a class?

💭 Think and talk about it

  1. What variables would you need to build a “Catch the Apple” game? Give each one a good name.
  2. Why do you think a computer treats the number 5 and the text "5" differently?
  3. What is the “state” of a game you like? Which values change in it every moment?

Questions kids ask

Why is it called a variable?

Because the value inside it can vary — change — while the program runs: the score goes up, the lives go down. The variable’s name stays the same; only what’s inside changes.

What’s the difference between a variable in math and a variable in programming?

In math, x stands for an unknown number in an equation. In programming, a variable is a box in memory with a known value that you can replace. That’s why score = score + 1 makes sense in programming, even though in math it’s an equation with no solution.

What is a data type?

The kind of value: whole number, decimal number, string (text), Boolean (true or false), list and more. The type decides what you can do with the value — for example, you can divide numbers, but not words.

🔎 Ask Thinkapedia another question