The first time I wrote two lines like this, I expected Python to argue with me:
mood = "tired"
mood = "awake"In maths class, the equals sign is sacred. You cannot say x = 3 and then x = 5 on the next line without getting marked down for contradicting yourself. I sat at the keyboard waiting for Python to underline something in red. It did not. It shrugged, ran both lines, and moved on with its life.
It took me an embarrassingly long time to realise that the = in Python is not the = from maths class. They look the same. They mean something completely different.
Why Python does not complain
In maths, = makes a statement about the world. "The left side is the same as the right side." It is a claim. You cannot claim two contradictory things at once.
In Python, on a line like mood = "tired", the = is not a claim. It is an instruction. It says: "Right now, put the thing on the right into the jar on the left."
That is a present-tense action, not a permanent truth. When you give the same instruction again with a different value, Python just follows it. The jar already exists. The old contents get tipped out. The new contents go in. No fuss, no contradiction, no red underline.
What actually happens to the jar
Picture the kitchen shelf again. Yesterday you wrote pepper on a jar's label and filled it with ground pepper. Today you want to keep salt in that jar instead. You do not go and buy a brand new jar. You do not write a new label. You empty the pepper out, wash the jar, pour the salt in, and put it back on the shelf. Same jar. Same label. Different contents.
mood = "tired" # the jar called mood now holds "tired"
print(mood) # prints: tired
mood = "awake" # the "tired" gets tipped out, "awake" goes in
print(mood) # prints: awakeAfter that second line, the word "tired" is gone. Python does not keep a history. It does not remember what was in the jar before. If you wanted to keep the old value too, you should have put it in a second jar.
old_mood = mood # copy the current contents into a second jar
mood = "awake" # now safely overwrite the originalTwo jars. Two different contents. Python lets you keep as many as you want, as long as each one has its own label.
Why this matters
Almost every useful program changes the value of some variable as it runs. A score ticks up. A counter increments. A user's input replaces a default. A running total grows as more numbers come in. Without reassignment, every line of your program would be frozen the moment you wrote it, and your code would only ever describe a single moment in time.
The most common pattern, and one you will write a hundred times this term:
count = 0
count = count + 1 # count is now 1
count = count + 1 # count is now 2
count = count + 1 # count is now 3Read that second line out loud the way Python reads it: "take what is in count right now, add 1 to it, and put the result back in count." Python evaluates the right side first using the current value (0 + 1 = 1), and only then does the new value land in the jar. The same shape works for any value type:
total = total + new_amount
name = name + " Mensah"
score = score - 5You are using the current contents to compute the new contents. The old contents disappear in the process. That is the engine that makes a program do something over time.
The line that confuses everyone once
Here is the line that trips up almost every beginner I have ever taught:
x = 5
x = x + 1"How can x equal x + 1? That would mean 5 = 6. That is impossible."
It would be impossible if the = meant "is equal to." But it does not. It means "evaluate the right side, then put the result in the jar on the left." Python does that in three small steps:
- It looks at the right side:
x + 1. It reads the jarx, sees5, and computes5 + 1. The result is6. - The line is now effectively
x = 6. - It does the assignment. The
5inxis replaced by6.
There is no contradiction because the old x and the new x never exist at the same time. The right side is computed first, using the old value, and only after that computation does the new value land in the jar. By the time the new value is there, the old one is gone.
Once that clicks, you will never read x = x + 1 as a maths claim again. You will read it as a kitchen instruction: "look in the jar, add one to what you find, put the result back."
Reading versus writing
One last frame that helped me. There are two things you can do with a variable. You can read from it, or you can write to it.
print(name)reads fromname. The contents stay put.name = "Amara"writes toname. The contents get replaced.greeting = "Hello, " + namereads fromnameand writes togreeting.
A single line can do one of these, or both. Once you start reading code with this lens, you will notice variables move through a program the way ingredients move through a kitchen. Picked up, used, replaced, picked up again.
Take this with you
There is no new syntax to learn for any of this. You already know =. You already know how to make a variable. The new idea is just this: the jar is not the value. The jar is a container that currently holds a value. You can pour something new into it any time you like. Python will not argue.
The day you stop reading x = x + 1 as a contradiction is the day you have stopped treating Python like maths and started reading it like instructions to a patient kitchen helper. Which is what it has been all along.