AlgoBazaAlgoBaza
Python for kids ยท Module 1

Lesson 3

Adding, subtracting, multiplying

After this lesson: You'll use Python as a calculator: add, subtract and multiply even very big numbers.

Video

?

The video for this lesson is coming soon.

Explanation

A computer calculates faster than the fastest mathematician in the world! See how to turn Python into a calculator.

Operation signs

OperationIn PythonExampleResult
addition+7 + 512
subtraction-20 - 812
multiplication* (a star!)6 * 742

Careful: we multiply with a star *, not the letter x. Spaces around the signs aren't required (7+5 works too), but they make the code easier to read.

When you put a calculation in print without quotes, Python first calculates it and then prints the result.

Python as a calculator

Every print first calculates, then prints the result.

0
789รท456ร—123-0=+
๐Ÿงพ the roll of results
โ€ฆ
?

Click the arrow - Python presses the keys like on a calculator.

Code
1print(7 + 5)
2print(20 - 8)
3print(6 * 7)
4print(5 - 12)
Screen
โ–
1/5 ยท line 1 runs next

The 7 times table

Every line calculates one multiplication. Watch the screen fill up.

๐Ÿ”ข 7:7

The number 7. Every print adds one row of 7 - count how many in all.

Code
1print("7 x 1 =", 7 * 1)
2print("7 x 2 =", 7 * 2)
3print("7 x 3 =", 7 * 3)
4print("7 x 4 =", 7 * 4)
5print("7 x 5 =", 7 * 5)
Screen
โ–
1/6 ยท line 1 runs next

Plus and minus

58 + 17 - 25
1/3 ยท click the arrow - Python works out the next part

Long calculations

One calculation can have many numbers and signs. When there are only additions and subtractions, Python goes from left to right - just like you do on paper:

From left to right

Click the arrow and watch Python calculate piece by piece.

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

Negative and huge numbers

The result can be negative: 5 - 12 is -7. And Python isn't afraid of huge numbers - it calculates 99999 * 99999 in a blink, and even numbers with a hundred digits!

Huge numbers

Code
1print(99999 * 99999)
2print(123456789 * 987654321)
Screen
โ–
1/3 ยท line 1 runs next
?

What will this program print?

print(3 * 4)
print("3 * 4")
?

What does print(20 - 5 - 5) print?

Common mistakes

  • print(6 x 7) - the letter x isn't multiplication, Python reports an error. Multiplication is *.
  • print("6 * 7") - in quotes the calculation isn't done.
  • A misread story: "how many more" is subtraction, not addition!

Shopping step by step

We buy 3 notebooks at 4 zl and a pen for 5 zl, and pay 20 zl. How much change? Multiplication goes first.

20 - 3 * 4 - 5
1/4 ยท click the arrow - Python works out the next part

Counting legs at the zoo

The zoo has 4 giraffes, 2 ostriches and 3 snakes. How many legs in total? ๐Ÿฆ’

โ€ฆ
?

Click the arrow - we'll count everything in the picture.

Code
1print("Giraffes:", 4 * 4)
2print("Ostriches:", 2 * 2)
3print("Snakes:", 3 * 0)
4print("Total:", 4 * 4 + 2 * 2 + 3 * 0)
Screen
โ–
1/5 ยท line 1 runs next

A bus on its route ๐ŸšŒ

20 people ride the bus. At the stops: 8 get off, 5 get on, 3 get off, 10 get on.

20 - 8 + 5 - 3 + 10
1/5 ยท click the arrow - Python works out the next part

A receipt from the shop ๐Ÿงพ

Every item is price times quantity. The sum comes at the end.

๐Ÿงพ SKLEPIK
โ€ฆ
?

Click the arrow - the till prints the receipt line by line.

Code
1print("Rolls:", 4 * 2)
2print("Milk:", 2 * 4)
3print("Apples:", 6 * 1)
4print("Total:", 4 * 2 + 2 * 4 + 6 * 1)
Screen
โ–
1/5 ยท line 1 runs next

Wheels in the car park ๐Ÿš—๐Ÿšฒ

5 cars (4 wheels each) and 7 bikes (2 wheels each).

โ€ฆ
?

Click the arrow - we'll count everything in the picture.

Code
1print("Cars:", 5 * 4)
2print("Bikes:", 7 * 2)
3print("Total:", 5 * 4 + 7 * 2)
Screen
โ–
1/4 ยท line 1 runs next

Presents under the tree ๐ŸŽ

3 cousins, each gets 2 presents, and the grandparents add 4 shared ones.

โ€ฆ
?

Click the arrow - we'll count everything in the picture.

Code
1print("Presents:", 3 * 2 + 4)
Screen
โ–
1/2 ยท line 1 runs next

The result can be negative

If you subtract more than you have, Python won't report an error - the result will be negative, with a minus in front: 5 - 8 is -3. Just like a thermometer in winter: 5 degrees, then 8 colder, is -3 degrees. ๐Ÿฅถ

Below zero

Code
1print(5 - 8)
2print(0 - 10)
3print(-3 + 3)
4print(-2 * 4)
Screen
โ–
1/5 ยท line 1 runs next

A lift ๐Ÿ›—

We're on floor 2 and go 4 floors down - to the garage.

3๐ŸชŸ ๐ŸชŸ
2๐ŸชŸ ๐ŸชŸ
1๐ŸชŸ ๐ŸชŸ
0๐Ÿšช ground
-1๐Ÿ…ฟ๏ธ garage
-2๐Ÿ…ฟ๏ธ garage
-3๐Ÿ…ฟ๏ธ garage

Click the arrow - the lift goes wherever the result says.

Code
1print("Start:", 2)
2print("After going down:", 2 - 4)
3print("And 1 more down:", 2 - 4 - 1)
Screen
โ–
1/4 ยท line 1 runs next

Solving a word problem

Task: Kate has 3 packs of 12 stickers. She gave her brother 7 stickers. How many does she have left?

  1. How many did she have? 3 packs of 12 is 3 * 12.
  2. How many did she give away? 7 - so we subtract: 3 * 12 - 7.
  3. Code: print(3 * 12 - 7)
  4. Check in my head: 36 - 7 = 29. โœ…
?

What does print(100 - 25 * 2) print?

?

Which sign means multiplication in Python?

Remember

  • Addition +, subtraction -, multiplication * (a star).
  • A calculation without quotes in print - Python prints the result.
  • Additions and subtractions go from left to right.
  • The result can be negative or huge - Python can handle it.

Let's go! In the tasks Python has to calculate the result - don't do it in your head, let the computer work.

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.