Variables
Names that point at values
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’.
first_name = "Ada"
birth_year = 1815
print(first_name, "was born in", birth_year)
Ada was born in 1815
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.
count = 0
count = count + 1
count = count + 1
print(count)
2
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)
Assign once, print, then assign a new value to the same name and print again.
city = "Lagos"
print("I live in", city)
city = "Helsinki"
print("Now I live in", city)
I live in Lagos
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.
score = 10
Score = 99
print(score, Score)
10 99
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.
x, y = 1, 2
score = lives = 3
x, y = y, x
print(x, y)
print(score, lives)
2 1 3 3
Common mistake: Using a variable before assigning it
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.
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?
Reassignment replaces what the name points at. The latest value, 9, wins.
Which checks whether a equals b (rather than assigning)?
Double equals == compares; single = assigns.
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)
Assign both, then print(item, "costs", price).
item = "Apple"
price = 3
print(item, "costs", price)
assert isinstance(item, str) and isinstance(price, (int, float)), "item should be text and price a number"
assert item and price, "give item and price real values"
print("✓ Looks good!")