Informatics Practices

Find the output of the following code:

x=-2
y=2
x+=y
y-=x
print (x,y)

Python Funda

3 Likes

Answer

0 2

Working

x += y

The += operator adds the value of y to x and assigns the result back to x.

  • Current values are:
    • x = -2
    • y = 2
  • Calculation: x + y = -2 + 2 = 0
  • Updating x: x += 2 means x = x + 2
  • So, x = -2 + 2 = 0.
y -= x

The -= operator subtracts the value of x from y and assigns the result back to y.

  • Current values are:
    • x = 0
    • y = 2
  • Calculation: y - x = 2 - 0 = 2
  • Updating y: y -= 0 means y = y - 0
  • So, y = 2 - 0 = 2.
print(x, y)

The final values of the variables are:

  • x = 0
  • y = 2

Hence, the output of the program is:

0 2

Answered By

2 Likes


Related Questions