Module 14 · Python Modules and Packages — topic 6 of 13
Standard library
A tour of Python's 'batteries included' standard library: the main module groups (text, data structures, maths, dates, files, data formats, internet, concurrency, testing) and how to find what you need.
Learning objectives
- Name the main areas of the standard library
- Choose a suitable module for common tasks
- Find documentation for standard modules
Definition
The standard library is the large collection of modules that ships with every Python installation. It provides ready-made tools for text, numbers, dates, files, data formats, networking, concurrency, testing and more — no installation required.
Introduction
Before installing a package or writing a helper from scratch, check the standard library — Python probably already has it.
Why it matters
Standard modules are tested, documented, stable and available everywhere Python runs. Using them saves time and avoids extra dependencies.
Detailed explanation
Text: string, re (regular expressions), textwrap, difflib, unicodedata.
Data structures and algorithms: collections, heapq, bisect, itertools, functools, dataclasses, enum.
Numbers and maths: math, cmath, decimal, fractions, random, statistics.
Dates and time: datetime, time, calendar, zoneinfo.
Files and the operating system: os, sys, pathlib, shutil, glob, tempfile, zipfile, subprocess.
Data formats: json, csv, sqlite3, pickle, configparser, xml, tomllib. Internet: urllib, http, email, socket. Concurrency: threading, multiprocessing, asyncio, concurrent.futures.
Development: unittest, doctest, logging, argparse, pdb, timeit. help(module) and the official 'Library Reference' document everything; module 15 covers the most important modules in depth.
Because the standard library is installed with Python itself, code that uses only standard modules runs anywhere Python is available — on servers, school computers or a friend's laptop — with nothing to install first.
Syntax
- Ships with Python — no install.
- Grouped by area.
- help() and dir() explore modules.
Code examples
A few batteries
6 5 ABCDE Friday
Small but handy modules
The Python standard [...] True 28
More examples
Combining several modules
[('batteries', 2), ('included', 2), ("python's", 1)]
Report for 27 Nov 2026: 8 words, 6 unique
6Step-by-step explanation
- re extracts lower-case words, keeping apostrophes.
- collections.Counter finds the most frequent words.
- datetime formats the date and string.punctuation counts punctuation marks — four modules, no installation.
Expected output
Running “Combining several modules” prints exactly:
Practical applications
- Command-line tools built with argparse, logging and pathlib.
- Small data tasks with csv, json, statistics and sqlite3.
Best practices
- Search the standard library before installing packages.
- Read the module documentation for edge cases.
- Prefer standard modules for portable scripts.
Common mistakes
Reinventing the wheel
Avoid
Prefer
The standard version is tested and handles edge cases.
Important notes
- Some old modules have been removed in recent versions; the documentation marks deprecated ones.
Key takeaways
- The standard library ships with Python.
- It covers text, data, maths, dates, files, formats, networking and testing.
- Use help() and the docs to explore it.
Try it yourself
- 1Find a standard module that can compare two lists of lines and show differences, and use it.
- 2Use calendar to print the month of your birthday.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions cover the “Python Modules and Packages” module. Answers and explanations are revealed after you submit.
- ModerateWhat happens the second time a module is imported in the same program?
- ModerateCode under if __name__ == "__main__": runs when…
- ModerateWhat is printed?