The Problem
You've watched the fetch-execute cycle run instructions one at a time (Phase 00). The very first instruction you'll write yourself is almost always something like age = 25. It looks like the simplest possible line of code — and it is also, across decades of teaching beginners, one of the most quietly misunderstood, because everyone arrives already knowing what = means from math class. That meaning is wrong here, and the gap between "what = means in algebra" and "what = means in Python" is where a huge number of first-month bugs come from.
A variable is a name tag, not a box
Plenty of intro material describes a variable as a box that stores a value. It's a fine mental model for about five minutes — right up until you meet a line like this:
x = x + 1
In algebra, "x equals x plus 1" has no solution. No number is equal to itself plus one. If you're still holding onto the "box" model, this line should look like nonsense — how can a box that holds 5 become a box that holds 6 just by being told to hold "itself plus 1"? But this exact line is completely ordinary, correct Python, and it does exactly what you'd want.
The box model is what's wrong. Here's the one that holds up: a variable name is a tag, tied onto a value that already exists. Assignment doesn't reach into a box and change what's inside it — it computes a value, then ties (or re-ties) a name onto it.
Walk through x = 5 this way: the value 5 comes into existence, and the tag x gets tied to it. Nothing about 5 itself is "x-shaped" — x is just a label currently attached to it.
Now walk through x = x + 1 the same way, in the order Python actually does it:
- Look up what
x is currently tied to: 5. - Compute the entire right-hand side using that value:
5 + 1 = 6. - Only now does the assignment happen: the tag
x is untied from 5 and retied onto the new value 6.
The original 5 didn't change — it can't, numbers don't mutate. It simply has no tag on it anymore. Python will quietly clean it up later; that's not something you need to manage by hand.
"=" doesn't mean "equals" — it means "bind"
Read x = x + 1 out loud as "compute the right side using x's current value, then make x mean that" and it stops being paradoxical. Assignment and mathematical equality are different operations that happen to share a symbol. Every language design has to pick some symbol for assignment; Python (like C, Java, and most others) picked the one that looks like equality, and that visual collision is the entire source of this misconception.
The four types you'll use constantly
Every value in Python has a type, and type() will tell you exactly what it is:
print(type(5))
print(type(5.0))
print(type("5"))
print(type(True))
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
| Type | Example values | What it's for |
|---|
int | 5, -3, 1000000 | Whole numbers |
float | 5.0, 3.14, -0.5 | Numbers with a decimal point |
str | "hello", 'Ada' | Text, in single or double quotes |
bool | True, False | Exactly two values, used for yes/no logic |
Notice: 5 and "5" are not the same value. One is a number you can do arithmetic on; the other is a single character of text that happens to look like a number. This distinction is about to matter a great deal.
Dynamic typing: types belong to values, not names
Some languages make you declare a variable's type up front, and that type is locked in forever. Python doesn't:
count = 5
count = "five"
This runs with no error at all. That's what dynamic typing actually means — not "Python doesn't have types" (it very much does, as you just saw with type()), but "types belong to values, and names are just tags that can be re-tied to a value of any type, at any time." The name count doesn't have a type. The 5 has type int. The "five" has type str. count is just whichever one it's currently tagging.
When types collide: A real TypeError
Here's where the "name tag, not a box" model and the "types belong to values" idea collide in a way that produces a genuinely common real bug:
age = 25
message = "You are " + age + " years old"
print(message)
Traceback (most recent call last):
File "example.py", line 2, in <module>
message = "You are " + age + " years old"
~~~~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to str
+ doesn't mean one fixed thing in Python — it means something different depending on the types on either side of it. Between two strings, it means "join them together." Between two numbers, it means "add them." Between a string and a number, Python has no built-in definition for what that should mean, so instead of guessing, it stops and tells you precisely which two types collided.
The fix is to convert explicitly, using str() to turn the number into text before joining it:
age = 25
message = "You are " + str(age) + " years old"
print(message)
You are 25 years old
Python will not guess what you meant
Some languages (JavaScript, for instance) would silently convert age into the string "25" for you and let this run without complaint. Python refuses on purpose. A silent, guessed conversion is exactly the kind of thing that produces a bug three files away from where it actually happened — Python would rather stop you here, at the source, with a precise message about which two types didn't fit together.
count = 3
count = count + 1
count = count * 2
label = "total: "
print(label + str(count))
Trace it the same way as x = x + 1 above: count starts tied to 3. Line 2 computes 3 + 1 = 4 first, then re-ties count to 4. Line 3 computes 4 * 2 = 8 first, then re-ties count to 8. label is a completely separate name, tied to the string "total: ". The final line needs str(count) because label is a str and count is an int — without the conversion, this would be the exact same TypeError as the age example above.