Python Assignment Operators
Table of Contents
In the last lesson, we learned logical operators. Now let’s learn shortcuts for updating variables.
After learning Python Logical Operators, we can combine conditions. Now let’s return to variables and learn shorter ways to update their values. In this tutorial, we’ll learn about Python assignment operators with easy examples.
Let’s Talk About Storing Values
We already know how to create a variable.
x = 5That equals sign = is called the assignment operator. It assigns the value on the right to the variable on the left.
But did we know Python has more assignment operators? Not just =. There are several shortcuts.
Let’s learn them.
The Basic Assignment Operator (=)
We already know this one.
x = 5name = "Alex"price = 19.99is_active = TrueIt takes the value on the right. And puts it in the variable on the left.
Simple.
But here is the thing. Sometimes we want to update a variable based on its own value.
Like this:
score = 10score = score + 5print(score)Output
15This works. But it is a bit long. Python has a shortcut for this.
Augmented Assignment Operators
These are shortcuts. They combine an operation with assignment.
Instead of writing x = x + 5, we can write x += 5.
Same thing. Less typing.
Let’s see all of them.
Python += Operator
Adds to the variable.
x = 10x += 5 # same as x = x + 5print(x)Output
15We can use it with strings too.
message = "Hello"message += " World"print(message)Output
Hello WorldPython -= Operator
Subtracts from the variable.
x = 10x -= 3 # same as x = x - 3print(x)Output
7Python *= Operator
Multiplies the variable.
x = 10x *= 2 # same as x = x * 2print(x)Output
20We can use it with strings too.
message = "Ha"message *= 3print(message)Output
HaHaHaPython /= Operator
Divides the variable.
x = 10x /= 2 # same as x = x / 2print(x)Output
5.0Remember, division always gives a float. So x becomes 5.0, not 5.
Python //= Operator
Floor division assignment.
x = 10x //= 3 # same as x = x // 3print(x)Output
3Python %= Operator
Modulo assignment.
x = 10x %= 3 # same as x = x % 3print(x)Output
1Python **= Operator
Exponentiation assignment.
x = 2x **= 3 # same as x = x ** 3print(x)Output
8All Assignment Operators at a Glance
| Operator | Example | Same as |
|---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
//= | x //= 5 | x = x // 5 |
%= | x %= 5 | x = x % 5 |
**= | x **= 5 | x = x ** 5 |
Real-Life Example: Game Health
Let’s say we are building a simple game. The player starts with 100 health.
health = 100print(f"Starting health: {health}")
# Player takes damagehealth -= 20print(f"After damage: {health}")
# Player finds a health packhealth += 30print(f"After health pack: {health}")
# Player takes more damagehealth -= 50print(f"After more damage: {health}")Output
Starting health: 100After damage: 80After health pack: 110After more damage: 60See how clean that is? Instead of writing health = health - 20, we just write health -= 20.
Much easier to read.
Another Example: Counting
Let’s count from 1 to 5.
count = 0
count += 1print(count)
count += 1print(count)
count += 1print(count)
count += 1print(count)
count += 1print(count)Output
12345This is a bit repetitive. But it shows how += works. We will see this pattern a lot in loops later.
Why Use Augmented Assignment?
Two reasons.
Reason 1: Less typing
x += 5 is shorter than x = x + 5. Not a big deal for one line. But when we have many lines, it adds up.
Reason 2: Clearer
x += 5 clearly says “add 5 to x.” It is easier to read.
Both work. But most Python programmers use the shortcuts.
Common Mistakes with Python Assignment Operators
Mistake 1: Using += in an Expression
Here’s a mistake beginners make. We write y = x += 5. And we get an error.
x = 10y = x += 5 # Error!Why? Because += is a statement. It can’t be part of an assignment like that.
So what do we do? We split it into two steps.
x = 10x += 5y = xprint(y)Output
15That works.
Mistake 2: Using += on a Variable That Doesn’t Exist
Let’s say we write x += 5 without creating x first. We get an error.
x += 5 # Error!Why? Because x does not exist yet. We cannot add to something that is not there.
So what do we do? We create the variable first.
x = 0x += 5print(x)Output
5Let’s always create the variable first.
Mistake 3: Expecting ++ to Work
If we come from C or Java, we might expect x++ to work. It doesn’t in Python.
x = 5x++ # Error!So what do we do? We use += and -=.
x = 5x += 1print(x) # 6
x -= 1print(x) # 5That is the Python way.
Next, we will learn about Membership Operators so we can continue step by step.