This Python Math lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.
The math module adds roots, rounding up and down, and constants such as pi. Built-in round, min, max, and sum cover many everyday cases.
ceil moves up to an integer. floor moves down.
Example 1: Start here
round uses bankers rounding on exact halves. Do not use it for money totals.
import math # load a tool that comes with Python
print(math.floor(3.9))
print(math.ceil(3.1))
print(round(3.14159, 2))
Example 2: Square root and power
sqrt is the square root. pow raises a number to a power.
import math # load a tool that comes with Python
print(math.sqrt(81))
print(math.pow(2, 3))
Example 3: Round with floor and ceil
floor goes down. ceil goes up.
import math # load a tool that comes with Python
print(math.floor(3.7))
print(math.ceil(3.2))
Example 4: Pi
math.pi is the constant. round keeps two decimal places.
import math # load a tool that comes with Python
print(round(math.pi, 2))
