Thursday , September 19 2024

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

Questions BankCategory: Python Essentials 1What is the output of the following piece of code? x = 1 y = 2 x, y, z = x, x, y z, y, z = x, y, z print(x, y, z)
What is the output of the following piece of code?

x = 1
y = 2
x, y, z = x, x, y
z, y, z = x, y, z
 
print(x, y, z)
  • 2 1 2
  • 1 2 2
  • 1 2 1
  • 1 1 2

Explanation: Let’s analyze the example:

  • x = 1 and y = 2
  • as a result of the first multiple variable assignment (Line 3), x = 1, y = 1, and z = 2
  • as a result of the second multiple variable assignment (Line 4), z = x = 1, y = y = 1, and z = z = 2 (because the original assignment from the previous line now overwrites the first z assignment from the current line)
  • the result printed to the screen is therefore 1 1 2

More Questions: Python Essentials 1 (PE1) Course Final Test