Assignment Operators
Assignment is how Python binds a name to a value. The plain = operator is the one you use every single day, but Python also gives you a family of augmented assignment operators that combine an arithmetic or bitwise operation with assignment in one step, plus the walrus operator for assigning inside an expression. This page walks through all of them.
Basic assignment
A single = binds the name on the left to the value on the right. Unlike some languages, this is not a "copy" for mutable objects — the name is just a label pointing at the same object in memory.
Basic assignment
x = 10 name = "Ada" numbers = [1, 2, 3] # Two names can point at the same list object alias = numbers alias.append(4) print(numbers) # [1, 2, 3, 4] -- both names see the change
Augmented assignment operators
Augmented assignment lets you write x = x + 1 as x += 1. It reads the current value, applies the operator, and reassigns the result — with the added benefit that, for mutable types like lists, some of these operators mutate in place rather than creating a new object.
Operator | Equivalent To | Example |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Augmented assignment in action
score = 100 score += 25 # 125 score -= 10 # 115 score *= 2 # 230 score //= 7 # 32 score %= 5 # 2 score **= 3 # 8 flags = 0b0000 flags |= 0b0101 # 0b0101 flags &= 0b0100 # 0b0100 flags <<= 2 # 0b010000 print(score, bin(flags))
The walrus operator := (Python 3.8+)
The walrus operator lets you assign a value to a name as part of a larger expression, instead of needing a separate statement. It is most useful when you want to reuse a computed value without calling the same function twice, or when a loop condition and the value it depends on are naturally the same expression.
Reading input until a sentinel value
# Without walrus: input() is called twice, and duplicated
line = input("Enter a command (or 'quit'): ")
while line != "quit":
print(f"You typed: {line}")
line = input("Enter a command (or 'quit'): ")
# With walrus: assignment happens inside the while condition itself
while (line := input("Enter a command (or 'quit'): ")) != "quit":
print(f"You typed: {line}")Reusing a computed value in a list comprehension
data = [1, 4, 9, 15, 22, 30, 45] # Without walrus: compute the expensive check twice results = [y for x in data if (y := x * x - 10) > 50] # y is bound to x * x - 10 once per element, then reused in the output # expression -- avoids recomputing "x * x - 10" a second time print(results) # [71, 210, 480, 990, 2015]
Chained and multiple assignment
Python supports assigning the same value to several names at once, swapping values without a temporary variable, and unpacking a sequence into multiple names in a single statement.
Chained assignment, swap, and unpacking
# Chained assignment: all three names point to the same int object a = b = c = 0 # Swap two variables in one line -- no temp variable needed a, b = 1, 2 a, b = b, a print(a, b) # 2 1 # Unpacking a sequence into multiple names a, b, c = 1, 2, 3 print(a, b, c) # 1 2 3
a = b = c = 0binds all three names to the same object — safe for immutable values like ints, but be careful with mutable defaults such asa = b = [], since both names share one list.a, b = b, aworks because Python evaluates the right-hand side tuple(b, a)fully before doing any assignment.Unpacking requires the number of names to match the number of values, unless you use a starred name like
a, *rest = [1, 2, 3, 4].