Skip to content

Question 2

The following Python expression is entered into a fresh Python shell with no prior import statements. Determine the result from evaluating the following expression. [3 marks]

print(3 + 20 / 4 * 12)
Solution

Since the division / and * operators have a higher precedence level than the addition + operator, we compute using those operations from left to right first before addressing the + operation.

3 + 20 / 4 * 12
= 3 + 5.0 * 12      # 20 / 4 = 5.0
= 3 + 60.0          # 5.0 * 12 = 60.0
= 63.0
Note

The answer does not equate to simply 63, but 63.0. This is because when using the regular divison / operator, the end result turns into a floating-point value. From here, that value type sticks around for the two operations following that.

However, if the question asks for 3 + 20 // 4 * 12, which involves the integer divison // operator, then we can compute the result as just 63 as an integer value.

Answer
63.0