Variables and Assignment
Programs would be useless if they couldn’t remember anything. Variables are how Python remembers — and today they click for you forever.
The mental model: a labeled box
A variable is a box with a label. You put a value in, and the label lets you find it later:
name = "Galvan"
Read that line as: “create a box labeled name, and put the text Galvan inside.” The = sign is not “equals” like in math class — it’s the assignment operator: “put the right side into the left side.”
The pattern: name = value
score = 99 # a number
player = "Rohit" # text (a string)
is_game_over = False # a True/False flag
Three assignments, three boxes. Python figures out what kind of value each holds — you never announce the type.
Variables can change (that’s why they’re called variables)
score = 10
print(score) # 10
score = 50
print(score) # 50 — the old 10 is gone, replaced
Assignment overwrites. The box labeled score now holds 50; the 10 is forgotten forever.
Variables can be built from other variables
base_price = 100
tax = 18
total = base_price + tax
print(total) # 118
The right side is computed first, then stored. total holds 118 — and importantly, it holds a copy of the value, not a live link. Change base_price later and total stays 118 until you reassign it.
The update pattern (self-assignment)
score = 10
score = score + 5 # right side computes 10+5=15, THEN stores into score
print(score) # 15
This looks like math nonsense (“x = x + 5”?) but reads as an instruction: “take the current score, add 5, put it back.” This pattern appears in almost every app you’ll build — counters, totals, streaks. Python even has a shorthand you’ll meet in the operators module: score += 5.
Naming recap (from the tokens lesson)
- Legal: letters, digits, underscores; can’t start with a digit; can’t be a keyword
- Style:
snake_case, descriptive —total_pricebeatstp - Case-sensitive:
scoreandScoreare different boxes
Common Errors & Fixes
NameError: name 'total' is not defined— you used the variable before creating it, or misspelled it. Python runs top-to-bottom; the box must exist before you open it.TypeError: can only concatenate str...— you triedage = "5" + 1. Text plus number doesn’t mix; convert first (int("5") + 1) — the type conversion lesson covers this fully.- “My variable is empty” — you created it inside an
ifblock that never ran, so the assignment never executed.
✅ Checkpoint
- What does
=mean in Python? (Assignment — put the right side into the left side) - What does this print?
x = 5thenx = x + 2thenprint(x)(7) - After
a = b = 10… actually, try it: does Python allow it? (Yes — both labels point to 10) - If
total = base + taxand you later changebase, doestotalupdate? (No — total holds a copy of the old result)
Next: numbers — the arithmetic your apps will run on, including the division gotcha.