Why variables trip people up
Variables are the first idea in programming where you have to picture something you can't see. Most tutorials show x = 5, say "x is now 5" and move on. Then the beginner sees x = x + 1 and thinks the computer has broken maths.
This post explains variables the way we teach them in Python Made Simple: an everyday picture first, then the real Python terms, then code you can run. You need Python installed and any editor. We use the free Thonny editor because it is made for beginners.
Picture a labelled box
When you move house, you write a label on every box: Kitchen, Books, Winter coats. Weeks later you don't open every box to find a cookery book. You look for the one labelled Books.
A variable works the same way. It has two halves:
- a name: the label on the outside, such as
customer_name - a value: the information it holds, such as
"Priya Shah"
Why bother? Imagine greeting a customer by name in ten places in a program. Without a variable you type the name ten times. With one, you type it once, and changing it in one place changes it everywhere.
Your first variable
Type this into a new file and run it:
customer_name = "Priya Shah"
print(customer_name)
Output:
Priya Shah
The first line is an assignment statement. The = sign is the assignment operator, and Python always carries it out in two steps:
- Work out the value on the right.
- Attach the name on the left to that value.
The most important thing to unlearn from school maths: in Python, a single = does not mean "equals". It is an instruction. Read the line aloud as "customer_name is set to Priya Shah".
The label or the text?
Quotation marks change everything:
customer_name = "Priya Shah"
print("customer_name")
print(customer_name)
Output:
customer_name
Priya Shah
Rule of thumb: quotation marks mean these exact characters. No quotation marks means the value attached to this name.
Several variables together
shop_name = "Corner Bakery"
customer_name = "Priya Shah"
number_of_items = 3
print("Welcome to", shop_name)
print("Customer:", customer_name)
print("Items in basket:", number_of_items)
Output:
Welcome to Corner Bakery
Customer: Priya Shah
Items in basket: 3
Text goes in quotation marks; the number 3 doesn't. The comma inside print() separates the items, and Python adds one space between them for you.
Changing a value
It's called a variable because its value can vary. Assign a new value to the same name and the old one is replaced:
favourite_drink = "tea"
print("My favourite drink is", favourite_drink)
favourite_drink = "coffee"
print("My favourite drink is now", favourite_drink)
Output:
My favourite drink is tea
My favourite drink is now coffee
The line that looks impossible
Now the one that confuses everyone:
number_of_items = 3
number_of_items = number_of_items + 2
print("Items in basket:", number_of_items)
Output:
Items in basket: 5
In maths, "number = number + 2" can never be true. In Python it makes perfect sense, because = is the same two-step instruction as before:
- Work out the right-hand side:
number_of_items + 2is3 + 2, which is5. - Attach the name on the left to the result:
number_of_itemsnow refers to5.
This is how every program keeps a running total: a score going up, a basket filling, a balance changing.
Python also has a shortcut for it. score += 5 means exactly the same as score = score + 5, and there are matching versions for subtracting (-=), multiplying (*=) and dividing (/=).
Naming your variables
Python is strict about names. A variable name:
- can contain letters, digits (0–9) and the underscore
_ - cannot start with a digit:
item_2is fine,2nd_itemis not - cannot contain spaces or symbols such as
-,!or? - cannot be a keyword Python already uses, such as
if,for,class,TrueorNone - is case-sensitive:
total_priceandTotal_priceare different names
A name can follow every rule and still be unhelpful:
| Valid but unclear | Clear and meaningful |
|---|---|
x |
number_of_items |
tp |
total_price |
thing |
delivery_address |
Python's official style guide, PEP 8, recommends lower-case words joined by underscores, a style called snake_case. Pick names that say what the value means; future you will thank you.
Three mistakes everyone makes (and the fixes)
Error messages are clues, not criticism. Read the last line first; it usually tells you the most.
1. A typo in the name
customer_name = "Priya Shah"
print(custmer_name)
NameError: name 'custmer_name' is not defined. Did you mean: 'customer_name'?
Python matches names letter for letter, capitals included, so custmer_name is a brand-new name it has never seen. Fix: take Python's suggestion and correct the spelling.
2. Using a variable before creating it
print(delivery_address)
delivery_address = "12 Station Road"
NameError: name 'delivery_address' is not defined
Python runs top to bottom, so at line 1 the name doesn't exist yet. Fix: assign first, use second.
3. A space in the name
customer name = "Priya Shah"
SyntaxError: invalid syntax
Python sees two separate words. Hyphens (first-name) and leading digits (2nd_place) fail too. Fix: customer_name.
Try it yourself
Predict the answer before you run this. Start with 20 coins, spend 7, then double what's left:
coins = 20
coins -= 7
coins *= 2
print(coins)
Write your prediction in the comments, then run it. (The answer is at the very end of this post.)
Bonus challenge: create two variables, morning_drink = "coffee" and evening_drink = "hot chocolate", then write code that swaps them so the morning drink is hot chocolate and the evening drink is coffee. It's trickier than it looks. Share your solution below.
What's next
You now know what a variable is, how = really works, how to change a value and how to name things well. That's the foundation of every program you'll ever write.
Next in this series: why 3 + 4 gives 7 but "3" + "4" gives "34", which is all about data types.
This post is adapted from Chapter 5 of Python Made Simple: A Visual, Hands-On Guide to Programming for Complete Beginners, a full-colour book where every idea gets a plain-English explanation and a labelled diagram on the facing page. It goes from your very first program to building a calculator, a quiz game and an expense tracker. Every example was tested on Python 3.15 and 3.14.
👉 Read the free sample on Leanpub
Answer to the coins puzzle: 26. Starting at 20, spending 7 leaves 13, and doubling 13 gives 26.
Top comments (0)