Exploring Built-in Python Tools

The Python standard library ships hundreds of modules that cover a huge range of common tasks — filesystem access, dates and times, HTTP, regular expressions, CSV, JSON, math, random numbers, subprocesses, compression, hashing, logging, testing. These modules require no pip install; they are right there with the interpreter. Learning which one to reach for is one of the highest-leverage skills you can build.

Batteries included is the project's slogan, and the standard library delivers on it. Before adding a PyPI dependency to a project, ask: is there a standard-library module that does 80% of what I need? The savings are real — fewer security updates, no version conflicts, instant availability on any machine with Python installed.

Modules are imported the same way regardless of source: import json, from pathlib import Path, import datetime as dt. Some modules are small enough that a single import statement gives you everything; others (like collections or itertools) are bags of useful helpers you reach for by name.

The docs at docs.python.org/3/library/ are organized by topic and are the canonical reference. Every module page starts with a one-line summary and then a detailed API. Skim the index once; bookmark the pages for the areas you work in most.

A tour of the most useful modules

os and pathlib handle files and directories. datetime and time deal with dates. json and csv read/write structured data. re matches text with regular expressions. math and statistics cover numeric work. random and secrets generate random values (secrets for security-sensitive cases).

collections (Counter, defaultdict, deque, namedtuple), itertools (combinators for iterators), and functools (caching, partial, reduce) round out the data-processing toolkit. Learn them and a large fraction of everyday Python becomes one-liners.

Discovering and exploring at the REPL

import module; help(module) prints the docstring and contents of a module. dir(module) lists its attributes; inspect.signature(func) shows the signature without reading source. These three tools let you explore any module, standard library or not, from an interactive shell.

The python -m form runs a module as a script: python -m http.server spins up a quick web server, python -m json.tool in.json pretty-prints JSON. Dozens of modules have useful CLIs; skim the docs to spot them.

A hand-picked subset of the standard library you will use constantly.

ToolPurpose
pathlib
module
Object-oriented filesystem paths.
datetime
module
Dates, times, and durations.
json
module
Read and write JSON documents.
re
module
Regular expression matching.
collections
module
Counter, defaultdict, deque, namedtuple.
itertools
module
Iterator building blocks (chain, groupby, ...).
functools
module
lru_cache, partial, reduce, cached_property.
random
module
Random numbers, shuffle, choice.

Exploring Built-in Python Tools code example

The script below solves one small task with six different standard-library modules.

# Lesson: Exploring Built-in Python Tools
import hashlib
import json
import math
import random
from collections import Counter
from datetime import date
from itertools import accumulate
from pathlib import Path

random.seed(7)
rolls = [random.randint(1, 6) for _ in range(20)]

print("rolls:", rolls)
print("counts:", Counter(rolls).most_common())
print("running sum:", list(accumulate(rolls)))

today = date.today().isoformat()
summary = {
    "date": today,
    "count": len(rolls),
    "sum": sum(rolls),
    "avg": sum(rolls) / len(rolls),
    "sqrt_sum": round(math.sqrt(sum(rolls)), 3),
}
print("summary:", summary)

payload = json.dumps(summary, sort_keys=True).encode("utf-8")
digest = hashlib.sha256(payload).hexdigest()[:12]
print("digest:", digest)

# pathlib as a lightweight "where am I?" check
here = Path.cwd()
print("cwd:   ", here)
print("parent:", here.parent.name)

Notice how every line leans on one standard-library module:

1) `random.seed(7)` makes the rolls reproducible for demo purposes.
2) `Counter.most_common()` and `itertools.accumulate` turn loops into one-liners.
3) `json.dumps` + `hashlib.sha256` creates a deterministic fingerprint in two lines.
4) `pathlib.Path.cwd()` replaces `os.getcwd()` with a richer object.

Explore two modules at the REPL.

# Example A: iterate pairwise with itertools
from itertools import pairwise  # Python 3.10+
nums = [1, 3, 6, 10]
for a, b in pairwise(nums):
    print(a, "->", b)

# Example B: cached_property vs property
from functools import cached_property
class Data:
    def __init__(self, n): self.n = n
    @cached_property
    def total(self):
        print("  computing...")
        return sum(range(self.n))

d = Data(1000)
print(d.total, d.total)  # "computing" prints only once

Confirm the behavior of a few helpers.

import math
from collections import Counter
from itertools import accumulate
assert math.sqrt(16) == 4.0
assert Counter("abc") == {"a": 1, "b": 1, "c": 1}
assert list(accumulate([1, 2, 3])) == [1, 3, 6]

Running prints (random numbers are stable because of the seed):

rolls: [1, 5, 2, 4, 3, 4, 2, 4, 3, 4, 1, 4, 5, 2, 6, 4, 6, 6, 2, 4]
counts: [(4, 6), (2, 4), (6, 3), (5, 2), (3, 2), (1, 2)]
running sum: [1, 6, 8, 12, 15, 19, 21, 25, 28, 32, 33, 37, 42, 44, 50, 54, 60, 66, 68, 72]
summary: {'date': '2026-04-21', 'count': 20, 'sum': 72, 'avg': 3.6, 'sqrt_sum': 8.485}
digest: d0c29c2ebf43
cwd:    /home/you/demo
parent: you