Skip to content

Numbers and arithmetic

3 min read

Python is a very good calculator, but a few of its operators don't behave like the calculator on your phone. By the end of this lesson you'll know the two kinds of numbers Python uses, all seven arithmetic operators, and the surprises that matter when you work with money.

Two kinds of division

Four lines, four divisions of sorts. Write down what you think each one prints, then reveal.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

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

Python prints

3.5
3
1
5.0

/ is ordinary division, and it always gives a number with a decimal point, even when the answer is whole: 10 / 2 is 5.0. // divides and keeps only the whole part, and % gives the remainder: 7 is 2 × 3 with 1 left over.

Python has two kinds of number. Whole numbers like 7 and 250 are an . Numbers with a decimal point, like 3.5 or 5.0, are a . 5 and 5.0 are equal in value, but they're different kinds of number, and / always gives a float.

Quick check

What does print(10 / 2) show?

Here are all the operators in one program. Run it, then change the numbers.

print(12 + 5)
print(12 - 5)
print(12 * 5)
print(12 / 5)
print(12 // 5)
print(12 % 5)
print(10 ** 3)

In the editor, press Escape then Tab to move on.

** means "to the power of": 10 ** 3 is 10 × 10 × 10.

print(2 ^ 3)
print(2 ** 3)

In the editor, press Escape then Tab to move on.

Which part is worked out first

Python follows the order you learned in school: ** first, then *, /, //, and %, then + and -. Parentheses go first of all.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

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

Python prints

14
20

In the first line, 3 * 4 happens first, then 2 +. The parentheses in the second line make Python add first.

A taxi costs 50 birr to start plus 12 birr per kilometre. For a 6 km ride, both of these give 122, but the second is easier to read. When in doubt, add parentheses.

print(50 + 12 * 6)
print(50 + (12 * 6))

In the editor, press Escape then Tab to move on.

Remainders are useful

// and % work as a pair. Say you have 1,275 birr and want it in 100-birr notes. // gives the number of whole notes, and % gives what's left over.

money = 1275
notes = money // 100
left = money % 100
print(notes, "notes of 100 birr")
print(left, "birr left over")

In the editor, press Escape then Tab to move on.

Change money to your own amount, or change 100 to 200 for 200-birr notes.

Dividing by zero is the one division Python refuses:

Floats are close, not exact

Try this. It surprises everyone the first time.

print(0.1 + 0.2)

In the editor, press Escape then Tab to move on.

Computers store floats in binary, and most decimal fractions, like 0.1, can't be written exactly in binary, the same way 1/3 can't be written exactly in decimal (0.3333…). So floats are very close to the right value, but not always exactly it.

For money this matters. Two simple rules: count in santim (whole numbers) when you add up prices, or round when you show the result. round(value, 2) rounds to two decimal places.

# 10 santim plus 20 santim, counted as whole numbers
print(10 + 20)
# 0.1 birr plus 0.2 birr, rounded when shown
print(round(0.1 + 0.2, 2))

In the editor, press Escape then Tab to move on.

round has one more surprise. Predict it.

What will this print?

Decide before you look. Guessing wrong is how this sticks.

print(round(2.5))
print(round(3.5))

Python prints

2
4

When a number is exactly halfway, round goes to the nearest even number. 2.5 goes down to 2, and 3.5 goes up to 4. It's often called banker's rounding: over many roundings, it avoids always pushing totals up.

One last thing, and a good one: Python's whole numbers have no fixed size limit.

print(2 ** 100)

In the editor, press Escape then Tab to move on.

That's a 31-digit number, and Python gets every digit right.

Exercise

Taxi fare calculator

A taxi charges a base fare of 50 birr plus 14 birr per kilometre. Give names to the base fare, the price per km, and the distance, 8.5 km. Work out the fare from those names and print it.

Output
Fare: 169.0 birr

Your code

# Base fare, price per km, and distance, then the fare

In the editor, press Escape then Tab to move on.

Show a solution

This is one way to solve it, not the only one. If yours prints the same thing, it works.

base_fare = 50
price_per_km = 14
distance = 8.5
fare = base_fare + price_per_km * distance
print("Fare:", fare, "birr")

Key takeaways

  • Python has whole numbers (int) and numbers with a decimal point (float).
  • / always gives a float (10 / 2 is 5.0); // keeps the whole part and % gives the remainder.
  • ** is power. ^ is not, and it won't warn you.
  • Multiplication and division happen before addition and subtraction; parentheses make the order clear.
  • Floats are close but not exact: count money in santim, or round when you show it. round sends halves to the even number.