Day 10 · ~14m

Variables & Expressions

How Python stores data, evaluates expressions, and the difference between = and ==.

🧑‍💻

OK so last week we used variables all over the place — name = "Alice", score = 95, stuff like that. But I realized I don't actually understand what's happening under the hood. When I write x = 5, what is Python doing?

👩‍🏫

Good question — and the fact that you're asking it after a week of writing code means your instincts are developing. Last week you used variables as tools. This week you'll understand them as concepts.

Python does two things when you write x = 5. First, it creates the value 5 in memory. Second, it attaches the name x to that value — like sticking a label on a box. The = sign is the assignment operator. It doesn't mean "equals" in the math sense. It means "make this name point to this value."

x = 5
y = x + 3
print(y)  # 8

The right side is evaluated first. Python computes x + 3 (which is 8), then assigns the result to y.

🧑‍💻

So x + 3 is an expression? I keep hearing that word. What exactly counts as an expression?

👩‍🏫

An expression is anything Python can evaluate to produce a value. Numbers, strings, math operations, function calls, comparisons — all expressions.

42              # literal expression
x + 3           # arithmetic expression
len("hello")    # function call expression
x > 10          # comparison expression (produces True or False)

A statement, on the other hand, is an instruction that does something. Assignments, if blocks, for loops — those are statements. The key difference: expressions produce values, statements perform actions.

# Statement: assigns a value
total = 10 + 20

# Expression: the 10 + 20 part produces 30
# Statement: the total = ... part stores it

Remember how last week you wrote if score >= 90:? The score >= 90 part is an expression — it produces True or False. The if ... : part is a statement — it decides what to do next.

🧑‍💻

Wait, I just realized something. You said = doesn't mean equals. But last week I wrote if x = 5: in one of my experiments and it broke. Why?

👩‍🏫

That's the classic beginner bug, and you just discovered it yourself — which is great. To check if two things are equal, you use == — the equality operator:

x = 5       # assignment: store 5 in x
x == 5      # comparison: is x equal to 5? → True
x == 10     # comparison: is x equal to 10? → False

If you write if x = 5:, Python throws a SyntaxError because = is assignment, not comparison. Always use == inside conditions. Think of it this way: one = means "store this," two == means "check this."

🧑‍💻

Can I change a variable after creating it? Like, my spreadsheet cells update when I change the formula...

👩‍🏫

Variables in Python are even more flexible than cells — you can reassign them anytime:

score = 0
print(score)    # 0

score = score + 10
print(score)    # 10

score += 5      # shorthand for score = score + 5
print(score)    # 15

The += operator is called an augmented assignment. There's also -=, *=, and /=. They all follow the same pattern: take the current value, apply the operation, store the result back. Remember the accumulator pattern from Day 7? That total += score inside the loop? Now you know exactly what it's doing.

🧑‍💻

What about naming rules? Last week I just used simple names like name and score. Can I call a variable anything?

👩‍🏫

Almost anything. Variable names must start with a letter or underscore, can contain letters, numbers, and underscores, and are case-sensitive. But they can't be Python keywords like if, for, def, or return.

my_name = "Alice"    # good
_count = 0           # good (leading underscore is fine)
student2 = "Bob"     # good
2nd_place = "Charlie" # SyntaxError — can't start with a number
for = 10             # SyntaxError — 'for' is a keyword

The convention in Python is snake_case — lowercase words separated by underscores. student_name, not studentName or StudentName.

Now let's practice the difference between assignment and equality.

Practice your skills

Sign up to write and run code in this lesson.

Already have an account? Sign in