Modules and the standard libraryChapter 73 of 114
Math
The math module, plus statistics and random.
Roots and powers
import math
print(math.sqrt(16))
print(math.pow(2, 10))
print(2 ** 10)
print(math.isqrt(17))Output
4.0 1024.0 1024 4
math.pow always gives a float; ** keeps integers as integers. isqrt is the integer square root, rounded down.
Rounding
import math
print(math.floor(3.7), math.floor(-3.2))
print(math.ceil(3.2), math.ceil(-3.7))
print(math.trunc(3.7), math.trunc(-3.7))
print(round(3.7))Output
3 -4 4 -3 3 -3 4
Four different answers for four different questions: floor rounds down, ceil rounds up, trunc rounds towards zero, and round rounds to nearest with halves going to even.
Constants
import math
print(math.pi)
print(math.e)
print(math.inf > 10 ** 100)
print(math.isnan(math.nan))Output
3.141592653589793 2.718281828459045 True True
nan is never equal to anything, including itself, which is why isnan() exists:
import math
print(math.nan == math.nan)Output
False
Comparing floats
The fix for the 0.1 + 0.2 problem:
import math
print(0.1 + 0.2 == 0.3)
print(math.isclose(0.1 + 0.2, 0.3))Output
False True
Logs and trigonometry
import math
print(math.log(math.e))
print(math.log10(1000))
print(math.log2(8))
print(round(math.sin(math.radians(90)), 10))Output
1.0 3.0 3.0 1.0
Trigonometric functions work in radians. math.radians() and math.degrees() convert.
Other useful pieces
import math
print(math.gcd(12, 18))
print(math.factorial(5))
print(math.comb(5, 2))
print(math.dist((0, 0), (3, 4)))Output
6 120 10 5.0
statistics
For averages, reach for statistics rather than writing your own:
import statistics
values = [2, 4, 4, 4, 5, 5, 7, 9]
print(statistics.mean(values))
print(statistics.median(values))
print(statistics.mode(values))
print(statistics.stdev(values))Output
5 4.5 4 2.138089935299395
random
import random
random.seed(1)
first = random.randint(1, 6)
random.seed(1)
again = random.randint(1, 6)
print(first == again)
print(1 <= first <= 6)
print(random.choice(["rock", "paper"]) in ("rock", "paper"))
deck = [1, 2, 3, 4, 5]
random.shuffle(deck)
print(sorted(deck))
print(len(random.sample(range(100), 3)))Output
True True True [1, 2, 3, 4, 5] 3
seed() fixes the sequence so the output is repeatable. Without it every run differs — which is the point, except when testing.
import secrets
print(len(secrets.token_hex(16)))Output
32
Decimal for money
from decimal import Decimal
print(0.1 + 0.2)
print(Decimal("0.10") + Decimal("0.20"))
print(Decimal("1.10") * 3)Output
0.30000000000000004 0.30 3.30
Build a Decimal from a string. Decimal(0.1) inherits the float's error before it ever gets there.
Test yourself
2 questionsWhy does math.nan == math.nan give False?
Show the answer
nan is defined never to equal anything, including itself — That is why math.isnan() exists as a separate function.
Which module should you use for passwords and tokens?
Show the answer
secrets — random is predictable from its seed and is not safe for anything security related.
JSON
Turn Python data into text and back, for files and APIs.