math, random, and statistics

What you'll learn
- The most useful functions in
math: roots, rounding, logarithms, and counting combinations - How to compare floats safely with
math.isclose() - How to pick random numbers, items, and samples with
random, and repeat them withseed() - Why passwords and tokens need
secrets, notrandom - How to describe data with
statistics: mean, median, mode, spread, quantiles
Why it matters
A delivery app calculates distances, a game rolls dice, a teacher finds the class average. The standard library already has these calculations, tested and fast, in three modules: math, random, and statistics.
The math module
Roots, powers, and constants
import math
print(math.sqrt(144))
print(math.pow(2, 10))
print(2 ** 10)
print(math.pi)
print(math.e)12.0
1024.0
1024
3.141592653589793
2.718281828459045math.pow() always returns a float, while ** keeps integers as integers. math.isqrt(150) returns the whole-number root, 12.
Rounding down and up
import math
print(math.floor(7.9), math.ceil(7.1))
print(math.floor(-2.5), math.ceil(-2.5))
print(math.trunc(-2.5))
print(f"Book {math.ceil(47 / 12)} buses") # 47 students, 12 seats per bus7 8
-3 -2
-2
Book 4 busesfloor always goes down and ceil always goes up, even for negative numbers, and trunc cuts off the decimal part. ceil is perfect for "how many do I need?" questions.
Comparing floats with isclose
Floats are stored in binary, so tiny rounding errors appear:
import math
total = 0.1 + 0.2
print(total)
print(total == 0.3)
print(math.isclose(total, 0.3))0.30000000000000004
False
TrueNever compare calculated floats with ==. Use math.isclose(a, b), which returns True when the numbers are nearly equal.
There is one catch: isclose measures closeness relative to the size of the numbers, so a comparison with 0 needs an absolute tolerance, abs_tol:
import math
left = 0.1 + 0.2 - 0.3
print(left)
print(math.isclose(left, 0))
print(math.isclose(left, 0, abs_tol=1e-9))5.551115123125783e-17
False
TrueFor very accurate sums of many floats, there is math.fsum():
import math
prices = [0.1] * 10
print(sum(prices))
print(math.fsum(prices))1.0
1.0Since Python 3.12, the built-in sum() also reduces these rounding errors (older versions print 0.9999999999999999), and math.fsum() is accurate on every version.
Logarithms
import math
print(math.log10(1000))
print(math.log2(1024))
print(math.log(math.e))
print(math.log(81, 3))3.0
10.0
1.0
4.0math.log(x) without a second argument uses base e.
Counting: factorial, combinations, permutations
import math
print(math.factorial(5)) # 5 * 4 * 3 * 2 * 1
print(math.comb(10, 3)) # teams of 3 from a class of 10 (order does not matter)
print(math.perm(10, 3)) # gold, silver, bronze among 10 runners (order matters)120
120
720The random module
random produces pseudo-random numbers from a formula that starts from a seed. The same seed gives the same sequence, so every example below calls random.seed() first and your output matches the page.
Random numbers
import random
random.seed(10)
print(random.random()) # a float from 0.0 up to (not including) 1.0
print(random.randint(1, 6)) # an int from 1 to 6, both ends included
print(random.uniform(20, 35)) # a float between 20 and 35
print(random.randrange(0, 100, 5)) # like range(): 0, 5, 10, ... 950.5714025946899135
4
27.23842511826284
0Notice randint(1, 6) includes 6, unlike range(1, 6).
Random items from a list
import random
random.seed(3)
cities = ["Tashkent", "Samarkand", "Bukhara", "Khiva", "Namangan"]
print(random.choice(cities)) # one item
print(random.sample(cities, 3)) # 3 different items
print(random.choices(cities, k=4)) # 4 items, repeats allowedSamarkand
['Namangan', 'Samarkand', 'Khiva']
['Namangan', 'Bukhara', 'Bukhara', 'Khiva']Weighted choices
choices can make some options more likely than others with weights:
import random
random.seed(4)
prizes = ["discount 5%", "discount 20%", "free tea"]
weights = [70, 10, 20] # 70% / 10% / 20% chance
spins = random.choices(prizes, weights=weights, k=10)
print(spins)
print(spins.count("discount 5%"), spins.count("discount 20%"), spins.count("free tea"))['discount 5%', 'discount 5%', 'discount 5%', 'discount 5%', 'discount 5%', 'discount 5%', 'free tea', 'free tea', 'discount 20%', 'discount 5%']
7 1 2The counts (7, 1, 2) match the weights here by luck; only with thousands of spins do they reliably get close.
Shuffling
shuffle changes the list in place and returns None:
import random
random.seed(5)
queue = ["Aziza", "Bekzod", "Dilnoza", "Farrukh"]
random.shuffle(queue)
print(queue)['Aziza', 'Bekzod', 'Farrukh', 'Dilnoza']Seeds make results reproducible
import random
random.seed(2026)
first = [random.randint(1, 100) for _ in range(5)]
random.seed(2026)
second = [random.randint(1, 100) for _ in range(5)]
print(first)
print(first == second)[16, 41, 65, 66, 83]
TrueSeeds are useful for testing and repeatable simulations. A real game normally does not set one.
secrets: for passwords and tokens
random is fine for games and simulations, but it is predictable: someone who sees enough outputs can work out the next ones. For passwords, reset codes, and security tokens, use the secrets module:
import secrets
import string
alphabet = string.ascii_letters + string.digits
password = "".join(secrets.choice(alphabet) for _ in range(12))
print(password)
print(secrets.randbelow(1_000_000)) # e.g. a 6-digit SMS code
print(secrets.token_hex(8)) # a random token for a linkq7RkX2mLp9Bs
482913
3fa91c07d25e6b84Your output differs every time, and there is no seed. That is the point.
The statistics module
Averages: mean, median, mode
import statistics
scores = [72, 85, 90, 85, 64, 98, 85, 70]
print(statistics.mean(scores))
print(statistics.median(scores))
print(statistics.mode(scores))81.125
85.0
85- mean: the usual average (sum divided by count).
- median: the middle value after sorting. With an even count, it is the average of the two middle values.
- mode: the most common value.
The median is useful when a few extreme values would distort the mean:
import statistics
salaries = [4_000_000, 4_500_000, 5_000_000, 5_200_000, 60_000_000]
print(f"mean: {statistics.mean(salaries):,.0f} som")
print(f"median: {statistics.median(salaries):,.0f} som")mean: 15,740,000 som
median: 5,000,000 somOne director's salary pulled the mean far up; the median describes a typical salary better.
If several values tie for most common, statistics.multimode() returns all of them as a list.
Spread: standard deviation
The standard deviation measures how far values usually are from the mean:
import statistics
class_a = [70, 72, 74, 76, 78]
class_b = [50, 60, 74, 88, 98]
for name, marks in [("A", class_a), ("B", class_b)]:
avg = statistics.mean(marks)
spread = statistics.stdev(marks)
print(f"Class {name}: mean {avg}, stdev {spread:.1f}")Class A: mean 74, stdev 3.2
Class B: mean 74, stdev 19.6Both classes average 74, but class B's results are spread much wider.
stdev treats the data as a sample of a bigger group; pstdev treats it as the whole population and gives a slightly smaller value (2.828 instead of 3.162 for class A).
statistics.quantiles(data) returns the three quartile cut points, with the median in the middle.
Try it
Roll dice, pick random winners, and describe the results with statistics. Change the seed and see what changes.
Common mistakes
1. Using the result of shuffle
import random
names = ["Aziza", "Bekzod", "Dilnoza"]
shuffled = random.shuffle(names)
print(shuffled)Noneshuffle changes the list in place and returns None. Fix: call random.shuffle(names) and then use names. To get a new shuffled list and keep the original, use random.sample(names, len(names)).
2. Asking for a bigger sample than the list
import random
random.sample(["Aziza", "Bekzod"], 3)ValueError: Sample larger than population or is negativesample never repeats items, so it cannot pick 3 from 2. Use choices if repeats are fine.
3. Statistics on empty data
import statistics
statistics.mean([])statistics.StatisticsError: mean requires at least one data pointCheck if data: before calculating, just like you would before dividing by len(data).
Practice
1. Paint calculator
A wall is 14.5 m² and one can of paint covers 4 m². Print how many cans to buy (whole cans only). Then check with math.isclose whether 0.1 * 3 equals 0.3.
Solution
import math
wall = 14.5
per_can = 4
print(math.ceil(wall / per_can))
print(math.isclose(0.1 * 3, 0.3))4
True14.5 / 4 is 3.625, and you cannot buy part of a can, so round up.
2. Grade statistics
For the grades [88, 92, 79, 65, 92, 100, 71, 84], print the mean (1 decimal), median, mode, and standard deviation (1 decimal).
Solution
import statistics
grades = [88, 92, 79, 65, 92, 100, 71, 84]
print(f"Mean: {statistics.mean(grades):.1f}")
print(f"Median: {statistics.median(grades)}")
print(f"Mode: {statistics.mode(grades)}")
print(f"Stdev: {statistics.stdev(grades):.1f}")Mean: 83.9
Median: 86.0
Mode: 92
Stdev: 11.73. Lottery draw
A lottery draws 6 different numbers from 1 to 49. Use random.seed(99) and print the draw sorted. Then use math.comb to print how many different tickets are possible and the chance of winning as "1 in ...".
Solution
import math
import random
random.seed(99)
draw = sorted(random.sample(range(1, 50), 6))
print("Winning numbers:", draw)
tickets = math.comb(49, 6)
print(f"Possible tickets: {tickets:,}")
print(f"Chance of winning: 1 in {tickets:,}")Winning numbers: [12, 13, 15, 25, 26, 39]
Possible tickets: 13,983,816
Chance of winning: 1 in 13,983,816The order of numbers on a ticket does not matter, so comb is the right counting tool.
4. Random password generator
Write make_password(length=12) using secrets and string that returns a password with letters, digits, and the symbols !@#$%. Make sure the password contains at least one digit by generating again until it does. Print its length and whether it has a digit (the password itself varies).
Solution
import secrets
import string
def make_password(length=12):
alphabet = string.ascii_letters + string.digits + "!@#$%"
while True:
password = "".join(secrets.choice(alphabet) for _ in range(length))
if any(ch.isdigit() for ch in password):
return password
pw = make_password()
print(len(pw))
print(any(ch.isdigit() for ch in pw))12
TruePrinting pw itself would show a different value on every run.
5. Dice game simulation
Bekzod and Aziza each roll two dice; the higher total wins the round, and equal totals are a draw. Simulate 1,000 rounds with random.seed(12). Print the wins for each player, the draws, and Bekzod's win percentage (1 decimal). Then print the mean and median of all Bekzod's totals.
Solution
import random
import statistics
def roll_two():
return random.randint(1, 6) + random.randint(1, 6)
random.seed(12)
results = {"Bekzod": 0, "Aziza": 0, "draw": 0}
bekzod_totals = []
for _ in range(1000):
bekzod = roll_two()
aziza = roll_two()
bekzod_totals.append(bekzod)
if bekzod > aziza:
results["Bekzod"] += 1
elif aziza > bekzod:
results["Aziza"] += 1
else:
results["draw"] += 1
print(results)
print(f"Bekzod won {results['Bekzod'] / 1000:.1%}")
print("Mean total:", statistics.mean(bekzod_totals))
print("Median total:", statistics.median(bekzod_totals)){'Bekzod': 447, 'Aziza': 454, 'draw': 99}
Bekzod won 44.7%
Mean total: 6.976
Median total: 7.0The players have equal chances, and the mean total is near 7, the theoretical average for two dice.
Recap
mathhassqrt,floor/ceil,log,factorial,comb/perm, and the constantspiande.- Compare calculated floats with
math.isclose(), not==, and usemath.fsum()for accurate float sums. randomgivesrandom(),randint(a, b)(both ends included),uniform,choice,choices(withweights),sample, andshuffle(in place, returnsNone);seed(n)makes the sequence repeatable.- Use
secrets, neverrandom, for passwords, codes, and tokens. statisticsdescribes data:mean,median,mode,stdev, andquantiles.