Order of operations with brackets in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Order of operations with brackets in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly finds the average of a and b?
A average = (a + b) / 2
B average = a + b / 2
2. What does this print? print(2 + 3 * 4)
A 14
B 20
3. What does this print? print((2 + 3) * 4)
A 14
B 20
4. Which statement about Python's order of operations is true?
A Brackets are worked out before **, * and /
B Addition is always worked out before multiplication
5. A student wants the average of three marks. Which line is correct?
A avg = m1 + m2 + m3 / 3
B avg = (m1 + m2 + m3) / 3

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1(a + b) / 2 AThe brackets are missing, so only b is divided by 2. It should be average = (a + b) / 2.
2a + b / 2 BDivides b by 2 first, then adds a, so it does not give the average of a and b
3Brackets are worked out first, so they can force an addition to happen before a multiplication CAdds a and b first because of the brackets, then divides the total by 2 to find the average
4Python works out ** before * / // % and before + -, so the power is calculated first DThree values are added but divided by 2. The total should be divided by 3 to find the average of three numbers.
5average = a + b / 2 Eresult = 2 + 3 ** 2
6average = (a + b + c) / 2 Ftotal = (3 + 4) * 2

Definitions

(a + b) / 2

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
average = (a + b) _ 2
2.
total = (5 + 3) _ 2 print(total)
3.
avg = (m1 + m2 + m3) / _
4.
print(_2 + 3) * 4)
5.
print((2 + 3) __ 2)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A a, b = 80, 90
B average = a + b / 2
C print(average)
Question 2
A total = 5 + 3 * 2
B print(total)
C print(total == 16)
Question 3
A english = 70
B maths = 90
C average = english + maths / 2
D print(average)
Question 4
A square = 3 + 2 ** 2
B print(square)
C print(square == 25)
Question 5
A ticket = 12
B total = ticket + 3 * 4
C print("Total:", total)

Answer Key

Order of operations with brackets in Python
Section A: Quick Questions
Q1
A — average = (a + b) / 2
Q2
A — 14
Q3
B — 20
Q4
A — Brackets are worked out before **, * and /
Q5
B — avg = (m1 + m2 + m3) / 3
Section B: Match the Pairs
Q1
(a + b) / 2 → Adds a and b first because of the brackets, then divides the total by 2 to find the average
Q2
a + b / 2 → Divides b by 2 first, then adds a, so it does not give the average of a and b
Q3
Brackets are worked out first, so they can force an addition to happen before a multiplication → total = (3 + 4) * 2
Q4
Python works out ** before * / // % and before + -, so the power is calculated first → result = 2 + 3 ** 2
Q5
average = a + b / 2 → The brackets are missing, so only b is divided by 2. It should be average = (a + b) / 2.
Q6
average = (a + b + c) / 2 → Three values are added but divided by 2. The total should be divided by 3 to find the average of three numbers.
Section C: Definitions
Q1
(a + b) / 2 — Python works out brackets first, so (a + b) / 2 adds a and b before dividing by 2. Without the brackets, the division would happen first and the average would be wrong.
Section D: Fill in the Blanks
Q1
average = (a + b) / 2
Q2
total = (5 + 3) * 2 print(total)
Q3
avg = (m1 + m2 + m3) / 3
Q4
print((2 + 3) * 4)
Q5
print((2 + 3) ** 2)
Section E: Spot the Error
Q1
B — Without brackets, Python divides b by 2 first and then adds a, so the result is 125.0, not 85.0. It should be average = (a + b) / 2.
Q2
A — Multiplication happens before addition, so this gives 11, not 16. Brackets are needed to add first: total = (5 + 3) * 2.
Q3
C — Division is done before addition, so only maths is halved and the result is 115.0. It should be average = (english + maths) / 2.
Q4
A — The ** operator runs before +, so this calculates 3 + 4 = 7. Brackets are needed to add first: square = (3 + 2) ** 2 gives 25.
Q5
B — Multiplication happens first, so this gives 12 + 12 = 24, not 60. Brackets are needed: total = (ticket + 3) * 4.