AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Planning and testing code

Week 3 · Unit 7.1: Block it out · 3 lessons
2

Order of operations

Computing 7: Unit 7.1, pp. 8–43
GoalUse BIDMAS to predict the result of a Python calculation and add brackets to change it.
New words
BIDMAS · the order of operations: brackets, indices, division, multiplication, addition, subtractionindex · a power: 2 ** 3 means 2 × 2 × 2, which is 8operator · a symbol such as + or * that tells the computer what calculation to do
Explanation

Python follows the same order as maths. Brackets are worked out first, then indices, then division and multiplication, and last addition and subtraction. Multiplication and division have the same rank, and so do addition and subtraction. When the rank is the same, Python goes from left to right. In Python the index sign is ** and the division sign is /. Division always gives a real number, such as 4.0. If a program gives a surprising answer, the order is often the reason.

LetterMeaningIn Python
Bbrackets, worked out first( )
Iindices (powers)**
Ddivision/
Mmultiplication*
Aaddition+
Ssubtraction−
Worked examples
StepWhat Python doesResult so far
16 / 23.0
23.0 * 39.0
38 + 9.017.0
Here is how Python works out 8 + 6 / 2 * 3. The division and the multiplication come before the addition, and they go from left to right.
Python
1print(120 + 40 / 4)
2print((120 + 40) / 4)
Output
130.0
40.0
A family of four share the costs of a trip: fuel costs 120 and the tolls cost 40. The first line is wrong, because only the tolls are divided by 4. Brackets in the second line make the addition happen first.
CalculationAnswer
18 - 6 / 316.0
(18 - 6) / 34.0
4 + 2 ** 312
(4 + 2) ** 3216
20 - 5 - 312
Brackets can completely change an answer. Compare these calculations.
Class activity

BIDMAS relay (no computer): write six calculations on strips of paper, for example 9 - 3 * 2 and 24 / 4 + 2 ** 2. Mix them up. A partner picks a strip, circles the operation that comes first, and then writes each step until they have the answer. You check with a calculator, or on paper. Then make a hard strip: use only the numbers 2, 3, 4 and 6 and add one pair of brackets so that the answer changes. If you have a computer or tablet, type the calculations into the Python shell with an adult.

Practice
1
Fill in the answers that the shell shows (the question marks).
You type at >>>The shell shows
5 + 4 * 2?
(5 + 4) * 2?
20 - 8 / 4?
2 ** 4 - 6?
2
Mia types 2 + 4 * 6 because she wants to add first and then multiply by 6, but the shell shows 26. (a) Explain why. (b) Add one pair of brackets so that it shows 36. (c) What does 30 - 6 / 3 show, and what does (30 - 6) / 3 show?
Python
1print(2 + 4 * 6)
Output
26
3
What does this program show? Then change line 3 so that it shows the total number of sweets after 6 more are added, divided between the friends.
Python
1sweets = 12
2friends = 3
3print(sweets + 6 / friends)
4
Write one line of Python for: add 15 and 25, divide the result by 5, and then add 3. What does it show?