Why is the execution of this code 13.0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the execution of this code 13.0?

calculation = 5 + 15 / 5 + 3 * 2 - 1 print(calculation) I don't get why the answer is 13.0

7th Nov 2018, 1:30 AM
Eddy
Eddy - avatar
1 Answer
+ 4
The / and the * operators have the same precedence, and the + and the - operators have the same, but lower precedence. Operators of the same precedence are evaluated left-to-right. So 5 + 15 / 3 + 3 * 2 - 1 = 5 + 3.0 + 3 * 2 - 1 = 5 + 3.0 + 6 - 1 = 8.0 + 6 - 1 = 14.0 - 1 = 13.0 Also know that the division operator in Python 3 always results in float. And for the + and - operators, the arguments are first converted to the common type, which is float in this case. Reference for operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence
7th Nov 2018, 1:49 AM
Kishalaya Saha
Kishalaya Saha - avatar