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
| Operation | In Python | Example | Result |
|---|---|---|---|
| addition | + | 7 + 5 | 12 |
| subtraction | - | 20 - 8 | 12 |
| multiplication | * (a star!) | 6 * 7 | 42 |
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.
Click the arrow - Python presses the keys like on a calculator.
1print(7 + 5)2print(20 - 8)3print(6 * 7)4print(5 - 12)
โThe 7 times table
Every line calculates one multiplication. Watch the screen fill up.
The number 7. Every print adds one row of 7 - count how many in all.
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)
โPlus and minus
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.
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
1print(99999 * 99999)2print(123456789 * 987654321)
โ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.
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.
1print("Giraffes:", 4 * 4)2print("Ostriches:", 2 * 2)3print("Snakes:", 3 * 0)4print("Total:", 4 * 4 + 2 * 2 + 3 * 0)
โ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.
A receipt from the shop ๐งพ
Every item is price times quantity. The sum comes at the end.
Click the arrow - the till prints the receipt line by line.
1print("Rolls:", 4 * 2)2print("Milk:", 2 * 4)3print("Apples:", 6 * 1)4print("Total:", 4 * 2 + 2 * 4 + 6 * 1)
โ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.
1print("Cars:", 5 * 4)2print("Bikes:", 7 * 2)3print("Total:", 5 * 4 + 7 * 2)
โ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.
1print("Presents:", 3 * 2 + 4)
โ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
1print(5 - 8)2print(0 - 10)3print(-3 + 3)4print(-2 * 4)
โA lift ๐
We're on floor 2 and go 4 floors down - to the garage.
Click the arrow - the lift goes wherever the result says.
1print("Start:", 2)2print("After going down:", 2 - 4)3print("And 1 more down:", 2 - 4 - 1)
โSolving a word problem
Task: Kate has 3 packs of 12 stickers. She gave her brother 7 stickers. How many does she have left?
- How many did she have? 3 packs of 12 is
3 * 12. - How many did she give away? 7 - so we subtract:
3 * 12 - 7. - Code:
print(3 * 12 - 7) - 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.