Share a pizza
8 slices for 4 friends: 8 / 4 is 2 slices each.
03OPERATORS ยท MATH
An operator is a sign that does a job with two values. Python has seven math signs. Three are new: //, %, and **.
IN REAL LIFE
Sharing, making groups, and counting tiles are math with operators.
8 slices for 4 friends: 8 / 4 is 2 slices each.
7 candies for 2 kids: 3 each, and 1 is left over.
A floor 3 tiles long and 3 wide has 3 ร 3 = 9 tiles.
THE MATH SIGNS
| Sign | It does | Example | Answer |
|---|---|---|---|
+ | Adds | 7 + 2 | 9 |
- | Takes away | 7 - 2 | 5 |
* | Multiplies | 7 * 2 | 14 |
/ | Divides, with decimals | 7 / 2 | 3.5 |
// | Divides, whole part only | 7 // 2 | 3 |
% | Gives what is left over | 7 % 2 | 1 |
** | Power: times itself | 7 ** 2 | 49 |
Remember: You met + - * / in Changing boxes. The new ones are // for the whole part, % for the leftover, and ** for power.
STEP BY STEP
Python can tell how many each kid gets and how many are left.
candies = 77 candies.kids = 22 kids.each = candies // kids7 // 2 is 3. Each kid gets 3.left = candies % kids7 % 2 is 1. One candy is left over.print(each, left)Shows 3 1.RUN IT
Guess first. Then run real Python and check.
candies = 7
kids = 2
print(candies // kids)
print(candies % kids)What will Python show?
PyBot's hint
Ready. Press Run Python.
Pyodide 314.0.3 runs CPython in your browser with WebAssembly.
Open-source under the Mozilla Public License 2.0. License
Now change one thing: Change 7 to 10 candies. Then try kids = 3.
YOUR TURN
Try each tiny question. A wrong answer becomes something to review.
How many full teams, and how many kids are left?
print(9 / 2)What will Python show?
print(9 // 2)What will Python show?
print(10 % 3)What will Python show?
print(8 % 2)What will Python show?
print(3 ** 2)What will Python show?
print(2 ** 3)What will Python show?
Which line gives the stickers left over?
PyBot shares 12 cookies with 3 friends. It wants a whole number, but it shows 4.0. Change one sign so it shows 4.
4OUTPUT
Want a fresh start? Clear only this page.
// gives the whole part, % the leftover, and ** the power. Next: signs that compare.