Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Variable

A named container in a program that stores a value, which can be read, updated, or replaced.


Quick Answer

A named container in a program that stores a value, which can be read, updated, or replaced. Use variables to remember anything a program needs later: a score, a name, a running total, whether the game is over. The name stays the same while the value inside can change: after score = 0 and then score = score + 10, score holds 10. Reading a variable copies its current value; assigning to it throws the old value away.

Why This Matters

Without variables a program could not remember anything from one line to the next: no score that grows, no total being added up, no player name to greet. Knowing that a variable holds exactly one current value, and that assignment replaces it, lets you trace code accurately and avoid errors such as reading a variable before it has been given a value.

Intuitive Explanation

Imagine the computer's memory as a shelf of labelled boxes. The statement score = 0 finds the box labelled score, or makes one, and puts 0 inside. score = score + 10 is read right side first: look in the box, find 0, add 10, then put the 10 back into the same box. The old 0 is gone. The label never changes; the contents can change as often as the program likes.

Copying is different from linking. After a = 5 and b = a, box b gets its own copy of 5. If you then set a = 7, b still holds 5, because b = a copied the value at that moment and made no lasting connection. A trace table follows this easily: one column per variable, updated every time something is assigned.

The = sign in code is not the = sign in maths. In algebra, x = x + 1 has no solution. In a program it is an instruction: compute x + 1, then store the result in x. A maths equation states a fact that is always true; an assignment is an action that happens once, at one moment, and a later line can undo it.

Think of it like this. A variable is like a whiteboard with a name written across the top. You can read what is on it, or wipe it and write something new, but there is room for only one thing at a time. Writing a new value erases the old one for good, unless you first copied it onto another whiteboard.

Core idea. Variables give meaningful names to data values so programs can store, retrieve, and modify them.


When to Use

Use variables when a program must store a value to use later, update it (a score, a counter, a running total), or give something a name so the code is readable. When tracing, keep one column per variable and update it at every assignment. If the question is about what kind of value is stored (number, text, true or false) and which operations it allows, use Data Types; if it is about the order lines run in, use Sequence.

Tip. Ask: Does the program need to remember a value under a name and change it later?

How to Recognize It

Before reaching for a variable, check whether some value has to be kept and used again as the program runs.

  1. Does the program need a value after the line where it is first worked out?

    Yes means store it in a variable. If the value is used once, right where it is made, you may not need to name it at all.

  2. Does the value change while the program runs, like a score or a timer?

    Yes is the classic job of a variable. A value that never changes, such as the number of days in a week, is often stored as a constant instead.

  3. Are there many values of the same kind, like 30 test scores?

    Yes points to an Array, which keeps many values under one name. A variable holds one value at a time.

  4. Is the question about the act of putting a value into the box with =?

    Yes points to Assignment, the operation that stores a value. The variable is the named box that receives it.

Variable vs Assignment vs Data Types vs Array

Whenever you write score = 0, all four ideas are present at once, so they are hard to separate. Ask whether you mean the named box, the act of filling it, the kind of value inside, or a row of many boxes.

Variable

Meaning
A variable is a named place in memory that holds one value, which the program can read or replace.
Key test
One name, one current value, which can change over time.
Example
lives starts at 3 and drops to 2 when the player is hit.

Assignment

Meaning
Assignment is the operation that works out the right-hand side and stores the result in the variable on the left.
Key test
It is the action of storing, written with = or an arrow.
Formula
v←e
Example
total = price * 2 works out price * 2 first, then stores it in total.

Data Types

Meaning
A data type says what kind of value is stored, such as a number, text or true/false, and what you can do with it.
Key test
Asks what kind the value is, not where it is kept.
Example
age = 12 holds an integer, while name = 'Ana' holds a string.

Array

Meaning
An array keeps many values in order under one name, each reached by its position number.
Key test
Many values, one name, picked out by index.
Example
temps = [18, 21, 19], so temps[1] is 21.

Formula & Notation

Worked Examples

Example 1 — Trace two variables

Easy

What are the values of x and y at the end? x = 4; y = x + 1; x = x * 3; y = y + x
Show the steps and the answer
  1. x = 4.

    The box x now holds 4.

  2. y = x + 1 = 4 + 1 = 5.

    Reading x copies its current value, 4, and leaves x unchanged.

  3. x = x * 3 = 4 * 3 = 12.

    The right side uses the old x; then the result replaces it, so the 4 is gone.

  4. y = y + x = 5 + 12 = 17.

    Both boxes are read at this moment: y holds 5 and x now holds 12.

Answer: x = 12, y = 17

Takeaway: Work out the right side with the current values, then overwrite the box named on the left.

Example 2 — A copy, not a link

Standard

What does this program print? a = 5; b = a; a = 7; print(b)
Show the steps and the answer
  1. a = 5, then b = a copies 5 into b.

    Assignment copies the value that a holds at that moment.

  2. a = 7 replaces the value in a only.

    b = a made no lasting link, so b keeps its own copy.

  3. print(b) shows 5.

    Nothing has been assigned to b since it received 5.

