Lesson 9 · Python standard library deep dive
Python math, random & statistics Explained | Standard Library #9
Video nine of the twenty-five-part series: the three core numeric modules, covering pure math, randomness, and descriptive statistics. Constants, trig,…
- CoursePython standard library deep dive
- Lesson9 of 24
- Video20 min
- FormatJupyter notebook · 15 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbPython Standard Library Deep-Dive, Video 9: math, random, statistics#
- Video nine of the twenty-five-part series: the three core numeric modules, covering pure math, randomness, and descriptive statistics.
- Constants, trig, logs, combinatorics, random numbers and distributions, and statistics on real data.
- Let's get into it.
Part 1: What These Three Offer#
import math
import random
import statistics
print(math.pi)
print(random.random())
print(statistics.mean([1, 2, 3, 4, 5]))
Part 2: math - Constants and Basic Functions#
print(math.pi)
print(math.e)
print(math.sqrt(16))
print(math.pow(2, 10))
print(2 ** 10)
print(math.floor(4.7))
print(math.ceil(4.2))
print(math.trunc(4.7))
print(math.trunc(-4.7))
Part 3: math - Trigonometry and Logarithms#
print(math.sin(math.pi / 2))
print(math.cos(0))
print(math.radians(180))
print(math.degrees(math.pi))
print(round(math.sin(math.radians(30)), 4))
print(math.log(math.e))
print(math.log(100, 10))
print(math.log2(8))
print(math.log10(1000))
print(math.exp(1))
print(math.exp(0))
Part 4: math - Combinatorics and Special Functions#
print(math.factorial(5))
print(math.gcd(48, 18))
print(math.lcm(4, 6))
print(math.comb(5, 2))
print(math.perm(5, 2))
print(math.perm(5))
a = 0.1 + 0.2
print(a == 0.3)
print(math.isclose(a, 0.3))
print(math.isclose(100, 100.0001, rel_tol=0.01))
print(math.isclose(100, 100.0001, rel_tol=1e-9))
Part 5: random - Basic Random Numbers#
value = random.random()
print(0 <= value < 1)
temp = random.uniform(60.0, 80.0)
print(60.0 <= temp <= 80.0)
dice_roll = random.randint(1, 6)
print(1 <= dice_roll <= 6)
even = random.randrange(0, 10, 2)
print(even in [0, 2, 4, 6, 8])
Part 6: random - Choosing and Shuffling#
colors = ['red', 'green', 'blue', 'yellow']
picked = random.choice(colors)
print(picked in colors)
with_repeats = random.choices(colors, k=6)
print(len(with_repeats))
without_repeats = random.sample(colors, k=3)
print(len(without_repeats))
print(len(set(without_repeats)))
deck = list(range(1, 11))
print(deck)
random.shuffle(deck)
print(deck)
print(sorted(deck) == list(range(1, 11)))
Part 7: random - Seeding for Reproducibility#
random.seed(42)
sequence1 = [random.randint(1, 100) for _ in range(5)]
random.seed(42)
sequence2 = [random.randint(1, 100) for _ in range(5)]
print(sequence1)
print(sequence2)
print(sequence1 == sequence2)
Part 8: random - Distributions#
random.seed(1)
heights = [random.gauss(170, 10) for _ in range(1000)]
print(round(statistics.mean(heights), 1))
print(round(statistics.stdev(heights), 1))
wait_times = [random.expovariate(1 / 5) for _ in range(1000)]
print(round(statistics.mean(wait_times), 1))
Part 9: statistics - Central Tendency#
salaries = [45000, 48000, 51000, 49000, 250000]
print(statistics.mean(salaries))
print(statistics.median(salaries))
grades = [85, 90, 90, 78, 90, 85]
print(statistics.mode(grades))
print(statistics.multimode([1, 1, 2, 2, 3]))
Part 10: statistics - Spread: variance and stdev#
scores = [72, 85, 90, 68, 95, 77]
print(statistics.variance(scores))
print(statistics.stdev(scores))
print(statistics.pvariance(scores))
print(statistics.pstdev(scores))
print(round(statistics.stdev(scores) ** 2, 4) == round(statistics.variance(scores), 4))
Part 11: Common Patterns#
random.seed(7)
def generate_test_scores(n, mean=75, stdev=10):
scores = [round(random.gauss(mean, stdev)) for _ in range(n)]
return [max(0, min(100, s)) for s in scores]
sample_scores = generate_test_scores(10)
print(sample_scores)
def five_number_summary(data):
sorted_data = sorted(data)
return {
'min': min(sorted_data),
'q1': statistics.median(sorted_data[:len(sorted_data) // 2]),
'median': statistics.median(sorted_data),
'q3': statistics.median(sorted_data[(len(sorted_data) + 1) // 2:]),
'max': max(sorted_data)
}
data = [4, 8, 15, 16, 23, 42, 8, 15, 4, 30]
print(five_number_summary(data))
Wrap-Up: What You Learned#
- math: constants, sqrt, pow, floor, ceil, trunc, trig with radians/degrees, logs, exp.
- math: factorial, gcd, lcm, comb, perm, and isclose for safe float comparison.
- random: random, uniform, randint, randrange, each with different endpoint rules.
- random: choice, choices, sample, shuffle, and seed for reproducibility.
- random: gauss and expovariate for sampling from real statistical distributions.
- statistics: mean, median, mode, multimode for central tendency.
- statistics: variance, stdev, and their population variants pvariance and pstdev.
- Two real patterns: synthetic test data generation and a five-number summary.
- That wraps up math, random, and statistics. Next up: subprocess, for running external programs.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



