Variables

Names that point at values

Beginner 10 min

In this lesson

Variables let you store a value and reuse it by name. You’ll learn how assignment works, how to name variables well, and the single most common beginner mix-up around the equals sign.

Explain it like I’m 5

A variable is a name tag you stick onto a value. Instead of repeating the value everywhere, you refer to it by its name, and you can move the tag onto a different value whenever you like.

Assignment: name on the left, value on the right

You create a variable with a single =: age = 36 means ‘let the name age refer to the value 36’. The right side is worked out first, then the name is attached to the result. Read = as ‘gets’ or ‘is set to’, not as ‘equals’.

A variable is a name tag pointing at a value, not a box that holds it.
Example
first_name = "Ada"
birth_year = 1815
print(first_name, "was born in", birth_year)
Output
Ada was born in 1815
Two variables, then used together.

Reassignment, and ‘names point at values’

You can point a name at a new value any time: age = 37. A helpful mental model is that variables are names that point at values, not boxes that contain them. x = 5 then y = x makes both names point at the same 5; later changing x doesn’t change y.

Example
count = 0
count = count + 1
count = count + 1
print(count)
Output
2
Reassigning a variable using its own current value.

Create a variable for a city, print a sentence with it, then reassign the variable to a different city and print again.

city = "Lagos"
print("I live in", city)

Naming rules and good names

Names may use letters, digits, and underscores, but can’t start with a digit or contain spaces. They’re case-sensitive: score and Score are different. By convention, use lowercase words joined by underscores (final_score). A good name describes the value’s meaning, price beats p.

Example
score = 10
Score = 99
print(score, Score)
Output
10 99
Case matters: score and Score are two different variables.

Assigning several variables at once

Python lets you set up several variables in a single line. x, y = 1, 2 assigns both at once. You can give several names the same starting value by chaining: a = b = c = 0. And you can swap two variables with no temporary helper: a, b = b, a.

These tricks all rely on Python pairing up the names on the left with the values on the right, the same ‘packing and unpacking’ idea behind tuples, which has its own lesson.

Example
x, y = 1, 2
score = lives = 3
x, y = y, x

print(x, y)
print(score, lives)
Output
2 1
3 3
Multiple assignment, shared values, and a clean swap.

Common mistake: Using a variable before assigning it

Why it happens:

Python runs top to bottom, so a name must be created on an earlier line than the line that uses it. Otherwise you get a NameError.

How to fix it:

Make sure the assignment (total = 0) appears above the first line that uses total.

total = 0
print(total)

After x = 5 and then x = 9, what does print(x) show?

Which checks whether a equals b (rather than assigning)?

Mini exercise (easy)

Create two variables, price (a number) and item (a string), then print a line like Apple costs 3.

Your turn. Fill in the code below and press Run to test it right here, nothing to install.

item = ""      # a string, e.g. "Apple"
price = 0      # a number
print(item, "costs", price)

What to learn next

You learned that a variable is a name pointing at a value rather than a box that holds it, how to assign and reassign, the naming rules and good style, and the classic = versus == trap. You applied it by storing an item and a price and printing them together.

You’ve been creating values of different kinds, text and numbers, without naming them as such. Python Data Types Overview gives you the vocabulary: str, int, float, bool, list, dict, and None, plus how to check any value’s type.