Assignment operators store a value in a variable. The basic one is =. The others, called augmented assignment operators, update a variable using its current value: score += 5 is a short way of writing score = score + 5. You will use them constantly in loops, counters and running totals.
Python Assignment Operators Table
| Operator | Example | Same as |
|---|---|---|
= |
x = 10 |
store 10 in x |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 2 |
x = x * 2 |
/= |
x /= 4 |
x = x / 4 |
//= |
x //= 5 |
x = x // 5 |
%= |
x %= 5 |
x = x % 5 |
**= |
x **= 4 |
x = x ** 4 |
&=, |=, ^= |
x &= 10 |
x = x & 10 (bitwise) |
<<=, >>= |
x <<= 2 |
x = x << 2 |
:= |
(n := len(data)) |
assign inside an expression (“walrus”) |
Arithmetic Assignment Operators
x = 10
x += 5
print(x)
x -= 3
print(x)
x *= 2
print(x)
x /= 4
print(x)
x = 17
x //= 5
print(x)
x = 17
x %= 5
print(x)
x = 3
x **= 4
print(x)
Output:
15
12
24
6.0
3
2
81
Notice that x /= 4 turns x into a float (6.0), because / always gives a float. Use //= if you want to stay with whole numbers.
Bitwise Assignment Operators
f = 12
f &= 10
print(f)
f |= 1
print(f)
f ^= 3
print(f)
f <<= 2
print(f)
f >>= 1
print(f)
Output:
8
9
10
40
20
These follow the same rules as the bitwise operators; they just store the result back in the variable.
Assigning Many Variables at Once
a, b, c = 1, 2, 3
print(a, b, c)
p = q = r = 0
print(p, q, r)
a, b = b, a # swap without a temporary variable
print(a, b)
first, *rest = [10, 20, 30, 40]
print(first, rest)
Output:
1 2 3
0 0 0
2 1
10 [20, 30, 40]
The starred name *rest collects all the remaining items into a list. This is called unpacking.
Assignment Operators with Strings and Lists
greeting = "Hello"
greeting += ", Riya"
print(greeting)
line = "-" * 3
line *= 2
print(line)
nums = [1, 2]
nums += [3]
print(nums)
Output:
Hello, Riya
------
[1, 2, 3]
The Walrus Operator :=
Added in Python 3.8, := lets you assign a value inside an expression, so you don’t have to compute it twice:
data = [4, 8, 15, 16, 23, 42]
if (n := len(data)) > 5:
print(f"List is long: {n} items")
Output:
List is long: 6 items
A Trap: += on Lists Changes the Shared List
For a mutable object like a list, += changes the list in place. If two names point to the same list, both see the change. Tuples are immutable, so += creates a new tuple instead:
list_a = [1, 2]
list_b = list_a
list_b += [3]
print(list_a)
t = (1, 2)
t2 = t
t2 += (3,)
print(t, t2)
Output:
[1, 2, 3]
(1, 2) (1, 2, 3)
If you want a separate list, make a copy first: list_b = list_a.copy().
Common Mistakes
- Writing
x =+ 5instead ofx += 5.x =+ 5is valid Python, but it meansx = +5, so x simply becomes 5. - Using
x++. Python has no++or--operator. Usex += 1. - Using a variable before giving it a value.
total += marksfails with a NameError iftotalwas never set. Start withtotal = 0.
Practice Questions
- What does this print?
k = 7; k += k; print(k) - What does this print?
k = 20; k //= 3; k **= 2; print(k)
Answers: 1) 14. 2) 36, because 20 // 3 is 6 and 6 ** 2 is 36.
Frequently Asked Questions
What is the difference between = and == in Python?
= stores a value in a variable. == compares two values and returns True or False.
Is x += 1 the same as x = x + 1?
For numbers and strings, yes. For lists, += changes the existing list in place, while x = x + [1] creates a new list.
What is the walrus operator in Python?
:=, which assigns a value inside an expression, such as an if or while condition. It needs Python 3.8 or newer.
Next: Python logical operators · Previous: Python arithmetic operators · All operators: Python operators overview