Find a course

Module 2: Math, Text and InputLesson 3 of 1012 min

Math With Operators

Use Python as a calculator with +, -, *, /, //, % and **, follow the order of operations, and solve real problems like splitting teams.

Step 1 of 13Learn

Python as a calculator

Your class of 29 students needs to split into teams of 4 for a robotics challenge. How many full teams, and how many students are left over? You could work it out by hand, but Python answers instantly, and the same code works for any class size.

Python's arithmetic operators:

OperatorMeaningExampleResult
+add7 + 310
-subtract7 - 34
*multiply7 * 321
/divide7 / 23.5
//floor division7 // 23
%modulo (remainder)7 % 21
**power2 ** 532

Two surprises. First, / always gives a float, even when the division is exact: 10 / 2 is 5.0, not 5. Second, multiplication uses *, never x, and powers use **. The ^ symbol exists in Python but means something completely different, so 2 ^ 3 does not give 8.

For the robotics teams: 29 // 4 is 7 full teams, and 29 % 4 is 1 student left over.

Step 2 of 13Try the calculator

Floor division and remainder

Try different class sizes and team sizes. Check that teams × team size + leftover always gets you back to the class size.

n / k
7.25
Full teams: n // k
7
Left over: n % k
1
Check: (n // k) * k + n % k
29

Step 3 of 13Practice · Work it out

A video is 437 seconds long. Using 437 // 60 and 437 % 60, how many seconds are left over after the full minutes? (Type just the seconds.)

Step 4 of 13Learn

Order of operations

Python follows the same order of operations as math class, often remembered as PEMDAS:

  1. Parentheses first
  2. Exponents (**)
  3. Multiplication and division (*, /, //, %), left to right
  4. Addition and subtraction, left to right

So 2 + 3 * 4 is 14, not 20, because the multiplication happens first. When in doubt, add parentheses. They never hurt, and they make your intention obvious.

A classic bug: computing an average.

test1 = 80
test2 = 90
average = test1 + test2 / 2
print(average)   # 125.0  (wrong!)

Python divides test2 by 2 first (45.0), then adds 80. The fix is (test1 + test2) / 2, which gives 85.0.

The modulo operator has a famous trick: a number is even when n % 2 is 0 and odd when it is 1. Games use the same idea to do something every third turn (turn % 3 == 0) or to wrap around a board: on a 12-hour clock, 10 o'clock plus 5 hours is (10 + 5) % 12, which is 3.

Step 5 of 13Practice · Diagram

Who goes first?

Higher levels run first. Operators on the same level run left to right.

In 2 + 3 ** 2, which level is worked out first?

Tap the shape that answers the question, then press Check.

Who goes first?Pyramid diagram: + and -, * / // %, ** (power), ( ) parentheses.+ and -* / // %** (power)( ) parentheses
+ and -

Last of all.

* / // %

Same level, left to right.

** (power)

2 ** 3 is 8.

( ) parentheses

Always first. Use them whenever you are unsure.

Step 6 of 13Practice · Work it out

What does Python print for print(20 - 4 * 3 + 10 // 4)?

Step 7 of 13Practice · Choose one

Which expression correctly calculates the average of three test scores a, b and c?

Step 8 of 13Note

Step 9 of 13Compare

Three kinds of division: 17 and 5

Aspect17 / 517 // 517 % 5
Result3.432
Typefloatintint
MeaningExact divisionHow many whole 5s fitWhat is left over
Example useAverage scoreFull teamsLeftover players, even/odd tests

Step 10 of 13Practice · Match the pairs

Match each expression to its result.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. 2 ** 4
  2. 17 % 5
  3. 17 // 5
  4. 9 / 3

Step 11 of 13Practice · True or false

In Python, 2 ^ 3 calculates 2 to the power of 3 and gives 8.

Step 12 of 13Practice · Fill in the blanks

Fill in the blanks

A pizza has 8 slices and 3 friends share it equally. Fill in the operators: each friend gets 8 3 slices, and 8 3 slices are left over.

Step 13 of 13Practice · Select all that apply

Which expressions evaluate to True only when the whole number n is even? Select all that apply.

Select all that apply.