Informatics Practices
Find the output of the following code:
a=5
b=2*a
a+=a+b
b*=a+b
print (a,b)
Python Funda
5 Likes
Answer
20 300
Working
a = 5
b = 2 * a
ais initialized to 5.bis set to2 * a, which means b = 2 * 5 = 10.
a += a + b
The += operator adds the value on the right to a and assigns the result back to a.
- Current values are:
- a = 5
- b = 10
- Calculation: a + b = 5 + 10 = 15
- Updating
a: a += 15 means a = a + 15 - So, a = 5 + 15 = 20.
b *= a + b
The *= operator multiplies b by the value on the right and assigns the result back to b.
- Current values are:
- a = 20
- b = 10
- Calculation: a + b = 20 + 10 = 30
- Updating
b: b *= 30 means b = b * 30 - So, b = 10 * 30 = 300.
print(a, b)
The final values of the variables are:
- a = 20
- b = 300
Hence, the output of the program is:
20 300
Answered By
1 Like
Related Questions
Find the output of the following code:
x=3 y=x+2 x+=y print(x,y)Find the output of the following code:
x=-2 y=2 x+=y y-=x print (x,y)Find the output of the following code:
p=10 q=20 p*=q/3 q+=p+q*2 print (p,q)Find the output of the following code:
p = 5 % 2 q = p ** 4 r = p // q p+= p + q + r r+= p + q + r q-= p + q * r print(p, q, r)