KnowledgeBoat Logo
|

Informatics Practices

Find the output of the following code:

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

Python Funda

4 Likes

Answer

8 5

Working

x = 3
y = x + 2
  • x is initialized to 3.
  • y is set to x + 2, which means y = 3 + 2 = 5.
x += y

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

  • Current values are:
    • x = 3
    • y = 5
  • Calculation: x + y = 3 + 5 = 8
  • Updating x: x += 5 means x = x + 5
  • So, x = 3 + 5 = 8.
print(x, y)

The final values of the variables are:

  • x = 8
  • y = 5

Hence, the output of the program is:

8 5

Answered By

2 Likes


Related Questions