← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 10 of 16

Python Assignment Operators

Basic Assignment

The plain = operator binds the name on the left to the value on the right — this is assignment, not mathematical equality, and confusing it with == is one of the most common early Python mistakes.

Example: Basic Assignment

python
x = 5
print(x)

Add and Subtract Assignment

+= adds a value to a variable and stores the result back in that same variable in one step, so count += 1 is shorthand for count = count + 1; -= does the same for subtraction.

Example: Add and Subtract Assignment

python
count = 10
count += 1
print(count)
count -= 2
print(count)

Multiply and Divide Assignment

*= and /= apply the same "do the operation, then reassign" pattern to multiplication and division, letting you update a running total or scale a value without retyping the variable name twice.

Example: Multiply and Divide Assignment

python
total = 5
total *= 3
print(total)
total /= 3
print(total)

Floor Divide and Modulo Assignment

//= performs floor division and reassigns the result, while %= computes the modulo remainder and reassigns it — both are shorthand forms that come up often when tracking counters or wrapping values within a fixed range.

Example: Floor Divide and Modulo Assignment

python
n = 17
n //= 5
print(n)
m = 17
m %= 5
print(m)

Exponent and Bitwise Assignment

**= raises a variable to a power and reassigns the result, and bitwise compound operators like &= and |= apply the same shorthand pattern to bit-level operations — all follow the identical "operate then store" logic as +=.

Example: Exponent and Bitwise Assignment

python
x = 2
x **= 3
print(x)
flags = 1
flags &= 3
print(flags)

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.