Lesson 2 of 912 min2 challenges

Variables: Labels on Boxes

The equals sign is not equality — it is an order to store something. Get that one idea and the rest follows.

A variable is a labeled box

You have a piece of information you'll need more than once. Typing it out every time is tedious and easy to get wrong. So you put it in a box and stick a label on the box. From then on, you say the label.

name = "Maya"
age = 18

print(name)
print(age)
print(name, age)

Output:

Maya
18
Maya 18

Read name = "Maya" out loud as "put the text Maya into the box called name." Not "name equals Maya." The reason that matters is next.

The equals sign is not equality

This is the single biggest mental-model mismatch for people coming from math class.

In math, x = 5 states a fact. It's a claim about the world that is either true or false. In programming, x = 5 is an order: "go do this — put 5 into x." It isn't true or false. It's an action.

Once you see it as an action, this stops being nonsense:

count = 0
count = count + 1
print(count)

Output:

1

In math, count = count + 1 is impossible — no number equals itself plus one. In Python it's ordinary, because the order of operations is always work out the right side first, then put the result into the box on the left:

  1. The right side, count + 1, is calculated using the current value: 0 + 1 is 1.
  2. That 1 goes into count, replacing what was there.

The old value is gone. Boxes hold one thing at a time.

Adding to a variable is so common that there's a shorthand:

count = 0
count += 1
count += 5
print(count)

Output:

6

count += 1 means exactly the same thing as count = count + 1. There's also -=, *=, and /=, which work the same way.

Rules for naming boxes

Hard rules — break these and Python refuses to run:

  • Letters, digits, and underscores only, and it cannot start with a digit. 2name is an error. name2 is fine.
  • Capitalization matters. Name and name are two different boxes. This bites everyone once.
  • You cannot use words Python has reserved for itself, like if, for, class, return, or None.

Soft rules — Python allows these, but do them anyway:

  • Use a name that says what's inside. a = 18 tells your future self nothing. user_age = 18 needs no explanation.
  • Join multiple words with underscores: total_price, first_name, is_logged_in. This style is called snake_case and it's the shared convention across the Python world. Following it makes your code look like everyone else's, which is a feature.

One more trap: naming a variable print is technically allowed, but it overwrites the built-in print function, and the rest of your program breaks in a confusing way. Don't name things after functions you use.

What can go in a box

Four kinds of value cover most of what you'll write:

title = "Lighthouse Code"   # str   — text
count = 42                  # int   — a whole number
price = 19.9                # float — a number with a decimal point
is_free = True              # bool  — only True or False, capitalized

Nothing prints here — these four lines only fill boxes. If you want to see what kind of thing is inside a box, ask:

price = 19.9
print(type(price))
print(type("19.9"))

Output:

<class 'float'>
<class 'str'>

Those two look similar on screen and behave completely differently. 19.9 is a number you can do math with. "19.9" is three characters, a dot, and another character. Lesson 3 is about the trouble that causes.

f-strings: dropping values into a sentence

The comfortable way to build a sentence out of variables is to put an f right before the opening quote, then punch holes with {}:

name = "Maya"
age = 18
print(f"{name} is {age} years old")

Output:

Maya is 18 years old

Python replaces each {...} with the current value of what's inside. You can put a small calculation in there too:

age = 18
print(f"Next year you turn {age + 1}")

Output:

Next year you turn 19

Forget the f and nothing gets replaced:

name = "Maya"
print("Hello {name}")

Output:

Hello {name}

Python had no reason to treat the braces as special, so it printed them as ordinary characters. This is an extremely common typo. If you ever see curly braces in your output, check for the missing f first.

Ten-second check

x = 10
y = x
x = 99
print(y)

What's y?

The answer is 10. At the moment y = x ran, the right side was worked out first — x was 10 — and that 10 was placed into y. Changing x afterward does nothing to y. They're two separate boxes that briefly held the same value, not two labels on one box.

If you expected 99, you were picturing y as a live link to x. It isn't. Assignment copies the value at that instant and then the two go their separate ways.

Ad slot (AdSense not configured; ads appear here in production)

Hands-on

Reading it isn't knowing it. Writing it is.

0/2 passed

Challenges and grading are completely free, and a wrong answer costs you nothing. Stuck? Hit "Ask the AI": it can see the code you are writing.

Level 01Guess the output+10 pts · +15 XP

Two Boxes, Not One Label

What does this print? ```python x = 10 y = x x = 99 print(y) ```

  • Might be multiple choice. Wrong answers cost nothing, so just try.
Level 02Multiple choice+10 pts · +15 XP

Which Names Are Legal

Which of these work as Python variable names? (Select all that apply.)

  • Might be multiple choice. Wrong answers cost nothing, so just try.

Finished? Mark it

Marking it done saves your progress, gives you 20 XP and keeps your streak alive.