Syllabus Lesson 43 of 239 · Files, Errors & Modules
Files, Errors & Modules

A Tour of the Standard Library

Python ships with a huge toolbox so you rarely start from scratch. Here are four modules you will reach for constantly.

datetime

from datetime import date

d = date(2026, 6, 17)
print(d.year)        # 2026
print(d.isoformat()) # 2026-06-17

math

import math
print(math.sqrt(144))   # 12.0
print(math.floor(3.7))  # 3
print(math.pi)          # 3.1415...

random (seed it for repeatable results)

Random output changes each run, which is bad for tests. Call random.seed(n) first to make the sequence deterministic, so the same seed always gives the same numbers.

import random
random.seed(42)
print(random.randint(1, 6))  # same value every run with seed 42

collections.Counter

Counter tallies how often each item appears, returning a dict-like object:

from collections import Counter
tally = Counter("banana")
print(tally["a"])          # 3
print(tally.most_common(1)) # [('a', 3)]
Your turn

Use the standard library. (1) From the list words = ["red", "blue", "red", "green", "red", "blue"] build a Counter named counts and store the single most common word (just the word, not the count) in top_word. (2) Set root to math.sqrt(81). (3) Call random.seed(7) then store random.randint(1, 100) in pick.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output