Files, Errors and Modules · Working at Scale · lesson 6 of 8
A standard library tour
about 15 minutes · free · runs in your browser
Step 1 of 2
Batteries included
Python ships with a large library. Four modules earn their place in your head:
math — sqrt, floor, ceil, pi, inf.
random — randint(a, b) inclusive at both ends, choice(seq),
shuffle(list), sample(seq, k). Call random.seed(n) first and you get the same
"random" numbers every time, which is how you test code that uses it.
datetime — date, datetime and timedelta:
from datetime import date, timedelta
launch = date(2026, 1, 15)
launch + timedelta(days=30) # date(2026, 2, 14)
(other - launch).days # difference in days
collections — Counter counts things in one line, and defaultdict removes the
"is this key there yet?" dance:
from collections import Counter
Counter("mississippi").most_common(2) # [('i', 4), ('s', 4)]
Your turn: use Counter for the two most common words, and timedelta to find the
date 45 days after 1 March 2026.
You start from this, and edit it in the browser:
from collections import Counter
from datetime import date, timedelta
words = ["red", "blue", "red", "green", "blue", "red"]
# Set top_two and deadline.
Step 2 of 2
Deterministic randomness
Code that uses random is untestable until you seed it. random.seed(n) makes the
sequence repeatable, so the same seed always produces the same draws.
Your turn: write roll_dice(seed, count) that seeds the generator and returns
count dice rolls, each from 1 to 6.
You start from this, and edit it in the browser:
import random
def roll_dice(seed, count):
# Seed, then return count rolls of a six-sided die.
pass