Python evaluates a mixed arithmetic expression according to a fixed precedence ranking. Arrange these levels from highest priority (evaluated first) to lowest priority (evaluated last).
1.
Unary minus, as in -x
2.
Exponentiation **
3.
Addition and subtraction + and -
4.
Multiplicative operators *, /, // and %
5.
Parentheses ( )
Unlock full course notes by signing up
Converting seconds with floor division and modulo
A fitness app stores a workout's length as a whole number of seconds and converts it for display with this Python code:
Give the values stored in hours, minutes and secs, each as an integer.
Right-associative powers and negative floor division
A programmer writes the following line of Python code while testing how operators combine:
What value is stored in result? Give your answer as an integer.
Checking successive percentage reductions
A hypothetical energy model starts with 560 Wh, subtracts a fixed loss of 32 Wh, and then applies two successive 15% reductions. Each reduction acts on the energy remaining after the preceding step. The final energy is shared equally among six devices. Assume the model's values are exact and retain full precision until each final answer.
A programmer writes (560 - 32) * (1 - 15 / 100 * 2) / 6, which combines the two reductions incorrectly.
Find the intended energy per device and the energy per device calculated by the programmer's expression, both in Wh to two decimal places. Then find how far the programmer's result falls below the intended result, expressed as a percentage of the intended result, to two decimal places.