Lesson 3 of 26 · about 12 min
Variables: names for values
A variable is a name tag stuck on a value so you can use it later. The = sign means "stick this name on that value", not "is equal to".
🧭 What you need first: Values, types and arithmetic
At a coat check, you hand over your coat and get a ticket that says “coat”. Later you say “I’d like coat, please” and get the right thing back. You never have to describe the coat again.
A variable works like that ticket. You tell Python: “remember this value, and call it price.” Later, whenever you write price, Python fetches the value.
And just like you can move a ticket to a different coat, you can point a name at a new value any time: price = 10 and later price = 12. The name stays; what it refers to changes.
Another way: sticky notes
Think of values as things on a table, and variables as sticky notes you put on them. age = 36 sticks a note saying “age” on the number 36. age = 37 peels the note off and sticks it on 37 instead.
Another way: contacts in your phone
You don’t memorize phone numbers. You save them under names like “Mom” and use the name. If Mom gets a new number, you update the contact, and every time you tap “Mom” you get the new one.
Watch names attach to values
Press Visualize, then Step ▶ one line at a time. Watch the Variables box. Pay close attention to line 3: the right side is worked out first, using the old value.
Watch the code run, one step at a time
- 1price = 10
- 2quantity = 3
- 3price = price + 2
- 4total = price * quantity
- 5quantity += 1
- 6print("Total:", total)
➜ yellow = the line about to run · ✓ = the line that just ran
Notice that total did not change when quantity became 4. total was calculated once, on line 4, and keeps that value. Variables don’t automatically update; they hold what you put in them.
Grown-up words
- A variable is a name that refers to a value.
=is the assignment operator.x = 5reads as “x gets 5” or “x becomes 5.” It is not a math equation. That’s whyx = x + 1makes sense in code but not in algebra.- Python works out the right side first, then attaches the name on the left.
- Shortcuts:
x += 1meansx = x + 1. Also-=,*=,/=.
Naming rules:
- Letters, digits and underscores only, and the name can’t start with a digit:
score2✅,2score❌. - Case matters:
Totalandtotalare different names. - Some words are reserved by Python (
if,for,class,True…) and can’t be names. - Style: Python programmers write
snake_case(total_price, notTotalPrice), and pick names that say what the value means.days_leftbeatsd.
Run a shopping cart
A notebook costs 4.50 and you buy 3. Set total to the price times the quantity. Then a 10% discount applies: reassign total to 90% of itself. Print the final total.
The swap puzzle
You have cup_a = "coffee" and cup_b = "tea". You want to swap them. Writing cup_a = cup_b first loses the coffee forever! The classic fix uses a third, temporary variable, like a spare cup.
🧩 Put these lines in order so the two cups swap drinks.
Use the arrows to put the lines in the right order, then check.
spare = cup_acup_b = sparecup_b = "tea"cup_a = cup_bcup_a = "coffee"print(cup_a, cup_b)
After these lines run: x = 5, then y = x, then x = 9. What is y?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?