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
Identify invalid variable names from the following, giving reason for each:
Group, if, total marks, S.I., volume, tot_strength, #tag, tag, 9a
Find the output of the following code:
x=3 y=x+2 x+=y print(x,y)Find the output of the following code:
a=5 b=2*a a+=a+b b*=a+b print (a,b)Find the output of the following code:
p=10 q=20 p*=q/3 q+=p+q*2 print (p,q)