Numbers and arithmetic

Python as a calculator. The arithmetic operators, whole-number division and remainders, powers, the order operations happen in, and why 0.1 + 0.2 is not quite 0.3.

Lesson · 15 min

In this lesson you will

  • Use + - * / // % and ** in Python
  • Explain the difference between / and //, and what % gives
  • Predict the order Python evaluates an expression in, and use brackets to change it
  • Round results with round() and explain small floating-point surprises

Computers were invented to calculate, and Python makes an excellent calculator. In this lesson you’ll learn every arithmetic operator you need for the rest of the path, including two that don’t exist on most calculators and turn out to be surprisingly useful.

The operators

An operator is a symbol that does something to values. An expression is a combination of values and operators that Python can work out, like 3 + 4 * 2.

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
//whole-number divide7 // 23
%remainder7 % 21
**power7 ** 249

Run this and compare with the table. Then change the numbers and predict before each run.

print(7 + 2, 7 - 2, 7 * 2)
print(7 / 2)
print(7 // 2, 7 % 2)
print(7 ** 2)

Division always gives a float

Here’s a surprise. Predict what this prints:

print(8 / 2)

It’s 4.0, not 4. The / operator always gives a float, even when the answer is a whole number. That way you always know what type you’ll get back.

Whole-number division and remainders

Remember doing division at school before you learned decimals? “17 divided by 5 is 3, remainder 2.” Python can do exactly that with two operators:

  • // gives the whole number of times one number fits into another: 17 // 5 is 3.
  • % (called modulo) gives what’s left over: 17 % 5 is 2.

They’re perfect for splitting things into groups. Say 23 cookies are shared between 4 friends:

cookies = 23
friends = 4
each = cookies // friends
left_over = cookies % friends
print("Each friend gets", each, "and", left_over, "are left over")

Remainders have another famous use: a number is even when dividing by 2 leaves nothing.

print(10 % 2)
print(7 % 2)

0 means even, 1 means odd. You’ll use exactly this trick in the next section to make decisions. And % works on time too: 135 minutes is 135 // 60 hours and 135 % 60 minutes.

Order of operations

What is 2 + 3 * 4? If you work left to right you get 20. Python gets 14, because, just like in maths, multiplication and division happen before addition and subtraction. The full order, from first to last:

  1. Brackets ( )
  2. Powers **
  3. Multiply and divide: *, /, //, %
  4. Add and subtract: +, -

Operators on the same level run left to right. When in doubt, add brackets. They cost nothing and make your meaning obvious.

print(2 + 3 * 4)
print((2 + 3) * 4)
print(10 - 4 - 3)
print(2 ** 3 * 2)

Here’s a real example where brackets matter. The average of three test scores:

a = 70
b = 80
c = 90
wrong = a + b + c / 3
right = (a + b + c) / 3
print(wrong, right)

Without brackets, only c gets divided by 3. A classic bug, and it doesn’t cause an error: the program happily prints the wrong answer. That’s why predicting results matters.

Ints and floats together

When an int and a float meet in a calculation, the answer is a float:

print(3 + 0.5)
print(2 * 1.5)
print(type(10 + 2), type(10 + 2.0))

Rounding

round(number, digits) rounds to that many digits after the decimal point. With no second value it rounds to a whole number:

price = 19.987
print(round(price, 2))
print(round(price))
print(round(3.14159, 3))

A tiny floating-point surprise

Try this, and predict first:

print(0.1 + 0.2)

You get 0.30000000000000004. Python isn’t broken. Computers store floats in binary, and just as one third can’t be written exactly in decimal (0.3333…), one tenth can’t be written exactly in binary. The error is tiny and almost never matters, but it’s why we round money before showing it, and why you should be careful asking whether two floats are exactly equal. Every programming language has this quirk.

print(round(0.1 + 0.2, 2))

Big numbers are fine

Python’s whole numbers can be as big as you like. You can even put underscores in long numbers to make them easier to read; Python ignores them.

print(2 ** 100)
population = 8_100_000_000
print(population * 2)

Your turn

Exercise · Python

Pizza party

You order 3 pizzas, each cut into 8 slices, for 7 friends. Everyone gets the same whole number of slices and the rest are left over. Create these variables:

  • slices: the total number of slices (use the numbers 3 and 8, don’t type 24),
  • each: how many whole slices each friend gets,
  • left_over: how many slices are left,
  • percent_eaten: the percentage of all slices that get eaten, rounded to 1 decimal place.
pizzas = 3
slices_per_pizza = 8
friends = 7

slices = 0
each = 0
left_over = 0
percent_eaten = 0

print(slices, each, left_over, percent_eaten)

Check yourself

Self-check · 4 questions

Numbers and arithmetic

1. What is 17 // 5?
2. What is 17 % 5?
3. What does 2 + 3 * 4 evaluate to?
4. What does 8 / 2 give in Python?