Lesson 7 of 7 · 9 min · 2 hands-on steps

Python's Batteries: The Standard Library

Dates, random numbers, counters, JSON and math, all built in.

Python comes with batteries included

Python ships with a huge standard library: ready-made modules for common jobs. You import what you need.

Python · try it
import math
import random
from datetime import date, timedelta

print(math.sqrt(144), math.pi)
print(random.choice(["rock", "paper", "scissors"]))
today = date(2026, 10, 24)
print(today + timedelta(days=30))

collections: smarter data structures

Python · try it
from collections import Counter, defaultdict

votes = ["tea", "coffee", "tea", "water", "tea"]
print(Counter(votes).most_common(2))

groups = defaultdict(list)
for name in ["Ada", "Alan", "Bo", "Bea"]:
    groups[name[0]].append(name)
print(dict(groups))

json: talk to the web

APIs send JSON. Python turns it into dictionaries and back:

Python · try it
import json

text = '{"name": "Ada", "skills": ["Python", "SQL"]}'
data = json.loads(text)
print(data["skills"][0])
print(json.dumps({"ok": True, "count": 3}))

Packages beyond the standard library

For everything else there's pip and PyPI, with hundreds of thousands of packages (requests, pandas, fastapi…). You'll install them on your own computer in later courses. Learning what's already built in first means you don't add packages you don't need.

🧪 Practice: 2 steps

0 / 2 done

Step 1 of 2 · Most common word

Use collections.Counter to write most_common_word(text) that returns the most frequent word (lowercased).

Python
from collections import Counter

def most_common_word(text):
    pass

Output appears here. Press Run or Ctrl/⌘ + Enter.

✅ Check your understanding

  1. What does import do?

Create a free account to track your progress.