Assignment

Bind a name to an object with =, optionally combined with an operator, or applied to several targets at once.

                target = expression
              

Purpose

= binds a name (the target) to the object an expression evaluates to. It does not copy the object or store a value “inside” the name — the name is just a reference to wherever the object already lives.

Example

prices = [19.99, 5.5, 3.25]
subtotal = sum(prices)

# Augmented assignment: rebinds subtotal to a new, larger value.
subtotal += 2.00

# Chained assignment: both names are bound to the same one object.
first = second = []

# Multiple assignment (tuple unpacking): each target gets its own object.
width, height = 12, 8

Behavior notes

  • Plain assignment never copies. After a = b, a and b are two names bound to the same object — mutating through one is visible through the other (for mutable objects; see id() for how to check this directly).
  • Chained assignment (first = second = []) evaluates the right-hand side once and binds every target to that single resulting object — it does not create one list per name.
  • Augmented assignment (x += 1) rebinds x to a new result for immutable types (int, float, str, tuple); for a mutable type that defines it, += mutates the existing object in place instead of creating a new one. Both end with x bound to a value that looks the same either way, but only one of them actually changed the original object.
  • Multiple assignment (width, height = 12, 8) unpacks an iterable of the right length into separate targets — each target gets its own object from the sequence, unlike chained assignment.

Common mistakes

  • Assuming first = second = [] creates two independent empty lists. It creates one list object and binds both names to it — appending through first is visible through second too.
  • Assuming += always mutates in place. Whether it does depends entirely on the object’s type; relying on the distinction without checking it is a common source of subtle bugs.

Implementation notes

Assignment semantics — name binding, chained-assignment sharing, and augmented assignment’s type-dependent behavior — are defined by the language itself and identical across every conforming Python implementation.

Taught in these lessons

Related reference