Mathew K Analytics

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,…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Python 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]))
3.141592653589793
0.1347965613844937
3

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))
3.141592653589793
2.718281828459045
4.0
1024.0
1024
4
5
4
-4

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))
1.0
1.0
3.141592653589793
180.0
0.5
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))
1.0
2.0
3.0
3.0
2.718281828459045
1.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))
120
6
12
10
20
120
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))
False
True
True
False

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])
True
True
True
True

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)))
True
6
3
3
deck = list(range(1, 11))
print(deck)
random.shuffle(deck)
print(deck)
print(sorted(deck) == list(range(1, 11)))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 9, 4, 1, 3, 10, 6, 7, 5, 8]
True

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)
[82, 15, 4, 95, 36]
[82, 15, 4, 95, 36]
True

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))
169.9
10.0
4.8

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]))
88600
49000
90
[1, 2]

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))
111.76666666666667
10.571975532825768
93.13888888888889
9.65084912786895
True

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)
[72, 80, 73, 72, 66, 73, 86, 79, 85, 77]
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))
{'min': 4, 'q1': 8, 'median': 15.0, 'q3': 23, 'max': 42}

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.