ShareYourGroupShareYourGroupPython

Lesson 4 of 21 · about 5 minutes with the exercises

Numbers and maths

Python is a very good calculator. This lesson covers the two kinds of number, the signs for sums, and the handful of built-in functions that save you work.

Two kinds of number

An int is a whole number such as 7, 0 or -250. A float is a number with a decimal point such as 3.14 or -0.5. Python chooses the type from the way you write the number.

The arithmetic operators

SignMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
//divide and drop the fraction7 // 23
%remainder after division7 % 21
**power7 ** 249
print(7 + 2, 7 - 2, 7 * 2)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(7 ** 2)

Output

9 5 14
3.5
3
1
49

Three points surprise beginners:

print(8 / 2)
print(17 // 5, "packets and", 17 % 5, "left over")

Output

4.0
3 packets and 2 left over

The remainder operator is more useful than it looks. A number is even when number % 2 is 0. Minutes turn into hours and minutes with // and %.

minutes = 135
print(minutes // 60, "hours", minutes % 60, "minutes")

Output

2 hours 15 minutes

Order of operations

Python follows the school rule. Powers first, then multiplication and division, then addition and subtraction. Brackets override everything. When in doubt, add brackets. They cost nothing and make the meaning plain.

print(2 + 3 * 4)
print((2 + 3) * 4)
print(100 - 20 / 4)

Output

14
20
95.0

Rounding

round takes a number and, if you wish, how many decimal places to keep.

print(round(3.14159, 2))
print(round(7.5))
print(round(8.5))
print(round(1234.5678, 1))

Output

3.14
8
8
1234.6

Look at the middle two lines. Python rounds a number that sits exactly halfway to the nearest even whole number, so 7.5 becomes 8 and 8.5 also becomes 8. This is called banker's rounding. School rounding always goes up at the half. Keep this in mind when your answer differs from your notebook.

Floats are not exact

Computers store decimals in binary, and some simple decimals cannot be stored exactly. You will sometimes see a long tail of digits.

print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))

Output

0.30000000000000004
0.3

This is normal in every programming language. Round the result when you show it.

Useful built-in functions

print(abs(-12))
print(max(4, 9, 2))
print(min(4, 9, 2))
print(pow(2, 10))
print(sum([10, 20, 30]))

Output

12
9
2
1024
60

abs gives the size of a number without its sign. max and min pick the largest and smallest. sum adds a list of numbers, which you will meet properly in the lesson on lists.

Dividing by zero

Dividing by zero is not allowed, in maths or in Python.

print(10 / 0)

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    print(10 / 0)
          ~~~^~~
ZeroDivisionError: division by zero

Later lessons show how to check a value before dividing. There is also a page on ZeroDivisionError.

Try it yourself

Exercise 1. A shop sells a pen for 12 rupees. Work out the cost of 15 pens, and the change from a 200 rupee note.

Show one answer
cost = 12 * 15
print("Cost:", cost)
print("Change:", 200 - cost)

Output

Cost: 180
Change: 20

Exercise 2. Turn 500 seconds into minutes and seconds using // and %.

Show one answer
seconds = 500
print(seconds // 60, "minutes", seconds % 60, "seconds")

Output

8 minutes 20 seconds

Exercise 3. Marks in three subjects are 78, 85 and 91. Print the average, rounded to one decimal place.

Show one answer
average = (78 + 85 + 91) / 3
print(round(average, 1))

Output

84.7

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.