Stdlib, Testing & Tooling
Batteries included
Section titled “Batteries included”Python ships with a philosophy known as “batteries included”: the standard library provides ready-made implementations for the tasks most programs need — no third-party packages required. When you install Python you automatically get modules for collections, data manipulation, networking, file I/O, testing, serialisation, concurrency, and much more.
The standard library spans more than 200 modules.
Before reaching for pip, check whether a stdlib module already solves your problem — it will be faster to import, always compatible with your Python version, and trusted by millions of users.
The modern Python tooling landscape
Section titled “The modern Python tooling landscape”A production Python project typically relies on a small set of tools:
venv— creates isolated virtual environments so dependencies do not bleed across projects.pip— installs third-party packages from PyPI. Should only be used after confirming the stdlib cannot help.pyproject.toml— the modern, PEP-517-standardised file for declaring package metadata, build backends, and tool configuration (replacing the oldsetup.py+setup.cfgapproach).pytest— the de-facto test runner for Python projects, building on top of the stdlibunittestinfrastructure with a friendlier API.
You will explore unittest and pytest in later lessons in this chapter.
For now, the key habit is: reach for the stdlib first.
A quick taste: three stdlib modules in action
Section titled “A quick taste: three stdlib modules in action”The demo below uses three modules you will encounter constantly:
collections.Counter— counts hashable objects in a single pass.itertools.islice— lazily slices any iterator without materialising it first.json— serialises Python objects to JSON strings and deserialises them back.
import collectionsimport itertoolsimport json
# Count word frequencieswords = "the quick brown fox jumps over the lazy dog the fox".split()counter = collections.Counter(words)print("Top 3 words:", counter.most_common(3))
# First 5 squares using itertoolssquares = list(itertools.islice((x*x for x in range(1, 100)), 5))print("First 5 squares:", squares)
# JSON round-trippayload = json.dumps({"lang": "Python", "batteries": True})data = json.loads(payload)print("Parsed:", data["lang"], "batteries:", data["batteries"])Loading Python runtime (first run only)…