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
| Sign | Meaning | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3.5 |
// | divide and drop the fraction | 7 // 2 | 3 |
% | remainder after division | 7 % 2 | 1 |
** | power | 7 ** 2 | 49 |
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
49Three points surprise beginners:
- A single slash always gives a float, even when the answer is whole.
8 / 2is4.0, not4. - The double slash
//is called floor division. It gives the whole part of the answer. - The percent sign has nothing to do with percentages. It gives the remainder.
print(8 / 2)
print(17 // 5, "packets and", 17 % 5, "left over")Output
4.0
3 packets and 2 left overThe 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 minutesOrder 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.0Rounding
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.6Look 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.3This 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
60abs 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 zeroLater 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: 20Exercise 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 secondsExercise 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.7Examples 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.