nerdexam
Python_Institute

PCEP-30-02 · Question #85

What is the expected output of the following code? ``python x, y, z = 3, 2, 1 z, y, x = x, y, z print(x, y, z) ``

The correct answer is A. 1 2 3. Option A is correct because Python evaluates the entire right-hand side before any assignment occurs. When z, y, x = x, y, z runs, Python first captures the current values (x=3, y=2, z=1) as a tuple, then unpacks them left-to-right: z=3, y=2, x=1 - so print(x, y, z) outputs 1 2…

Question

What is the expected output of the following code?
x, y, z = 3, 2, 1
z, y, x = x, y, z
print(x, y, z)

Options

  • A1 2 3
  • B122
  • C321
  • D213

How the community answered

(21 responses)
  • A
    81% (17)
  • B
    10% (2)
  • C
    5% (1)
  • D
    5% (1)

Explanation

Option A is correct because Python evaluates the entire right-hand side before any assignment occurs. When z, y, x = x, y, z runs, Python first captures the current values (x=3, y=2, z=1) as a tuple, then unpacks them left-to-right: z=3, y=2, x=1 - so print(x, y, z) outputs 1 2 3.

Option C (321) is the most tempting trap - it's the original values of x, y, z and would be correct only if the swap line were absent. Option D (213) has no basis; no sequence of correct assignments produces that order. Option B (122) is nonsensical - no value of 2 appears twice, making it easy to eliminate immediately.

Memory tip: Think of tuple unpacking like a temp variable swap - Python "takes a snapshot" of the right side all at once before touching any variable on the left, so simultaneous assignments never clobber each other mid-operation.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice