Skip to content

Arithmetic

~15 min ยท prerequisite: Variables and types

What you will be able to do

  • Use the seven arithmetic operators: +, -, *, /, //, %, and **.
  • Predict the result of an expression that mixes operators, using the order of operations.
  • Recognize when a result is a float, and why 0.1 + 0.2 is not exactly 0.3.

The idea

An operator is a symbol that performs an action on values. The arithmetic operators work on numbers, and an expression is any combination of values and operators that produces a result. The expression 7 + 3 has the value 10.

Here are the operators you will use most:

  • + adds two numbers, and - subtracts.
  • * multiplies, and ** raises a number to a power, so 2 ** 10 is 1024.
  • / divides and always gives a float, so 7 / 2 is 3.5, even though 7 and 2 are integers. Also 8 / 2 is 4.0, not 4.
  • // is floor division. It divides and rounds down to the nearest whole number, so 7 // 2 is 3.
  • % is the remainder operator. It gives what is left after division, so 7 % 2 is 1.

Floor division and remainder work as a pair. For positive whole numbers, a // b counts the full groups of b in a, and a % b counts what is left over.

When an expression has several operators, Python follows the order of operations. ** is done first, then *, /, //, and %, and finally + and -. Operators at the same level are done from left to right, except **, which is done from right to left. Parentheses override all of these rules, and they make your intent clear to the next reader.

Mixing an integer and a float gives a float. Floats are stored in binary, so some decimal values cannot be represented exactly. The result of 0.1 + 0.2 is 0.30000000000000004. For this reason, check float results with a small tolerance instead of with ==.

Worked example

seconds = 3725
hours = seconds // 3600
minutes = seconds % 3600 // 60
rest = seconds % 60

print(hours, minutes, rest)

Line by line, this is what Python does:

  1. seconds = 3725 stores the integer 3725.
  2. hours = seconds // 3600 divides 3725 by 3600 and rounds down. The result is 1, so there is one full hour.
  3. minutes = seconds % 3600 // 60 evaluates in the usual order. First seconds % 3600 gives 125, the seconds left after the full hour. Then 125 // 60 gives 2, so there are two full minutes.
  4. rest = seconds % 60 gives the remainder after dividing by 60. The result is 5, so there are five seconds left.
  5. print(hours, minutes, rest) shows 1 2 5, with a space between each value.

Try it

Each block checks a group of results with assert. If any result is different from what the comment says, the check fails.

assert 7 + 3 == 10
assert 7 - 3 == 4
assert 7 * 3 == 21
assert 7 / 2 == 3.5
assert 7 // 2 == 3
assert 7 % 2 == 1
assert 2 ** 10 == 1024
assert 2 + 3 * 4 == 14
assert (2 + 3) * 4 == 20
assert 2 ** 3 ** 2 == 512
assert -7 // 2 == -4
assert -7 % 2 == 1
assert type(8 / 2) is float
assert 8 / 2 == 4
assert type(8 // 2) is int
assert 0.1 + 0.2 != 0.3
assert abs((0.1 + 0.2) - 0.3) < 1e-9

seconds = 3725
assert seconds // 3600 == 1
assert seconds % 3600 // 60 == 2
assert seconds % 60 == 5

Common mistakes

  1. Expecting / to give a whole number. The following check fails:

    result = 7 / 2
    assert result == 3
    
    AssertionError
    

    / gives 3.5. Use // when you want the whole part.

  2. Dividing by zero. Writing print(10 / 0) gives:

    ZeroDivisionError: division by zero
    

    Check the divisor before you divide.

  3. Using ^ for powers. In Python, ^ is not the power operator. The following check fails:

    assert 2 ^ 3 == 8
    
    AssertionError
    

    2 ^ 3 evaluates to 1, because ^ means a different operation. Use 2 ** 3 for powers.

  4. Adding a number to text. Writing print(2 + "3") gives:

    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    

    Convert the text with int("3") first.

  5. Comparing floats with ==. Printing 0.1 + 0.2 shows 0.30000000000000004, not 0.3. Compare with a small tolerance, as in the third block above.

Practice

  1. Compute 15 // 4 and 15 % 4 by hand. Then check your answers with assert.
  2. Write an assert that checks 3 * 4 + 2 equals 14, and another that checks 3 * (4 + 2) equals 18.
  3. Write an assert that checks 2 ** 5 equals 32. Then write an assert that checks 32 / 2 equals 16.0.
  4. Given minutes = 135, compute how many full hours are in it and how many minutes are left over. Assert both values.
  5. Stretch: a number is even when n % 2 == 0. Write an assert that checks 14 is even and 15 is not. Then write three lines that compute the sum of the digits of 472 using // and %, and assert the sum is 13.
Solution
  1. 15 // 4 is 3 and 15 % 4 is 3.
assert 15 // 4 == 3
assert 15 % 4 == 3
  1. python assert 3 * 4 + 2 == 14 assert 3 * (4 + 2) == 18

  2. python assert 2 ** 5 == 32 assert 32 / 2 == 16.0

  3. python minutes_total = 135 hours = minutes_total // 60 leftover = minutes_total % 60 assert hours == 2 assert leftover == 15

  4. ```python assert 14 % 2 == 0 assert 15 % 2 != 0

n = 472 digit_a = n % 10 n = n // 10 digit_b = n % 10 n = n // 10 digit_c = n % 10 assert digit_a + digit_b + digit_c == 13 ```

The digits are 2, 7, and 4, and their sum is 13.

Recap

  • The operators are +, -, *, /, //, %, and **. Division with / always gives a float.
  • Python evaluates ** first, then *, /, //, %, and then + and -. Parentheses override this order.
  • Use // and % together to split a number into whole groups and a remainder.

Sources