Answer: 5

Takeaway: b = a copies a value once; it is not a rule that keeps b equal to a, the way a spreadsheet formula would.

Example 3 — Find the mistake: = inside a condition

Application

Kofi wants to check whether a player's lives have run out. He writes: lives = 3; if lives = 0: print('Game over'). Find the mistake and correct it.
Show the steps and the answer
  1. Read lives = 0 inside the if: a single = is assignment.

    It means 'put 0 into lives', not 'is lives equal to 0?'.

  2. Trace it in a language like C: lives becomes 0, and the condition's value is 0, which counts as false.

    'Game over' never prints, and the player's 3 lives have been wiped out by the check itself. Python refuses to run the line at all.

  3. Fix: if lives == 0: print('Game over').

    == compares the stored value with 0 and leaves lives unchanged.

  4. Trace the fix: lives is 3, 3 == 0 is false, nothing prints, and lives is still 3.

    The check now reads the variable without changing it.

Answer: Use == in the condition: if lives == 0. With lives = 3, nothing prints and lives stays 3.

Takeaway: One = stores a value; == compares two values. A condition should compare, never store.

Where You'll See It

  • The score box in a video game

    The game keeps your score in a named container and updates it every time you earn points, which is exactly what a variable does: store a value you can read and change.

  • A labeled lunchbox

    The box has a name (yours) and holds whatever you put in today; tomorrow you can swap the contents while the label stays the same, like a variable holding different values over time.

  • A scoreboard at a basketball game

    'Home' and 'Away' are named slots whose numbers get updated as the game goes, just like variables tracking state.

Common Mistakes

  1. The slip: I can use a variable before I've given it a value.

    Why it's tempting: You've already written the name, so it feels like the box must hold something.

    Instead: Reading a variable before assigning it gives an undefined or null value (or an error); always set a value before you use it.

  2. The slip: x = 5 means x is permanently 5, the way it does in a math equation.

    Why it's tempting: In math, x = 5 is a fixed fact, so it feels like the value can't change.

    Instead: In programming = is assignment: it puts 5 into the box now, but a later line can replace the contents; variables are designed to change.

  3. The slip: Short names like x or temp are just as good as descriptive ones.

    Why it's tempting: Short names are faster to type and you know what they mean right now.

    Instead: Vague names make code hard to understand later; choose names like score or totalPrice that say what the value represents.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. Trace: `coins = 10`, then `coins = coins + 5`, then `coins = coins - 3`. What is `coins` now?

    Hint: Each line reads the current value, then stores a new one.

  2. Trace: `name = "Ana"`, then `greeting = "Hi " + name`, then `name = "Ben"`. What are `name` and `greeting` at the end?

    Hint: What was `name` at the moment `greeting` was made?

  3. A game starts with `lives = 3`. The player loses a life twice, then finds one bonus life. Write the three update lines and give the final value.

    Hint: Each update should read `lives` and store a new value in `lives`.

  4. Trace: `a = 4`, `b = 6`, `a = a + b`, `b = a - b`, `a = a - b`. What are `a` and `b` at the end?

    Hint: Write both variables after every line.

  5. In the line `if x == 5:`, does `x == 5` change the variable `x`, or check it?

    Hint: Compare one equals sign with two.

  6. A program's first line is `total = total + 4`, and it crashes. Explain why, and fix it.

    Hint: What value does `total` have when this line tries to read it?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why does x = x + 1 make sense in code but not in maths?

In maths, x = x + 1 would say a number equals itself plus one, which is impossible. In code, = is an instruction, not a statement of fact: work out the right side using the current value of x, then store the result back in x. If x was 4, the right side gives 5, and x now holds 5. Some books write it as x←x+1x \leftarrow x + 1 to make this clearer.

Can I use a variable made inside a function anywhere in my program?

Not always. A variable created inside a function usually exists only while that function runs, and only code inside the function can see it; where a name can be seen is called its scope. A variable created at the top level of a program can be read from most places. Keeping variables inside the functions that need them stops one part of a program from changing another part's values by accident.

Are there rules for naming a variable?

Yes, and they differ a little between languages. In most, a name can use letters, digits and underscores, cannot start with a digit, and cannot contain spaces, so total_score works but 2score and total score do not. Names are usually case-sensitive, so Score and score are two different variables. Words the language already uses, such as if or while, are not allowed.

Where does a variable actually live?

In the computer's memory, its RAM. The name is for you; the computer links it to a numbered spot in memory and reads or writes there. When the program ends, that memory is handed back for other programs to use, which is why variables lose their values when you close a program unless you save them to a file first.

Learning Path

← Before

No prerequisites
Variable

You are here

You can begin with variables before any other programming lesson, as long as you know that a program runs one line at a time. A variable gives a value a name so the program can store it, read it, and change it. Next, Data Types explains the kinds of values a variable can hold, and Assignment looks closely at how a value is stored and replaced, including statements like count = count + 1.

See Also