Numbers and maths
Do calculations in Python with whole numbers and decimals: + - * /, whole-number division, remainders, powers and rounding, with a bill-splitting example.
🎯 By the end of this lesson you will be able to:
- Use Python's maths operators, including // and %
- Explain the difference between an int and a float
- Round results for display with round()
Outline
What you'll learn and why it matters
Prices, marks, distances, time: most programs calculate something. Python is an excellent calculator once you know its symbols, which are called operators. In this lesson you will use them to split a restaurant bill and convert minutes into hours.
The maths operators
| Operator | Meaning | 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 |
** | power | 7 ** 2 | 49 |
Try them all in the IDLE shell. Then try this program:
print(7 + 2)
print(7 / 2)
print(7 // 2)
print(7 % 2)
print(2 ** 10)Output
9 3.5 3 1 1024
Python follows the order of operations you learned at school: powers first, then * / // %, then + and -. Use brackets to make your meaning clear: (2 + 3) * 4 is 20, while 2 + 3 * 4 is 14.
Two kinds of number: int and float
Python has two everyday kinds (or types) of number:
| Type | What it is | Examples |
|---|---|---|
int (integer) | A whole number | 3, 0, -12, 1500 |
float | A number with a decimal point | 3.5, 0.1, -2.75, 100.0 |
Dividing with / always gives a float, even when the answer is whole: 10 / 2 is 5.0. You can ask Python for the type of any value with type():
print(type(10))
print(type(10 / 2))Output
<class 'int'> <class 'float'>
Worked example: split a bill
Four friends share a restaurant bill of ₹2,350 and want to add a 10% tip. How much does each person pay?
bill = 2350
tip_percent = 10
people = 4
tip = bill * tip_percent / 100
total = bill + tip
each = total / people
print("Tip:", tip)
print("Total:", total)
print("Each person pays:", round(each, 2))Output
Tip: 235.0 Total: 2585.0 Each person pays: 646.25
| Step | Calculation | Value |
|---|---|---|
| Tip | 2350 * 10 / 100 | 235.0 |
| Total | 2350 + 235.0 | 2585.0 |
| Each | 2585.0 / 4 | 646.25 |
round(each, 2) rounds to 2 decimal places, which is how we write money. round(3.14159, 1) gives 3.1; round(7.6) with no second number gives the whole number 8.
Whole-number division and remainders
// and % work as a pair. They are perfect for converting units. How many hours and minutes is 135 minutes?
minutes = 135
hours = minutes // 60
left_over = minutes % 60
print(hours, "hours and", left_over, "minutes")Output
2 hours and 15 minutes
135 // 60 is 2 (two full hours fit) and 135 % 60 is 15 (the minutes left over). A common use of %: a number is even if number % 2 is 0.
Practice
Guided exercise
Price with GST
A course book costs ₹450 before tax. GST of 18% is added. Print the tax and the final price.
- Create
price = 450andgst_percent = 18. - Work out
tax = price * gst_percent / 100. - Work out
final_price = price + tax. - Print both with labels.
Show the solution
price = 450
gst_percent = 18
tax = price * gst_percent / 100
final_price = price + tax
print("Tax:", tax)
print("Final price:", final_price)Output
Tax: 81.0 Final price: 531.0
How to get there: 18% of 450 is 450 × 18 ÷ 100 = 81. Because / is used, the answer is a float (81.0). Adding it to the price gives 531.0.
Your turn
Seconds to minutes
Store seconds = 500. Print how many whole minutes and remaining seconds that is, as 8 minutes and 20 seconds.
Show a hint
Use // for the minutes and % for the seconds left over.
Show the solution
seconds = 500
print(seconds // 60, "minutes and", seconds % 60, "seconds")Output
8 minutes and 20 seconds
How to get there: 500 // 60 is 8 because 8 × 60 = 480 fits into 500. 500 % 60 is the 20 seconds left over.
Common mistakes
print(10 / 0)Python stops with this error
ZeroDivisionError: division by zero
Dividing by zero
You see: ZeroDivisionError: division by zero
Fix: Check the number you divide by is not 0 before dividing (you will learn if in lesson 6).
Expecting / to give a whole number
You see: You see 5.0 instead of 5
Fix: Use // when you want a whole-number result, or int() to drop the decimal part.
Writing x for multiply
You see: SyntaxError: invalid syntax or a NameError
Fix: Python uses * for multiply.
Forgetting brackets
You see: A wrong answer with no error, e.g. 2 + 3 * 4 gives 14
Fix: Add brackets to show what should happen first: (2 + 3) * 4.
Recap
- Python uses
+ - * /plus//(whole-number division),%(remainder) and**(power). intis a whole number;floathas a decimal point./always gives a float.- Use brackets to control the order of operations, and
round(value, 2)to show money. - Store numbers in well-named variables so calculations are easy to read and change.
Key terms
- Operator
- A symbol that does something with values, such as + or *.
- int
- A whole number, such as 42.
- float
- A number with a decimal point, such as 4.2.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. What does `17 // 5` give?
2. What does `17 % 5` give?
3. What is the type of `8 / 2`?
4. Which line prints 20?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.