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
1 Réponse
+ 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