For anything beyond + - * /, import the math module: import math. It works on numbers only and gives fast, accurate results.
Rounding
math.floor(x) and math.ceil(x)
floor rounds down to a whole number, ceil rounds up. Careful with negatives.
import math
print(math.floor(3.7), math.ceil(3.2))
print(math.floor(-3.2), math.ceil(-3.7))
Output:
3 4
-4 -3
math.trunc(x)
Cuts off the decimals (rounds toward zero).
import math
print(math.trunc(3.9), math.trunc(-3.9))
Output:
3 -3
Powers, roots and logs
math.sqrt(x)
The square root. Use math.isqrt(n) for the exact whole-number part.
import math
print(math.sqrt(144))
print(math.isqrt(50))
Output:
12.0
7
math.pow(a, b), math.exp(x)
math.pow always returns a float. exp(x) is e to the power x.
import math
print(math.pow(2, 3))
print(round(math.exp(1), 4))
Output:
8.0
2.7183
math.log(x, base), log2, log10
Logarithms. With one argument it is the natural logarithm.
import math
print(math.log10(1000))
print(math.log2(8))
print(math.log(81, 3))
Output:
3.0
3.0
4.0
Whole-number helpers
math.gcd(a, b) and math.lcm(a, b)
Greatest common divisor and least common multiple.
import math
print(math.gcd(12, 18))
print(math.lcm(4, 6))
Output:
6
12
math.factorial(n)
n! = 1 x 2 x ... x n.
import math
print(math.factorial(5))
print(math.factorial(10))
Output:
120
3628800
math.comb(n, k) and math.perm(n, k)
Combinations (order does not matter) and permutations (order matters).
import math
print(math.comb(5, 2))
print(math.perm(5, 2))
Output:
10
20
Constants and trigonometry
math.pi, math.e, math.inf
Useful constants.
import math
print(round(math.pi, 5))
print(round(math.e, 5))
print(math.inf > 10 ** 100)
Output:
3.14159
2.71828
True
math.sin, cos, tan, radians, degrees
Trigonometry works in radians. Convert with radians() and degrees().
import math
print(round(math.sin(math.radians(30)), 3))
print(round(math.degrees(math.pi), 1))
Output:
0.5
180.0
math.hypot(a, b)
The length of the hypotenuse: sqrt(a squared + b squared).
import math
print(math.hypot(3, 4))
Output:
5.0
Comparing decimals safely
Decimal fractions are stored approximately, so never compare floats with ==. Use math.isclose.
math.isclose(a, b)
True if two floats are equal within a small tolerance.
import math
print(0.1 + 0.2 == 0.3)
print(math.isclose(0.1 + 0.2, 0.3))
Output:
False
True
Number bases
bin(n), oct(n), hex(n) and int(text, base)
Show a number in binary, octal or hexadecimal, and convert text in any base back to an integer.
print(bin(10))
print(oct(64))
print(hex(255))
print(int("1010", 2))
print(int("ff", 16))
Output:
0b1010
0o100
0xff
10
255
random.randint, random.choice, random.random
Random numbers. The results change on every run, so this example is shown but not run. random.seed(n) makes a sequence repeatable.
import random
print(random.randint(1, 6)) # a whole number from 1 to 6
print(random.choice(["a", "b", "c"]))
print(random.random()) # a decimal from 0 up to 1
Try it yourself
- Calculate the area of a circle of radius 7 with
math.pi. - Find the gcd and lcm of 84 and 36.
- How many ways can you choose 3 students from 10? Use
math.comb.
Next lesson: text, part one (cleaning and changing).

Comments
Loading comments…