PythonAssignment Operators

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

Python
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

+=

x = x + y

total += 5

-=

x = x - y

count -= 1

*=

x = x * y

price *= 1.1

/=

x = x / y

average /= 2

//=

x = x // y

pages //= 10

%=

x = x % y

index %= len(items)

**=

x = x ** y

value **= 2

&=

x = x & y

flags &= mask

|=

x = x | y

flags |= 0b0100

^=

x = x ^ y

flags ^= 0b0001

>>=

x = x >> y

value >>= 1

<<=

x = x << y

value <<= 1

Augmented assignment in action

Python
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))
Mutation vs. reassignment
For lists, `list += [x]` calls `__iadd__` and extends the list in place, while `list = list + [x]` builds a brand-new list. This matters if another variable is aliasing the same list — `+=` will be visible through the alias, `= ... + ...` will not.
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

Python
# 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

Python
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]
Tip
Use `:=` when a value is computed once but needed twice (in a condition and in the body/result). If you only need it once, a plain `=` statement is clearer — the walrus operator is a convenience for a specific shape of problem, not a replacement for normal assignment.
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

Python
# 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 = 0 binds all three names to the same object — safe for immutable values like ints, but be careful with mutable defaults such as a = b = [], since both names share one list.

  • a, b = b, a works 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].