AlgoBazaAlgoBaza
Python for kids · Module 1

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.

2 + 3 * 4
1/3 · click the arrow - Python works out the next part

Order of operations

Python calculates in this order:

  1. brackets ( )
  2. powers **
  3. multiplication and division * / // %
  4. 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.

(2 + 3) * 4
1/3 · click the arrow - Python works out the next part

Multiplication and division from the left

20 // 4 * 2
1/3 · click the arrow - Python works out the next part

Without brackets

3 + 4 * 2
1/3 · click the arrow - Python works out the next part

With brackets

(3 + 4) * 2
1/3 · click the arrow - Python works out the next part

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.

Code
1print(2 * 30 + 3 * 15)
2print((2 + 3) * 15)
3print(2 + 3 * 15)
Screen
▍
1/4 · line 1 runs next

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

Code
1print(2 ** 3)
2print(5 ** 2)
3print(10 ** 6)
4print(2 ** 10)
Screen
▍
1/5 · line 1 runs next

Power before multiplication

1 + 2 * 3 ** 2
1/4 · click the arrow - Python works out the next part

Two brackets

(1 + 2) * (3 + 4)
1/4 · click the arrow - Python works out the next part

Rice on a chessboard 🍚

Twice as many grains on every next square. Watch how fast it grows!

Code
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)
Screen
▍
1/7 · line 1 runs next

A thousand is 10 to the power of 3

10 ** 3
1/4 · click the arrow - Python works out the next part

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.

Code
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)
Screen
▍
1/5 · line 1 runs next

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.

the average must be here (6-10)
best score: 10
average = 8
8
quiz 1
6
quiz 2
10
quiz 3

Code

🖥️ Screen

1?

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.

Step 1 / 8

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.

Code
1print("Without brackets (wrong):", 8 + 6 + 10 / 3)
2print("With brackets (right):", (8 + 6 + 10) / 3)
Screen
▍
1/3 · line 1 runs next
?

What does print(10 - 2 * 3) print?

?

What does print((10 - 2) * 3) print?

?

What is 2 ** 4?

Common mistakes

  • 2 ^ 3 is not a power in Python! A power is 2 ** 3.
  • 2 * * 3 with a space between the stars - an error. The stars must be together.
  • A forgotten bracket in an average: a + b / 2 isn'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.

(5 + 4 + 6) / 3
1/4 · click the arrow - Python works out the next part

With and without brackets

…
?

Click the arrow - we'll work out every print step by step, like Python: brackets first, then multiplying, adding last.

Code
1print((5 + 4 + 6) / 3)
2print(5 + 4 + 6 / 3)
Screen
▍
1/3 · line 1 runs next

Order of operations - just like in maths class

Python calculates in the same order as you do at school:

  1. brackets ( ),
  2. powers **,
  3. multiplication and division * / // % (left to right),
  4. 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

(2 + 3) * 4 ** 2 - 10
1/5 · click the arrow - Python works out the next part

Left to right

Subtraction and addition are equally important - then we go from the left.

10 - 4 + 2
1/3 · click the arrow - Python works out the next part

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

Code
1print(2 ** 1)
2print(2 ** 5)
3print(2 ** 10)
4print(2 ** 63)
Screen
▍
1/5 · line 1 runs next

Area and volume

A square with side 7 and a cube with edge 3.

…
?

Click the arrow - every power builds its own shape.

Code
1print("Square area:", 7 ** 2)
2print("Cube volume:", 3 ** 3)
Screen
▍
1/3 · line 1 runs next
?

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.