Informatics Practices

What will be the output of the following snippet?

x, y = 2, 6
x, y = y, x + 2
print(x, y)
  1. 6 6
  2. 4 4
  3. 4 6
  4. 6 4

Python Funda

2 Likes

Answer

6 4

Reason — Initially, x is assigned the value 2, and y is assigned 6 using multiple assignments. Then, the expression y, x + 2 is evaluated, where y is 6, and x + 2 calculates to 4. After evaluating the right-hand side, x is reassigned to y, making x equal to 6, and y is reassigned to x + 2, making y equal to 4. Thus, when print(x, y) executes, it outputs 6 4, showing the final values after the assignments.

Answered By

1 Like


Related Questions