█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/Python (Professional)/Python Standard Library Deep Dive
45 minIntermediate

Python Standard Library Deep Dive

After this lesson, you will be able to: Use the Python standard library effectively: collections, itertools, functools, datetime, os, subprocess.

Python ships with a vast standard library. Knowing what's there means not reinventing wheels.

Prerequisites:File I/O and Data

collections: better data structures

Beyond dict and list.

python
from collections import Counter, defaultdict, deque, namedtuple
# Counter, count occurrences
words = "the quick brown fox the lazy dog".split()
Counter(words).most_common(3)
# [('the', 2), ('quick', 1), ('brown', 1)]
# defaultdict, auto-create missing keys
by_tag = defaultdict(list)
for item in items:
by_tag[item.tag].append(item)
# No KeyError; auto-creates [] on first access
# deque, O(1) append/pop on both ends
q = deque(maxlen=100) # ring buffer
q.append("new")
q.popleft()
# namedtuple, lightweight class
Point = namedtuple("Point", ["x", "y"])
p = Point(1, 2)

itertools + functools

Powerful combinators.

python
from itertools import chain, groupby, islice, pairwise, batched
list(chain([1,2], [3,4])) # [1, 2, 3, 4]
list(islice(range(100), 5, 10)) # [5,6,7,8,9]
list(pairwise([1,2,3,4])) # [(1,2),(2,3),(3,4)]
list(batched([1,2,3,4,5,6,7], 3)) # [(1,2,3),(4,5,6),(7,)]
from functools import reduce, lru_cache, partial
reduce(lambda a, b: a*b, range(1, 5)) # 24 (factorial of 4)
@lru_cache(maxsize=128)
def expensive(n): return slow_compute(n)
add5 = partial(lambda a, b: a + b, 5)
add5(10) # 15

datetime + subprocess

Two pieces every script needs eventually.

python
from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc) # always work in UTC
tomorrow = now + timedelta(days=1)
now.isoformat()
datetime.fromisoformat("2026-05-23T10:00:00+00:00")
# subprocess (calling other programs)
import subprocess
result = subprocess.run(
["git", "log", "-n", "5", "--oneline"],
capture_output=True, text=True, check=True,
)
print(result.stdout)
# Never pass shell=True with user input (command injection)

Common mistakes only experienced Python devs avoid

Reinventing Counter or defaultdict by hand. Naive datetime (no timezone). Causes off-by-one-day bugs at edges. ALWAYS use timezone-aware. subprocess with shell=True + user input. Command injection. Always pass a list. Forgetting check=True on subprocess.run. Failure goes unnoticed.

Quick Check

Why use Counter instead of a manual dict?

Pick the cleanest answer.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←File I/O and Working with Data
Back to Python (Professional)
Testing in Python with pytest→