← Python from Scratch Lesson 52 of 78 · 10 XP

The math module

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

math has the maths that ordinary operators don't:

import math

math.sqrt(25)        # 5.0  — square root
math.pi              # 3.141592653589793
math.ceil(2.1)       # 3    — round up
math.floor(2.9)      # 2    — round down
math.gcd(12, 18)     # 6    — greatest common divisor
math.hypot(3, 4)     # 5.0  — hypotenuse
math.factorial(5)    # 120

Don't mix up round(), math.floor() and int():

2.7 -2.7
round(x) 3 -3
math.floor(x) 2 -3
int(x) 2 -2

math.ceil is often needed in everyday tasks: how many buses do 45 people need if a bus has 20 seats? math.ceil(45 / 20) = 3.

Task. Read the number of students and the number of seats in one bus. Print how many buses are needed.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.