Python for beginners · Lesson 3 of 15

Numbers and maths in Python

Whole numbers and decimals, the maths operators including //, % and **, the order of operations and rounding with round().

About 14 minutes · code runs in your browser, nothing to install

Python has two main kinds of numbers: int for whole numbers (7, -3, 2026) and float for numbers with a decimal point (3.5, 0.25). You can calculate with them using the usual operators.

Pythoneditable
print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)
print(8 / 2)
Output
9
5
14
3.5
4.0

Division with / always gives a float, even when the result is a whole number.

Whole-number division and the remainder

// divides and rounds the result down to a whole number. % (called modulo) gives the remainder that is left over. Together they are perfect for splitting things into groups, like minutes into hours.

Pythoneditable
minutes = 135
hours = minutes // 60
rest = minutes % 60
print(hours, "hours and", rest, "minutes")
print(10 % 2)
print(7 % 2)
Output
2 hours and 15 minutes
0
1

A number is even when number % 2 is 0, and odd when it is 1.

Powers and the order of operations

** raises a number to a power: 2 ** 3 is 2 × 2 × 2. Python follows the same order as maths: powers first, then * / // %, then + and -. Use round brackets to change the order.

Pythoneditable
print(2 ** 10)
print(9 ** 0.5)
print(2 + 3 * 4)
print((2 + 3) * 4)
Output
1024
3.0
14
20

A power of 0.5 is the square root.

Decimals are not always exact

Computers store floats in binary, so some decimals cannot be stored exactly. The tiny error usually does not matter, but it shows up when you print. Use round() to round to a number of decimal places.

Pythoneditable
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
print(round(3.14159, 2))
print(round(7.6))
print(round(2.5))
print(round(3.5))
Output
0.30000000000000004
0.3
3.14
8
2
4
Note: round(2.5) gives 2, not 3. When a number is exactly halfway, Python rounds to the nearest even number. This avoids always pushing totals upwards.

Shortcuts and conversions

balance += 25 is short for balance = balance + 25. The same works with -=, *= and /=. To turn a float into an int (cutting off the decimals) use int(), and to go the other way use float().

Pythoneditable
balance = 100
balance += 25
balance -= 40
print(balance)
balance *= 2
print(balance)
print(int(7.9))
print(float(3))
Output
85
170
7
3.0

int() does not round: it simply drops everything after the decimal point.

✎ Exercise

Three friends share a restaurant bill of 87.60 and want to add a 10% tip. Calculate the total with tip and what each person pays. Print both rounded to 2 decimal places, after the labels Total: and Each pays:.

Hint: 10% of the bill is bill * 0.10. Divide the total by 3, then use round(value, 2).

Your codewrite your answer, then Run
bill = 87.60
# your code here

Expected output:

Total: 96.36
Each pays: 32.12
Show a solution
Solutioneditable
bill = 87.60
tip = bill * 0.10
total = bill + tip
each = total / 3
print("Total:", round(total, 2))
print("Each pays:", round(each, 2))
Output
Total: 96.36
Each pays: 32.12

Check yourself

1. What is 17 // 5?

2. What is 17 % 5?

3. What does print(10 / 2) show?

Spotted a mistake on this page? Tell us