The big ideaPython is a calculator. It multiplies before it adds, and brackets go first.
How it works
Python is a calculator. Plus adds, minus takes away, a star multiplies and a slash divides.
Python multiplies before it adds. That is called the order of operations.
Brackets tell Python to work that part out first.
A slash always gives a decimal, even when the answer is a whole number, so 8 / 2 shows 4.0.
What each line makes. Each bar is the number the line shows.
Example 1: Multiply first
Zara types print(2 + 3 * 4). Leo works left to right and says twenty.
Python does the star first: 3 * 4 = 12.
Then it adds the two: 2 + 12 = 14.
The screen shows 14, and Python was right all along.
Example 2: Brackets first
Zara puts brackets round the first part: print((2 + 3) * 4).
Brackets go first: 2 + 3 = 5.
Then Python multiplies: 5 * 4 = 20.
The screen shows 20.
Example 3: Take away and multiply
The line is print(9 - 3 * 2).
The star goes first: 3 * 2 = 6.
Then Python takes away: 9 - 6 = 3.
The screen shows 3.
Example 4: A slash
Bo asks how to share eight cupcakes between two plates, so Zara types print(8 / 2).
Eight shared between two plates is four each.
A slash always gives a decimal, so the screen shows 4.0.
Example 5: A mix-up to fix
The line print(3 x 4) does not work, because Python multiplies with a star, not an x.
The mix-up to fix: print(3 * 4).
Now the screen shows 12.
Words to know
order of operations
Which part of a sum Python works out first.
brackets
Make Python work that part out first.
star
The sign that multiplies.
slash
The sign that divides. It always shows a decimal.
Watch out
Python multiplies before it adds, even when the star comes second. In 2 + 3 * 4 the star goes first.
Use a star to multiply. An x does not work.
A number in quotes is text, so print("2" + 3) has a mix-up to fix.
A slash always shows a decimal, so 8 / 2 shows 4.0, not 4.
Try it
What does print(10 - 4) show?
What does print(2 + 3 * 4) show?
What does print((2 + 3) * 4) show?
What does print(8 / 2) show?
True or false: print(3 x 4) works in Python.
Answers: 1. 6 · 2. 14 · 3. 20 · 4. 4.0 · 5. False
Try at homeWith a grown-up, write three sums on paper using plus, minus and a star, like two plus three times four. Say which part Python would work out first, then check with a real calculator. Add brackets to one sum and see if the answer changes.
For grown-upsWrite three sums on paper using plus, minus and a star, like two plus three times four. Ask which part Python would work out first, then check with a real calculator. Add brackets to one sum and see what changes.
Open these notes on screenScan the code, or go to tutorpip.com.