0

[SOLVED] What is the order of the operators for this? Can't figure it out. Thank you.

https://code.sololearn.com/cBq0vOq2x7JV/?ref=app

12th Jun 2017, 7:25 AM
Ingvar Karsten
Ingvar Karsten - avatar
4 odpowiedzi
+ 16
The statement is: print(int(1+2-3*4/5%6)) *, /, //, % are given higher precedence over + and -, and associativity is from left to right, which means that operations with same precedence operators are solved left to right. Considering the part 3*4/5%6, since these operators have higher precedence, and solving it left to right, 3*4=12 12/5=2.4 2.4%6=2.4 3*4/5%6 = 12/5%6 = 2.4%6 = 2.4 Therefore 1+2-3*4/5%6 = 1+2-2.4 = 3-2.4 = 0.6 And since 0.6 is being converted to an integer, the decimal part is truncated and you're left with 0. Hope this helped ☺
12th Jun 2017, 7:42 AM
Sheena Singh
Sheena Singh - avatar
+ 2
1 + 2 - (((3*4) / 5) % 6) Which equals 0.6, you were using an int which outputs 0 instead of a float.
12th Jun 2017, 7:40 AM
Tim
+ 1
Thank you both for answers, my mistake was I didn't realize that 12/5 will return float 2.4 and not just int 2. O:-)
12th Jun 2017, 7:55 AM
Ingvar Karsten
Ingvar Karsten - avatar
+ 1
thanks sheena...it was very usefull
13th Jun 2017, 3:59 AM
Ravi Prasad