Lists & Tuples · Question 7 · Easy

Two variables are exchanged using tuple packing and unpacking. What is printed?

a, b = 5, 9
a, b = b, a
print(a, b)
  1. 5 9
  2. 9 9
  3. 5 5
  4. 9 5
Answer and explanation

Answer: D. 9 5

Explanation: The right side b, a is packed into the tuple (9, 5) before any assignment happens. It is then unpacked into a and b, so the values are swapped.

Previous: An element of a tuple is assigned a new value. What happens? · Next: The same list goes through sorted() and then sort(). What does this print?

Practise lists & tuples

Try a fresh set of lists & tuples questions with an explanation after every answer, free and without signing up: Lists & Tuples practice. Or sit a full placement mock test.

More lists & tuples questions

More python practice

Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge