Variables — Storing Information

How to create, update, and use labelled storage boxes inside your computer's memory.

Imagine playing a game where your score never updates, or your character forgets their own name. Computers use virtual 'storage boxes' called variables to remember everything from high scores to player names. Let's learn how to create and control them!

Words to own

Variable
A named container in computer memory that stores a piece of information that can change.
Assignment (=)
The symbol used to store or assign a value into a variable, acting from right to left.
Data Type
A category that tells Python what kind of value a variable is holding, like text or a number.
String
A data type used for text, always wrapped in quotation marks.
Integer
A data type used for whole numbers without any decimal points.
Float
A data type used for decimal numbers that have a fractional part.
Overwrite
Replacing an old value inside a variable with a brand new one, erasing the original value.

What is a Variable?

Imagine a labelled storage box. You write a name on the outside, like 'Toys' or 'Books', and you can put something inside, take it out, or swap it for something else later. The label stays the same, but what is inside can change.

A variable in programming works exactly like this. It is a named container in the computer's memory that holds a piece of information. Because the information inside can change (or vary), we call them variables.

Valid vs. Invalid Variable Names
PointValid Variable Name (Python is happy)Invalid Variable Name (Python crashes)
Using spacesplayer_score = 10player score = 10
Starting with numberscats3 = 33cats = 3
Special symbolsitem_cost = 5.00item-cost$ = 5.00
Case matchingscore = 5 print(score)score = 5 print(Score)

Storing Your First Values

In Python, we use the equals sign (=) to create a variable and put a value inside it. This is called assignment.

Be careful: in computer science, this symbol does not mean 'equal to' like in maths. Instead, it means 'take the value on the right and store it into the box on the left'.

Creating variables and printing them
name = "Maya"
age = 12
print(name)
print(age)
Output
Maya
12

Strings, Integers, and Floats

Notice that "Maya" has quotation marks around it, but 12 does not. This is because Python uses different data types for different kinds of information.

If you want to store text, you must use quotation marks to make it a String. If you want to store a whole number, write it without quotes to make it an Integer. For decimal numbers, write them without quotes to make them a Float.

Three common data types in action
hero_name = "Aroha"
health_points = 100
height_meters = 1.52
print(hero_name)
print(health_points)
print(height_meters)
Output
Aroha
100
1.52

Why We Call Them Variables

Variables are highly useful because their stored value can change while the program is running. This is called overwriting.

When you assign a new value to an existing variable, Python throws away the old value and replaces it with the new one. A variable can only hold one value at a time.

How variables get overwritten
score = 0
print(score)

score = 10
print(score)
Output
0
10

Rules for Variable Names

Python has strict rules for naming your boxes. Variable names cannot start with a number, and they cannot contain spaces. If you use a space, Python gets confused and thinks you are writing two separate commands.

To make multi-word variables readable, programmers use underscores instead of spaces, or capitalise the first letter of each new word.

Your turn — run real Python

Cat Years Calculator

Modify the starter code to calculate your age in cat years. Cat years are calculated by multiplying your human age by 15. Create a new variable called cat_years, calculate the value, and print it alongside your name.

1
2
3
4
5
6

Bug hunt

What is wrong with this code?

my score = 100
print(my score)

Myth-busting corner

  • MythThe equals sign (=) means mathematical equality in Python.

    TruthIn Python, = is the assignment operator. It means 'evaluate what is on the right side, and save it into the variable name on the left side'.

  • MythVariables can hold multiple independent values at the same time.

    TruthA standard variable can only hold one single value. Storing a new value inside it automatically erases and replaces the previous value.

  • MythPutting quotation marks around a variable name prints its stored value.

    TruthWriting print("score") will output the literal word 'score'. To print the value inside the variable, you must write print(score) without quotation marks.

Exam answer that scores full marks

Explain what happens, step by step, when this code runs: score = 5 score = score + 3 print(score)

First, Python creates a variable named 'score' and stores the integer value 5 inside it. Second, the line 'score = score + 3' tells Python to evaluate the right-hand side first by retrieving the current value of 'score' (which is 5) and adding 3 to it, resulting in 8. This new value of 8 is then stored back into the 'score' variable, overwriting the old value of 5. Finally, the print(score) statement looks up the current value inside the 'score' box and outputs 8 to the screen.

Why it scores

  • Correctly identifies the initial variable creation and assignment.
  • Explains that Python evaluates the right side of the assignment operator first.
  • Identifies that the old value is overwritten and replaced by the new calculation.
  • Correctly states the final output of the print statement is 8.