Skip to content
VLSI Mentor

Python Part I · Module 2 · Variables and Data Types

Assigning Several Variables at Once

Python lets you assign several names on one line, which is occasionally very useful and occasionally a way to hide what a line is doing. This chapter covers the three forms you will meet, starting with the ordinary one — a row of names on the left, a row of values on the right, matched up in order. From that single rule the well-known one-line swap falls out for free, and it works for exactly the reason the previous chapter gave: Python works out the entire right-hand side before it assigns anything. The chapter finishes with the judgement half, because the compact form is genuinely better for two or three closely related values and genuinely worse for five unrelated ones.

Foundation7 min readPythonPart IFundamentalsAssignmentReadability

Part I · Module 2 · Chapter 2.5 · Assigning Several Variables at Once

1. Several Names, Several Values

You already know how to do this the long way:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
test_name = "axi_smoke"
seed = 4210

Python also allows both on one line:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
test_name, seed = "axi_smoke", 4210
print(test_name)
print(seed)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
axi_smoke
4210

The rule is positional: the names on the left are matched with the values on the right, left to right. First name gets first value, second name gets second value.

The counts must match. If they do not, Python says so:

Azvya Education Pvt. Ltd.VLSI Mentor
BROKEN ON PURPOSE — two names, three values
a, b = 1, 2, 3
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
ValueError: too many values to unpack (expected 2)

That message is worth reading rather than skipping. "Expected 2" is the number of names you wrote; Python found three values to hand out and had nowhere to put the third.

2. The Swap

Here is the form that earns the syntax its place. Suppose two names refer to two values and you want them exchanged.

Doing it the obvious way does not work:

Azvya Education Pvt. Ltd.VLSI Mentor
BROKEN ON PURPOSE — this does not swap anything
first = "A"
second = "B"

first = second
second = first

print(first)
print(second)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
B
B

Both ended up as "B". Line first = second pointed first at "B", and by the time the next line ran, the original "A" had no name referring to it — so second = first just pointed second at "B" as well.

The traditional fix is a third name to hold one value while the other moves. Python does not need one:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
first = "A"
second = "B"

first, second = second, first

print(first)
print(second)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
B
A

Why it works is exactly the rule from Chapter 2.3 §4: Python works out the entire right-hand side first. At that moment second is "B" and first is "A", so the right-hand side becomes those two values. Only then are the names on the left pointed at them. Neither name has moved while the other was being read.

3. One Value, Several Names

A third form, which chains:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
errors = warnings = fatals = 0
print(errors)
print(warnings)
print(fatals)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
0
0
0

All three names now refer to the same value, 0. This is a reasonable way to set several counters to the same starting point.

4. When to Use It

The compact forms are a readability tool, and like any such tool they can be used against the reader.

Reasonable:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
test_name, seed = "axi_smoke", 4210

Two closely related facts about the same thing, set up together. A reader takes it in at a glance.

Not reasonable:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
test_name, seed, run_dir, passed, error_count = "axi_smoke", 4210, "runs/smoke", False, 0

Legal, and considerably harder to read than five ordinary lines. To check what run_dir is, you have to count positions on both sides and hope you counted right. A reviewer will not.

The practical rule: two, occasionally three. Past that, use separate lines. The line is doing you no favours once you have to count.

5. Predict, Run, Observe

Predict all output before running.

(a)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
a, b = 1, 2
print(a)
print(b)

(b)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
a, b = 1, 2
a, b = b, a
print(a)
print(b)

(c)

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
x = y = 5
x = 9
print(x)
print(y)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
(a) 1, 2      (b) 2, 1      (c) 9, 5

For (c), check your reasoning: x = 9 pointed x at a new value. y was never mentioned on that line, so it still refers to 5 — the same independence Chapter 2.3 established.

6. Exercise

Exercise 1 — set up a run. Using one line, give names to a test name and a seed. Print both. Then add a third fact on a separate line and say in one sentence why you did not extend the first line to three.

Exercise 2 — swap and explain. Write the swap from §2. Then explain in two sentences why the three-line version fails, using the phrase "right-hand side".

Exercise 3 — predict the error. Predict the exact error, then run it:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
a, b, c = 1, 2

What does the number in the message refer to — the names or the values?

Exercise 4 — judge the line. Which of these would you accept in a code review, and why?

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
low, high = 0, 100
a, b, c, d = 1, "two", True, None
first = second = third = 0

7. Summary

  • Names on the left, values on the right, matched in order. The counts must agree or Python raises a ValueError.
  • a, b = b, a swaps two values with no temporary name, because the whole right-hand side is worked out before anything is assigned.
  • The naive three-line swap fails, and understanding why is a good test of the model from Chapter 2.3.
  • x = y = z = 0 points several names at one value — safe for every value type in this module.
  • Two or three related values, at most. Past that the line stops helping the reader and starts costing them.

Assignment in all its forms is now covered. The remaining chapters of Module 2 are about the values themselves — starting with the two kinds of number.

Where this fits

Part of the Python curriculum.