Nishaglobal Education logo

Numbers and maths

Do calculations in Python with whole numbers and decimals: + - * /, whole-number division, remainders, powers and rounding, with a bill-splitting example.

Lesson 3 of 1420 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. The maths operators
  3. Two kinds of number: int and float
  4. Worked example: split a bill
  5. Whole-number division and remainders
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

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

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
//divide and drop the remainder7 // 23
%remainder after dividing7 % 21
**power7 ** 249

Try them all in the IDLE shell. Then try this program:

Python
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:

TypeWhat it isExamples
int (integer)A whole number3, 0, -12, 1500
floatA number with a decimal point3.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():

Python
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?

Python
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
StepCalculationValue
Tip2350 * 10 / 100235.0
Total2350 + 235.02585.0
Each2585.0 / 4646.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?

Python
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.

  1. Create price = 450 and gst_percent = 18.
  2. Work out tax = price * gst_percent / 100.
  3. Work out final_price = price + tax.
  4. Print both with labels.
Show the solution
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
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

Dividing by zero
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).
  • int is a whole number; float has 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. 1. What does `17 // 5` give?

  2. 2. What does `17 % 5` give?

  3. 3. What is the type of `8 / 2`?

  4. 4. Which line prints 20?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.