Module 3 · Python for Beginners
Numbers and Maths
Do arithmetic in Python: the operators, whole-number division and remainders, the order of operations, rounding, useful built-in functions, the math module and formatting money.
About 35 minutes
Open in ColabEvery example as a notebook you can run, edit and save to your Google Drive.
Arithmetic operators
Python is a very good calculator. The operators:
| Operator | Does | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3.5 |
// | divide and drop the remainder | 7 // 2 | 3 |
% | remainder after dividing | 7 % 2 | 1 |
** | to the power of | 7 ** 2 | 49 |
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
9
5
14
3.5
3
1
49Division always gives a float
/ always returns a decimal, even when the answer is whole:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
5.0That's usually fine. When you need a whole number, use // or int().
Whole-number division and remainder
// and % answer two everyday questions. You have ₦25,000 and data bundles cost ₦3,500: how many can you buy, and what's left?
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
7
500Seven bundles, ₦500 change. % is also how you check whether a number is even (n % 2 == 0) or turn 130 minutes into hours and minutes:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
2 hours and 10 minutesThe order of operations
Python follows the same order as maths: brackets first, then powers, then * / // %, then + -. When operators have the same rank, it works left to right.
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
14
20In the first line, 3 * 4 happens first. Brackets change the order. When in doubt, add brackets: they cost nothing and make your intent clear.
A practical example. Your scores are 67, 81 and 54. The average needs brackets round the sum:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
166.0
67.33333333333333The first is wrong: only 54 was divided by 3.
Rounding
round(number, places) rounds to a number of decimal places. With no places, it rounds to a whole number:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
67.33
67Why 0.1 + 0.2 isn't exactly 0.3
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
0.30000000000000004Computers store decimals in binary, and some decimals, like 0.1, can't be stored exactly, just as 1/3 can't be written exactly as a decimal. The error is tiny, but it shows up. Round when you display results, and never test decimals with ==.
Useful built-in functions
| Function | Returns | Example | Result |
|---|---|---|---|
abs(x) | the value without its sign | abs(-500) | 500 |
min(a, b, ...) | the smallest | min(67, 81, 54) | 54 |
max(a, b, ...) | the largest | max(67, 81, 54) | 81 |
round(x, n) | x rounded to n places | round(3.14159, 2) | 3.14 |
pow(x, y) | x to the power y | pow(2, 10) | 1024 |
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
500
54 81The math module
More maths lives in the math module, a collection of extra tools you import before using:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
12.0
5
4
3.141592653589793math.ceil rounds up and math.floor rounds down, whatever the decimal. That's handy for "how many buses do we need?": 130 students at 18 seats a bus needs math.ceil(130 / 18), which is 8, not 7.2.
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
8Working with money
Percentages
A percentage is just multiplication by a fraction. 7.5% VAT on ₦40,000:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
3000.0
43000.0A 15% discount, and what share one amount is of another:
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
34000.0
20.8So ₦12,500 is 20.8% of ₦60,000.
Formatting numbers for people
Long numbers are hard to read. Inside an f-string, add a format after a colon:
| Format | Does | Example | Shows |
|---|---|---|---|
:, | thousands separators | f"{1234567:,}" | 1,234,567 |
:.2f | two decimal places | f"{3.14159:.2f}" | 3.14 |
:,.0f | separators, no decimals | f"{43000.0:,.0f}" | 43,000 |
:.1% | as a percentage | f"{0.208:.1%}" | 20.8% |
Edit the code and press Run (Ctrl + Enter). Examples on this page share their variables, like cells in a notebook.
Output
Total: ₦43,000
Share: 20.8%When it goes wrong
print(100 / 0)Output
ZeroDivisionError: division by zeroDividing by zero is impossible, and Python stops. Check the divisor first, for example if count > 0:, when it could be zero.
print("100" * 2 + 50)Output
TypeError: can only concatenate str (not "int") to str"100" is text, so "100" * 2 is "100100", and text plus a number fails. Convert first: int("100") * 2 + 50.
Summary
| You want... | Write |
|---|---|
| a division with decimals | a / b |
| how many whole times | a // b |
| what's left over | a % b |
| a power | a ** b |
| a rounded result | round(x, 2) |
| round up / down | math.ceil(x) / math.floor(x) |
| a percentage of a value | value * rate / 100 |
| money for people | f"₦{amount:,.0f}" |
Try it
Practice
You have ₦25,000. Data bundles cost ₦3,500 each. Using %, how much change is left after buying as many bundles as you can?
Practice
What does 2 + 3 * 4 ** 2 give? Work it out, then check in Colab.
Practice
A trip needs seats for 130 students. Each bus holds 18. How many buses do you need? Use math.ceil.
Task
8 minWrite a small shopping calculator: store a price and a quantity, work out the subtotal, add 7.5% VAT, and print the subtotal, VAT and total as naira with thousands separators using f-strings. Run it, then paste your code.
Your work is checked for
- A price and a quantity in variables
- Multiplies them
- Works out 7.5% VAT
- Prints with f-strings
- Formats with thousands separators
Earn your badge
Take the module check
Questions on this module. Pass and you earn the Python Numbers badge, free.
Create a free account to earn the badge