The Standard Library
math, random, datetime, collections, and itertools.
6 minute read
Big idea
Python comes with batteries included: a standard library of modules for maths, randomness, dates, counting, and much more. Before writing something yourself, check whether it's already there.
math
import math
math.sqrt(2) # 1.4142135623730951
math.gcd(12, 18) # 6 — greatest common divisor
math.isclose(0.1 + 0.2, 0.3) # True — compare floats safely
random
import random
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))
cards = ["A", "K", "Q", "J"]
random.shuffle(cards)
print(cards)Run and edit it in the app.
randint(a, b)— a whole number from a to b, both included.choice(items)— one random item.shuffle(items)— mixes a list in place.random.seed(42)— makes the "random" sequence repeat exactly; handy for testing.
datetime
from datetime import date, timedelta
launch = date(2026, 10, 3)
print(launch)
print(launch.strftime("%d %B %Y"))
print(launch + timedelta(days=30))
print((date(2026, 12, 25) - launch).days, "days to go")output
2026-10-03
03 October 2026
2026-11-02
83 days to goRun and edit it in the app.
date.today() and datetime.now() give the current date and time. Subtracting dates gives a timedelta, a length of time.
collections
Two ready-made containers you'll use constantly:
from collections import Counter, defaultdict
votes = ["tea", "coffee", "tea", "juice", "tea", "coffee"]
tally = Counter(votes)
print(tally.most_common(2))
print(tally["juice"], tally["water"])
by_letter = defaultdict(list)
for word in ["apple", "avocado", "banana"]:
by_letter[word[0]].append(word)
print(dict(by_letter))What does it print? Show the answer
[('tea', 3), ('coffee', 2)]
1 0
{'a': ['apple', 'avocado'], 'b': ['banana']}Run and edit it in the app.
Counter— counts things for you; a missing key counts as 0 instead of raisingKeyError.defaultdict(list)— a dict that creates an empty list the first time you use a new key.
itertools
Tools for looping in clever ways:
from itertools import combinations, permutations, product
print(list(combinations("ABC", 2)))
print(len(list(permutations("ABC"))))
print(list(product([0, 1], repeat=2)))output
[('A', 'B'), ('A', 'C'), ('B', 'C')]
6
[(0, 0), (0, 1), (1, 0), (1, 1)]Run and edit it in the app.
combinations(items, k)— every group of k, order ignored.permutations(items)— every ordering.product(a, b)— every pairing, like nested loops.
from collections import Counter
counts = Counter("mississippi")
print(counts.most_common(1)[0][1])output
iRun and edit it in the app.
most_common(1) is a list holding one (letter, count) pair. [0] takes the pair; [1] took the count. The letter is at [0][0] — but careful: i and s both appear 4 times, and ties keep the order they were first seen, so it's i.
A few more worth knowing
osandpathlib— files and folders.json— read and write JSON data (you'll use it in the project).statistics— mean, median, standard deviation.time—time.sleep(1)pauses for a second.
Which values can random.randint(1, 3) return?
Watch it
Think of it like
Moving into a furnished flat. The kitchen already has pans, the drawers have cutlery. You could forge your own spoon — but why would you?
Key terms
- Standard library — The modules that come with every Python install.
- Counter — A dict subclass that counts items.
- timedelta — A length of time, from subtracting dates or for adding to one.
Check yourself
Can random.randint(1, 6) return 6?
Counter("aab") gives what count for 'a'?
What does date(2026, 1, 10) - date(2026, 1, 3) give?