Skip to content
Crayon CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

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

The story

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.

Play with it

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

  1. 1price = 10
  2. 2quantity = 3
  3. 3price = price + 2
  4. 4total = price * quantity
  5. 5quantity += 1
  6. 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.

Name it

Grown-up words

  • A variable is a name that refers to a value.
  • = is the assignment operator. x = 5 reads as “x gets 5” or “x becomes 5.” It is not a math equation. That’s why x = x + 1 makes sense in code but not in algebra.
  • Python works out the right side first, then attaches the name on the left.
  • Shortcuts: x += 1 means x = x + 1. Also -=, *=, /=.

Naming rules:

  • Letters, digits and underscores only, and the name can’t start with a digit: score2 ✅, 2score ❌.
  • Case matters: Total and total are 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, not TotalPrice), and pick names that say what the value means. days_left beats d.
Build it

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.

Challenge

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.

  1. spare = cup_a
  2. cup_b = spare
  3. cup_b = "tea"
  4. cup_a = cup_b
  5. cup_a = "coffee"
  6. print(cup_a, cup_b)
Remember it

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?