Multiple Assignment
flowchart TD A["a = b = []"] --> B["ONE list is created"] B --> C["a and b both point at it"] C --> D["a.append(1) -> b is [1] too"] E["a, b = [], []"] --> F["TWO lists are created"] F --> G["a and b are independent"] G --> H["a.append(1) -> b is still []"] D --> I["fine for 0, None or a string -- they cannot be changed"] D --> J["a bug waiting for any mutable value"]
Many Values to Multiple Variables
Section titled “Many Values to Multiple Variables”We can assign multiple values to multiple variables in one line. This is called multiple assignment.
Example:
Section titled “Example:”# Multiple assignment
a, b, c = 10, 20.5, "Hello, World!"
print(a, b, c)Output
C:\Users\Your Name> python variable.py
10 20.5 Hello, World!One Value to Multiple Variables
Section titled “One Value to Multiple Variables”We can assign one value to multiple variables in one line. This is called one value to multiple variables.
Example:
Section titled “Example:”# One value to multiple variables
a = b = c = 10
print(a, b, c)Output
C:\Users\Your Name> python variable.py
10 10 10Multiple Values to One Variable
Section titled “Multiple Values to One Variable”We can assign multiple values to one variable in one line. This is called multiple values to one variable.
Example:
Section titled “Example:”# Multiple values to one variable
a = 10, 20, 30
print(a)Output
C:\Users\Your Name> python variable.py
(10, 20, 30)In this case, the variable a is a tuple.
Unpacking a Tuple
Section titled “Unpacking a Tuple”We can unpack a tuple in multiple variables. This is called unpacking a tuple.
Example:
Section titled “Example:”# Unpacking a tuple
a = 10, 20, 30
x, y, z = a
print(x, y, z)Output
C:\Users\Your Name> python variable.py
10 20 30Unpacking a List
Section titled “Unpacking a List”We can unpack a list in multiple variables. This is called unpacking a list.
Example:
Section titled “Example:”# Unpacking a list
a = [10, 20, 30]
x, y, z = a
print(x, y, z)Output
C:\Users\Your Name> python variable.py
10 20 30Extended Unpacking with *
Section titled “Extended Unpacking with *”When you do not know (or do not care) how many items are in the middle, a starred variable * captures the rest as a list. This is called extended unpacking.
numbers = [1, 2, 3, 4, 5]
first, *rest = numbers
print(first, rest) # 1 [2, 3, 4, 5]
first, *middle, last = numbers
print(first, middle, last) # 1 [2, 3, 4] 5Swapping Variables
Section titled “Swapping Variables”Multiple assignment gives Python an elegant one-line swap — no temporary variable needed. The right-hand side is built into a tuple first, then unpacked.
a, b = 5, 10
a, b = b, a
print(a, b) # 10 5Check yourself
Section titled “Check yourself”-
`a = b = []` then `a.append(1)`. What is `b`?
The right-hand side is evaluated once and both names are bound to that single list. Measured: `a is b` is True. `x, y = [], []` creates two lists instead.
pch.quizShowAnswer
B — `[1]` — The right-hand side is evaluated once and both names are bound to that single list. Measured: `a is b` is True. `x, y = [], []` creates two lists instead.
-
Why is `i = j = 0` harmless while `a = b = []` is a trap?
Sharing only matters when the shared object can change. Immutable values — numbers, strings, tuples — are safe to share because nothing can mutate them.
pch.quizShowAnswer
B — `0` cannot be modified, so sharing it can never be observed — Sharing only matters when the shared object can change. Immutable values — numbers, strings, tuples — are safe to share because nothing can mutate them.
-
`grid = [[0] * 3] * 3` then `grid[0][0] = 9`. What is `grid`?
Verified. `* 3` repeats the reference, not the row, so all three names point at one list. Build it with a comprehension instead: `[[0]*3 for _ in range(3)]`.
pch.quizShowAnswer
B — `[[9,0,0], [9,0,0], [9,0,0]]` — Verified. `* 3` repeats the reference, not the row, so all three names point at one list. Build it with a comprehension instead: `[[0]*3 for _ in range(3)]`.
-
Which statement creates two independent lists?
Only the tuple-unpacking form evaluates `[]` twice. The others all bind one object to two names.
pch.quizShowAnswer
B — `a, b = [], []` — Only the tuple-unpacking form evaluates `[]` twice. The others all bind one object to two names.
Try it: Multiple Assignment Exercises
Section titled “Try it: Multiple Assignment Exercises”Exercise 1 – Assign Same Value to Multiple Variables
Section titled “Exercise 1 – Assign Same Value to Multiple Variables”Exercise 2 – Tuple Unpacking
Section titled “Exercise 2 – Tuple Unpacking”Exercise 3 – Assign from a List
Section titled “Exercise 3 – Assign from a List”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading