Lesson 10
Order of operations and powers
After this lesson: You'll know what Python calculates first and use brackets and powers.
Video
The video for this lesson is coming soon.
Explanation
What's 2 + 3 * 4? 20 or 14? Like in maths, Python multiplies first and adds afterwards.
Multiplication first
Click the arrow and watch Python calculate step by step.
Order of operations
Python calculates in this order:
- brackets
( ) - powers
** - multiplication and division
* / // % - addition and subtraction
+ -
Operations on the same level go from left to right. For example in 10 - 4 + 2 the subtraction comes first (it's on the left): 6 + 2 = 8.
Brackets change the order
The same numbers as above, but with brackets.
Multiplication and division from the left
Without brackets
With brackets
Zoo tickets 🦁
2 adult tickets at 30 zl and 3 child tickets at 15 zl. No brackets needed - multiplication goes first anyway.
Click the arrow - we'll work out every print step by step, like Python: brackets first, then multiplying, adding last.
1print(2 * 30 + 3 * 15)2print((2 + 3) * 15)3print(2 + 3 * 15)
▍Powers
A power is multiplying a number by itself several times. In Python we write it with two stars **:
2 ** 3= 2 * 2 * 2 = 8 ("two to the power of three"),5 ** 2= 5 * 5 = 25 ("five squared"),10 ** 6= 1000000 (a one and six zeros).
A power is calculated before multiplication and addition.
Powers in a program
1print(2 ** 3)2print(5 ** 2)3print(10 ** 6)4print(2 ** 10)
▍Power before multiplication
Two brackets
Rice on a chessboard 🍚
Twice as many grains on every next square. Watch how fast it grows!
1print("Square 1:", 2 ** 0)2print("Square 2:", 2 ** 1)3print("Square 8:", 2 ** 7)4print("Square 16:", 2 ** 15)5print("Square 32:", 2 ** 31)6print("Square 64:", 2 ** 63)
▍A thousand is 10 to the power of 3
Square flower beds 🌱
Bajtek plants flowers in square beds - as many flowers in each row as there are rows. How many flowers fit in a bed? The side to the power of 2! Watch every next square grow.
Click the arrow - every power builds its own shape.
1print("Bed 2 by 2:", 2 ** 2)2print("Bed 3 by 3:", 3 ** 2)3print("Bed 4 by 4:", 4 ** 2)4print("Bed 5 by 5:", 5 ** 2)
▍The average - a typical trap
Ola scored 8, 6 and 10 points in three quizzes. The average is the sum divided by the number of quizzes: (8 + 6 + 10) / 3.
It's tempting to write it without brackets: 8 + 6 + 10 / 3. But remember the order of operations? Just like in maths, Python divides first and only then adds. Without brackets it divides only the last number - 10 - by 3, and adds the 8 and 6 whole!
Ola's average on bars 🎬
Click Next to go step by step. The bars show Ola's points - see what an average is and why you get a wrong result without brackets.
Code
🖥️ Screen
Ola's quizzes
Ola scored 8, 6 and 10 points in three quizzes. She wants to know her average - how many points she'd have in each quiz if they were all equally good.
Average with and without brackets
The same difference in a program. Remember: when you divide a sum, always put it in brackets.
Click the arrow - we'll work out every print step by step, like Python: brackets first, then multiplying, adding last.
1print("Without brackets (wrong):", 8 + 6 + 10 / 3)2print("With brackets (right):", (8 + 6 + 10) / 3)
▍What does print(10 - 2 * 3) print?
What does print((10 - 2) * 3) print?
What is 2 ** 4?
Common mistakes
2 ^ 3is not a power in Python! A power is2 ** 3.2 * * 3with a space between the stars - an error. The stars must be together.- A forgotten bracket in an average:
a + b / 2isn't the same as(a + b) / 2.
The average of three grades 📒
Grades: 5, 4 and 6. First the sum in brackets, then divide by the number of grades.
With and without brackets
Click the arrow - we'll work out every print step by step, like Python: brackets first, then multiplying, adding last.
1print((5 + 4 + 6) / 3)2print(5 + 4 + 6 / 3)
▍Order of operations - just like in maths class
Python calculates in the same order as you do at school:
- brackets
( ), - powers
**, - multiplication and division
* / // %(left to right), - addition and subtraction
+ -(left to right).
Below Python works out a long calculation step by step - always picking the most important operation. 👇
Everything at once
Left to right
Subtraction and addition are equally important - then we go from the left.
Powers grow very fast
2 ** 10 is 2 multiplied by itself 10 times. See how fast it grows! Legend says the inventor of chess asked the king for grains of rice: 1 on the first square, 2 on the second, 4 on the third... The last square would hold 2 ** 63 grains - more than all the rice in the world! 🍚♟️
Doubling
1print(2 ** 1)2print(2 ** 5)3print(2 ** 10)4print(2 ** 63)
▍Area and volume
A square with side 7 and a cube with edge 3.
Click the arrow - every power builds its own shape.
1print("Square area:", 7 ** 2)2print("Cube volume:", 3 ** 3)
▍What does print(2 + 3 * 2 ** 2) print?
How do you calculate the average of 6 and 10?
Remember
- Order: brackets, powers, multiplication and division, addition and subtraction.
- On the same level - from left to right.
- A power is
**:2 ** 3= 8. - When unsure what goes first - add brackets.
When you're not sure what's calculated first - add brackets. They never hurt!
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.