Incrementation in python (a*=b)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Incrementation in python (a*=b)



Q.) WRITE THE OUTPUT OF THE FOLLOWING:


a=12
b=7.4
c=1
a-=b
print(a,b) # I understood this much
a*=2+c
print(a)



My answer is 10.2 whereas the answer should be 13.799999999999999





Do you have a question? What part don't you understand? Are you supposed to do this in your head or use an interpreter?
– Patrick Haugh
Aug 10 at 14:40





I just ran through your code and got 13.79 repeating, not sure what your issue is.
– The Tesseract's Shadow
Aug 10 at 14:40




1 Answer
1



The *= augmented assignment will evaluate all of the right hand side and then multiply the left hand side by the result. You are confusing operator priorities here and think that the addition happens after the multiplication but that isn't the case.


*=



So before the augmented assignment a is 4.6 and c is 1, then a *= 2+c is the same as a *= 3 which is a = a * 3 or about 13.8 (repr is 13.79999... but if you use print it will round to a sensible number).


a


4.6


c


1


a *= 2+c


a *= 3


a = a * 3


13.8


repr


print






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard