Skip to content

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.

Beginner 10 min read·Lesson 214 of 306 in Python Programming

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

Python
import math, statistics, datetime
help(statistics) # documentation in the console
print(dir(statistics)) # names it provides
  • Ships with Python — no install.
  • Grouped by area.
  • help() and dir() explore modules.

Code examples

A few batteries

Python
import datetime
import math
import statistics
import string
print(math.gcd(12, 18), statistics.median([5, 1, 9]), string.ascii_uppercase[:5])
print(datetime.date(2026, 11, 27).strftime("%A"))
Output
6 5 ABCDE
Friday

Small but handy modules

Python
import calendar
import textwrap
print(textwrap.shorten("The Python standard library is very large and useful", width=30))
print(calendar.isleap(2028), calendar.monthrange(2026, 2)[1])
Output
The Python standard [...]
True 28

More examples

Combining several modules

Python
import re
import string
from collections import Counter
from datetime import date
text = "Python's standard library: batteries included. Batteries! Included, indeed."
words = re.findall(r"[a-z']+", text.lower())
print(Counter(words).most_common(3))
print(f"Report for {date(2026, 11, 27):%d %b %Y}: {len(words)} words, {len(set(words))} unique")
print(sum(ch in string.punctuation for ch in text))
Output
[('batteries', 2), ('included', 2), ("python's", 1)]
Report for 27 Nov 2026: 8 words, 6 unique
6

Step-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:

TextOutput
[('batteries', 2), ('included', 2), ("python's", 1)]
Report for 27 Nov 2026: 8 words, 6 unique
6

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

def mean(xs):
return sum(xs) / len(xs) # plus median, mode...

Prefer

from statistics import mean, median, mode

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

  1. 1
    Find a standard module that can compare two lists of lines and show differences, and use it.
  2. 2
    Use 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?
Practise this module