Skip to content

Beginner · First steps in Python · topic 4 of 50 · 20 min

Numbers and maths

Explanation

Python does maths with + - * / and a few extra operators: // for whole-number division, % for the remainder and ** for powers. Whole numbers are int, decimals are float, and round() tidies long decimals.

The basic operators

OperatorMeaningExampleResult
+add7 + 310
-subtract7 - 34
*multiply7 * 321
/divide (always gives a float)7 / 23.5
//floor division: divide and drop the decimal part7 // 23
%modulo: the remainder after division7 % 21
**power2 ** 38
Think of it like this: Think of 17 laddoos shared among 5 children. Each child gets 17 // 5 = 3 laddoos, and 17 % 5 = 2 laddoos are left over in the box. Floor division answers "how many each", and modulo answers "how many left".
python
laddoos = 17
children = 5
print(laddoos // children)  # each child gets
print(laddoos % children)   # left over
Shows 3 and then 2.

Order of operations

Python follows the same order as school maths (BODMAS): brackets first, then powers, then multiply and divide, then add and subtract. Use brackets when you want to be sure.

python
print(2 + 3 * 4)
print((2 + 3) * 4)
Shows 14, then 20.

int and float

An int is a whole number like 25 or -3. A float is a number with a decimal point like 2.5. Dividing with / always gives a float, even when it divides evenly: 10 / 2 is 5.0.

Careful: Floats are stored in binary, so some decimals are not exact. 0.1 + 0.2 gives 0.30000000000000004, not 0.3. This is normal in every language. Use round() when you show money, and never compare floats with == for exact equality.
python
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
round(value, 2) keeps two digits after the point.

Useful number functions

python
print(abs(-50))        # distance from zero: 50
print(round(3.14159, 2)) # 3.14
print(max(45, 78, 62))  # biggest: 78
print(min(45, 78, 62))  # smallest: 45
print(int("25") + 5)    # turn text into a number: 30
abs, round, max, min and int are built in.
Tip: Use % to test if a number is even: n % 2 == 0 is True for even numbers. You will use this trick very often.

Short forms save typing. total += 50 means total = total + 50. The same works for -=, *= and /=.

python
wallet = 500
wallet -= 120  # bought a book
wallet += 200  # pocket money
print(wallet)
Shows 580.

Watch the lesson

Numbers and maths · 1/6

Numbers and maths

Python is a very good calculator. In this lesson you will learn its maths operators.

Prefer a video by a teacher? Find free videos on “python arithmetic operators floor division modulo” on YouTube ↗

In real life: Packing mangoes into boxes

A farmer in Ratnagiri has 1,250 mangoes. Each box holds 12. How many full boxes can he pack, and how many mangoes are left for his family? Floor division and modulo answer this in two lines.

python
mangoes = 1250
per_box = 12
print("Full boxes:", mangoes // per_box)
print("Left over:", mangoes % per_box)
Full boxes: 104, Left over: 2.

Practice (0/3 done)

Task 1 · Code · Python

Write a function boxes_needed(items, per_box) that returns how many FULL boxes you can pack. Example: boxes_needed(1250, 12) returns 104.

Task 2 · Code · Python

Write a function share(bill, people) that returns how much each person pays, rounded to 2 decimal places. Example: share(1000, 3) returns 333.33.

Task 3 · Code · Python

Write a function is_even(n) that returns True when n is even and False when it is odd.

Quick quiz

Question 1

1.What is 17 // 5?

Question 2

2.What is 17 % 5?

Question 3

3.What does 10 / 2 give in Python 3?

Question 4

4.What is 2 + 3 * 2?

Question 5

5.What does 2 ** 4 give?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Variables: giving names to values