Python Assignment Operators

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 = 5

That 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 = 5
name = "Alex"
price = 19.99
is_active = True

It 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 = 10
score = score + 5
print(score)

Output

15

This 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 = 10
x += 5 # same as x = x + 5
print(x)

Output

15

We can use it with strings too.

message = "Hello"
message += " World"
print(message)

Output

Hello World

Python -= Operator

Subtracts from the variable.

x = 10
x -= 3 # same as x = x - 3
print(x)

Output

7

Python *= Operator

Multiplies the variable.

x = 10
x *= 2 # same as x = x * 2
print(x)

Output

20

We can use it with strings too.

message = "Ha"
message *= 3
print(message)

Output

HaHaHa

Python /= Operator

Divides the variable.

x = 10
x /= 2 # same as x = x / 2
print(x)

Output

5.0

Remember, division always gives a float. So x becomes 5.0, not 5.

Python //= Operator

Floor division assignment.

x = 10
x //= 3 # same as x = x // 3
print(x)

Output

3

Python %= Operator

Modulo assignment.

x = 10
x %= 3 # same as x = x % 3
print(x)

Output

1

Python **= Operator

Exponentiation assignment.

x = 2
x **= 3 # same as x = x ** 3
print(x)

Output

8

All Assignment Operators at a Glance

OperatorExampleSame as
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
//=x //= 5x = x // 5
%=x %= 5x = x % 5
**=x **= 5x = x ** 5

Real-Life Example: Game Health

Let’s say we are building a simple game. The player starts with 100 health.

health = 100
print(f"Starting health: {health}")
# Player takes damage
health -= 20
print(f"After damage: {health}")
# Player finds a health pack
health += 30
print(f"After health pack: {health}")
# Player takes more damage
health -= 50
print(f"After more damage: {health}")

Output

Starting health: 100
After damage: 80
After health pack: 110
After more damage: 60

See 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 += 1
print(count)
count += 1
print(count)
count += 1
print(count)
count += 1
print(count)
count += 1
print(count)

Output

1
2
3
4
5

This 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 = 10
y = 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 = 10
x += 5
y = x
print(y)

Output

15

That 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 = 0
x += 5
print(x)

Output

5

Let’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 = 5
x++ # Error!

So what do we do? We use += and -=.

x = 5
x += 1
print(x) # 6
x -= 1
print(x) # 5

That is the Python way.

Next, we will learn about Membership Operators so we can continue step by step.