Python Assignment Operators
Table of Contents + −
In the last lesson you learned Logical Operators. There you combined true-or-false answers to make decisions. Now we move to a different job. We change what a variable holds. Maybe you are new to coding. Maybe you come from another language. Either way, this is one of those small skills you will use in almost every program you write.
🤔 Why Assignment Operators?
Here is a pain you hit fast. You have a score. You want to add one to it. So you write the new value using the old value:
score = 0score = score + 1score = score + 1print(score)That works. But you type the variable name twice every time. Do that again and again and it gets long. It is also easy to mistype. The fix is the assignment operator in its shortcut form. It is a tiny symbol that says “take what is already here, do something to it, and store the result back”. So score += 1 does the same thing as score = score + 1, with less typing.
🧠 What Is an Assignment Operator?
An assignment operator is the symbol that puts a value into a variable. The plain one is the single equals sign =. You have already used it many times:
name = "Riya"age = 25The = here does not mean “is equal to” like in maths. It means “store the value on the right into the name on the left”. So read age = 25 as “let age be 25”. It is not a question.
Think of a variable like a labelled box. The = is you putting something into the box. The right side gets worked out first. Then the result drops into the box on the left.
Note
In Python, = means “assign” (store a value). To check if two things are equal you use ==, the double equals. We cover comparison in its own lesson. So just remember this: one equals stores, two equals compares.
➕ The Shortcut Forms
Now the useful part. Python gives you a shortcut for the common pattern “update a variable using its own value”. These are called augmented assignment operators. “Augmented” just means the plain = got an extra job added to it.
The idea is always the same. Take this:
total = total + 5And shorten it to this:
total += 5Both read the current value of total, add 5, then store the answer back into total. The shortcut form just saves you from writing total twice.
Here is each shortcut and the longer form it stands for:
| Shortcut | Same as | What it does |
|---|---|---|
x += 5 | x = x + 5 | Add 5 to x |
x -= 5 | x = x - 5 | Subtract 5 from x |
x *= 5 | x = x * 5 | Multiply x by 5 |
x /= 5 | x = x / 5 | Divide x by 5 (result is a decimal) |
x //= 5 | x = x // 5 | Divide and drop the fraction (whole number) |
x %= 5 | x = x % 5 | Keep the remainder after dividing |
x **= 5 | x = x ** 5 | Raise x to the power of 5 |
Notice the pattern. The operator (+, -, *, and so on) comes first. Then the equals sign. So you read *= as “multiply, then store back”.
🔢 A Simple Example
Let us walk one variable through a few of these. The code runs top to bottom. Each line changes x based on its current value:
x = 10
x += 4 # x is now 14x -= 2 # x is now 12x *= 3 # x is now 36x //= 5 # x is now 7x %= 4 # x is now 3x **= 2 # x is now 9
print(x)Let us read it line by line:
x += 4takes 10, adds 4, stores 14.x -= 2takes 14, subtracts 2, stores 12.x *= 3takes 12, times 3, stores 36.x //= 5divides 36 by 5 and drops the fraction (7.2 becomes 7).x %= 4keeps the remainder of 7 divided by 4, which is 3.x **= 2raises 3 to the power 2, which is 9.
So the final value printed is 9.
Output
9
Here is the why. Each shortcut always works on the value the variable holds right now. It does not use the value it started with. That is what makes them perfect for counting and totals.
🎮 A Real Example: A Running Total
This is where assignment operators are most useful. Say Alex is playing a quiz game. Every right answer adds points. A wrong answer takes some away. A bonus round doubles the score. We keep one variable, score, and keep updating it:
score = 0
score += 10 # right answerscore += 10 # another right answerscore -= 5 # wrong answerscore *= 2 # bonus round doubles it
print(f"Final score: {score}")Read it as a story:
- We start
scoreat 0. - First right answer adds 10, so
scoreis 10. - Second right answer adds another 10, so
scoreis 20. - A wrong answer subtracts 5, so
scoreis 15. - The bonus round doubles it, so
scorebecomes 30.
The program prints this:
Output
Final score: 30
See how score carries its value forward each step? That is a running total. It is one variable that keeps the result so far and updates as new events happen. You will use this exact pattern for shopping carts, game scores, step counters, anything that grows or shrinks over time.
🔤 They Work on Text Too
The += operator is not only for numbers. With strings (text), += joins more text onto the end. This is handy when you build up a sentence piece by piece:
message = "Hello"message += ", "message += "Riya"message += "!"
print(message)Each += glues the new text onto what message already holds. So the box fills up step by step:
Output
Hello, Riya!
Tip
A quick rule. += adds for numbers and joins for text. Python looks at what is in the box and does the matching action. Just do not mix the two. More on that next.
⚠️ Common Mistakes
A few traps catch people early on. Watch for these:
- Using a variable before it exists. A shortcut like
+=reads the current value first. So the variable must already have one. If it does not, Python stops with aNameError.
# ❌ Avoid: count was never given a starting valuecount += 1 # NameError: name 'count' is not defined
# ✅ Good: set a starting value firstcount = 0count += 1 # now count is 1- Writing the operator backwards. It is
+=, not=+. Writing=+does not add. It quietly stores+3and wipes the old value. That makes it a silent bug.
total = 5# ❌ Avoid: this just stores +3, wiping the old valuetotal =+ 3 # total becomes 3, not 8
# ✅ Good: this adds 3 to what is theretotal += 3 # total becomes 8- Mixing text and numbers. You cannot
+=a number onto a string. Python raises aTypeError. It does not know whether you meant to add or to join.
label = "Level "# ❌ Avoid: number onto textlabel += 5 # TypeError
# ✅ Good: turn the number into text firstlabel += str(5) # "Level 5"✅ Best Practices
- Reach for the shortcut form (
score += 1) over the long form (score = score + 1). It is shorter and the intent is clearer. - Always give a variable a starting value before you update it. For counters and totals that usually means setting it to
0. For text use an empty string"". - Keep the name meaningful.
total,score, andcounttell the reader what is being tracked far better thanx. - Pick the operator that matches the real action. Use
+=to add,*=to scale,%=to wrap around. Reading the code should tell the story of what is happening.
🧩 What You’ve Learned
✅ The single = stores a value into a variable; it does not mean “is equal to”.
✅ Augmented assignment operators (+=, -=, *=, /=, //=, %=, **=) update a variable using its own current value.
✅ score += 1 is just a shorter way to write score = score + 1.
✅ These operators are the natural way to build a running total, like a game score or a shopping cart.
✅ += adds for numbers and joins text for strings, but you must set a starting value before you update.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is `score += 5` short for?
Why: The += shortcut reads the current value of score, adds 5, and stores the result back, exactly like score = score + 5.
- 2
After this code runs, what does x hold? x = 6 x *= 2 x -= 3
Why: x *= 2 makes x 12, then x -= 3 makes it 9, because each line works on the value x holds right then.
- 3
Why does `count += 1` fail if count was never given a value?
Why: An augmented assignment reads the existing value before updating, so the variable must already exist, or Python raises a NameError.
- 4
What does `message += "!"` do when message is a string?
Why: For strings, += joins the new text onto the end, so message grows by one character.
🚀 What’s Next?
You can now update a variable using its own value. That is the heart of counting and totals. Next we look at how to ask whether a value is inside a collection like a list or a piece of text.