Informatics Practices
Find the output of the following code:
p=10
q=20
p*=q/3
q+=p+q*2
print (p,q)
Python Funda
12 Likes
Answer
66.66666666666667 126.66666666666667
Working
p *= q / 3
The *= operator multiplies p by the value on the right and assigns it to p again.
- First, compute the division: q / 3 = 20 / 3 ≈ 6.6667
- p *= 6.6667 means p = p * 6.6667
- So, p = 10 * 6.6667 = 66.6667
q += p + q * 2
The += operator adds the value on the right to q and assigns it to q again.
- First, compute q * 2 = 20 * 2 = 40
- Then add values: p + q * 2 = 66.6667 + 40
- Finally, add it to
q: q += 106.6667 means q = q + 106.6667 - So, q = 20 + 106.6667 = 126.6667
print(p, q)
The variables now have these final values:
- p = 66.6667
- q = 126.6667
The precision difference is due to floating-point arithmetic. For simplicity of explanation we have taken lesser precision values.
Answered By
4 Likes
Related Questions
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:
a=5 b=2*a a+=a+b b*=a+b print (a,b)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)Find the output of the following code:
p = 21 // 5 q = p % 4 r = p * q p+= p + q - r r*= p - q + r q+= p + q print(p, q, r)