Python Standard Libraries Handbook
Python Standard Libraries Handbook
While Python's built-in functions are always available, its Standard Library acts as an
expansive toolbox that you must explicitly open. These modules contain pre-written code—
functions, classes, and variables—designed to handle everything from complex math to
web requests.
This imports the whole module. You must prefix any function with the module's name. This
is best for avoiding name collisions.
import math
The Python
# You must link the module name to the function
Standard Libraries
result = [Link](25)
Handbook
2. SPECIFIC IMPORT (DIRECT ACCESS)
This imports only specific tools from the library. You can use them directly without the
prefix. This is best for frequently used tools.
This renames the module in your script for brevity. Heavily used in data science (e.g.,
pandas as pd).
import itertools as it
# Linked using the shortened alias
chained = [Link]([1,2], [3,4])
The Battery Included Philosophy: Python is famous for having "batteries included."
Before you try to write complex logic or download a third-party package, check this
handbook—chances are, the Standard Library already has a module built specifically for
your task.
1. Data Types & Structures
[Link]
WHAT IT DOES
A dict subclass for counting hashable objects. It creates a dictionary where elements
are stored as dictionary keys and their counts are stored as dictionary values.
WHEN TO USE IT
Whenever you need to count the frequency of items in a list or characters in a string.
HOW TO LINK IT
EXAMPLE
WHAT IT DOES
Provides classes for manipulating dates and times in both simple and complex ways.
WHEN TO USE IT
HOW TO LINK IT
Frequently linked with the `timedelta` object to add or subtract days/hours from a
current date.
EXAMPLE
today = [Link]()
# Linking datetime with timedelta
next_week = today + timedelta(days=7)
print(f"Next week is: {next_week.strftime('%Y-%m-%d')}")
2. File System & OS Interactivity
Modules designed to interact with the operating system, handle file paths, and manage
environments.
[Link]
import os
[Link](path, *paths)
[Link](path)
WHAT IT DOES
WHEN TO USE IT
When you need to build file paths dynamically or check if a file/folder exists before
opening it.
HOW TO LINK IT
Always link `[Link]` with dynamic strings rather than concatenating strings
with slashes to ensure cross-platform compatibility.
EXAMPLE
import os
folder = "documents"
filename = "[Link]"
# Safely link folder and filename
full_path = [Link](folder, filename)
print(full_path) # Output: documents/[Link] (or
documents\[Link] on Win)
[Link]
import sys
[Link]
WHAT IT DOES
A list in Python, which contains the command-line arguments passed to the script.
WHEN TO USE IT
When building command-line interface (CLI) tools and you need to read inputs
provided by the user directly from the terminal.
HOW TO LINK IT
Often linked with list slicing `[Link][1:]` to ignore the script name itself, and passed
to `argparse` for advanced parsing.
EXAMPLE
import sys
Modules for saving/loading structured data and performing advanced text pattern
matching.
[Link] / [Link]
import json
[Link](s) / [Link](obj)
WHAT IT DOES
`loads` parses a JSON string into a Python dictionary. `dumps` serializes a Python
dictionary into a formatted JSON string.
WHEN TO USE IT
Communicating with web APIs, saving configuration files, or passing structured data
between systems.
HOW TO LINK IT
Linked with Python's built-in `open()` function (using `[Link]()` and `[Link]()`)
to read/write JSON directly to files.
EXAMPLE
import json
import re
[Link](pattern, string)
[Link](pattern, repl, string)
WHAT IT DOES
WHEN TO USE IT
Extracting specific formats (emails, phone numbers) from messy text, or mass-
replacing complex string patterns.
HOW TO LINK IT
Often linked with `.group()` to extract the exact matched text from the regex match
object.
EXAMPLE
import re
[Link] / [Link]
import math
[Link](x) / [Link](x)
WHAT IT DOES
`ceil` returns the smallest integer greater than or equal to x. `floor` returns the largest
integer less than or equal to x.
WHEN TO USE IT
When you need strict directional rounding rather than standard proximity rounding
(e.g., calculating how many pages you need for pagination).
HOW TO LINK IT
EXAMPLE
import math
total_items = 52
items_per_page = 10
# Math ceiling ensures any remainder creates a new page
pages_needed = [Link](total_items / items_per_page)
print(f"Pages: {pages_needed}") # Output: Pages: 6
[Link] / [Link]
import random
[Link](seq)
[Link](a, b)
WHAT IT DOES
WHEN TO USE IT
HOW TO LINK IT
EXAMPLE
import random
The 'itertools' module is a collection of tools for handling iterators. They are fast, memory-
efficient, and designed to be linked together.
[Link]
WHAT IT DOES
Makes an iterator that returns elements from the first iterable until it is exhausted,
then proceeds to the next iterable, until all of the iterables are exhausted.
WHEN TO USE IT
When you need to treat multiple lists or sequences as a single sequence without
actually concatenating them in memory.
HOW TO LINK IT
Highly effective when linked in a `for` loop, acting as a bridge between disparate data
sources.
EXAMPLE
list_a = [1, 2, 3]
list_b = [4, 5, 6]
# Iterating through both without creating a new combined list
for num in chain(list_a, list_b):
print(num, end=' ') # Output: 1 2 3 4 5 6
[Link]
WHAT IT DOES
WHEN TO USE IT
When solving probability tasks, scheduling, or finding all possible unique pairings in
a dataset.
HOW TO LINK IT
EXAMPLE