Standard Library

Essential modules included with Python.

datetime - Dates and Times

from datetime import datetime, date, time, timedelta

# Current date/time
now = datetime.now()
today = date.today()

# Create specific date/time
d = date(2024, 12, 25)
t = time(14, 30, 0)
dt = datetime(2024, 12, 25, 14, 30, 0)

# Components
dt.year, dt.month, dt.day
dt.hour, dt.minute, dt.second
dt.weekday()      # 0=Monday, 6=Sunday

# Formatting
dt.strftime("%Y-%m-%d %H:%M:%S")  # "2024-12-25 14:30:00"
dt.strftime("%B %d, %Y")          # "December 25, 2024"

# Parsing
datetime.strptime("2024-12-25", "%Y-%m-%d")

# Arithmetic
tomorrow = today + timedelta(days=1)
diff = date(2024, 12, 31) - today
diff.days

# ISO format
dt.isoformat()                    # "2024-12-25T14:30:00"
datetime.fromisoformat("2024-12-25T14:30:00")

Common Format Codes

CodeMeaningExample
%YYear (4 digit)2024
%mMonth (zero-padded)01-12
%dDay (zero-padded)01-31
%HHour (24-hour)00-23
%MMinute00-59
%SSecond00-59
%BMonth nameDecember
%AWeekday nameWednesday

os - Operating System Interface

import os

# Environment variables
os.environ["HOME"]
os.environ.get("API_KEY", "default")
os.getenv("API_KEY", "default")

# Current directory
os.getcwd()
os.chdir("/path/to/dir")

# Execute commands
os.system("ls -la")  # Runs in shell

# Process info
os.getpid()          # Process ID
os.getppid()         # Parent process ID

# File operations (prefer pathlib)
os.path.exists("file.txt")
os.path.isfile("file.txt")
os.path.isdir("directory")
os.path.join("dir", "file.txt")
os.path.basename("/path/to/file.txt")  # "file.txt"
os.path.dirname("/path/to/file.txt")   # "/path/to"
os.path.splitext("file.txt")           # ("file", ".txt")

# Directory operations
os.makedirs("path/to/dir", exist_ok=True)
os.listdir(".")
os.remove("file.txt")
os.rmdir("empty_dir")
os.rename("old", "new")

sys - System-Specific Parameters

import sys

# Command line arguments
sys.argv           # ["script.py", "arg1", "arg2"]
sys.argv[0]        # Script name
sys.argv[1:]       # Arguments

# Python info
sys.version        # Python version string
sys.version_info   # (3, 12, 0, 'final', 0)
sys.platform       # "linux", "darwin", "win32"
sys.executable     # Path to Python interpreter

# Standard streams
sys.stdin
sys.stdout
sys.stderr
print("Error", file=sys.stderr)

# Exit
sys.exit(0)        # Success
sys.exit(1)        # Error
sys.exit("Error message")

# Module search path
sys.path
sys.path.append("/custom/path")

re - Regular Expressions

import re

text = "Contact: email@example.com or phone: 555-1234"

# Search (first match)
match = re.search(r"\d{3}-\d{4}", text)
if match:
    print(match.group())  # "555-1234"

# Find all matches
emails = re.findall(r"\S+@\S+", text)  # ["email@example.com"]

# Match at beginning
if re.match(r"Contact", text):
    print("Starts with Contact")

# Replace
result = re.sub(r"\d", "X", text)  # Replace digits with X

# Split
parts = re.split(r"\s+", text)    # Split on whitespace

# Compile for reuse
pattern = re.compile(r"\d{3}-\d{4}")
pattern.findall(text)

Common Patterns

PatternMeaning
\dDigit
\wWord character (letter, digit, underscore)
\sWhitespace
.Any character
*0 or more
+1 or more
?0 or 1
{n}Exactly n
{n,m}n to m
^Start of string
$End of string
[abc]Character class
[^abc]Not in class
(...)Capture group
(?:...)Non-capturing group

collections - Container Datatypes

from collections import Counter, defaultdict, deque, namedtuple

# Counter - count occurrences
counts = Counter("abracadabra")
counts.most_common(3)        # [('a', 5), ('b', 2), ('r', 2)]

# defaultdict - dict with default factory
d = defaultdict(list)
d["key"].append(1)           # No KeyError

# deque - double-ended queue
dq = deque([1, 2, 3], maxlen=5)
dq.append(4)                 # Right
dq.appendleft(0)             # Left
dq.popleft()                 # O(1) unlike list

# namedtuple - tuple with named fields
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
p.x, p.y                     # 3, 4

itertools - Iterator Tools

from itertools import (
    chain, combinations, permutations,
    groupby, islice, cycle, repeat, count
)

# Chain iterables
list(chain([1, 2], [3, 4]))       # [1, 2, 3, 4]

# Combinations and permutations
list(combinations([1, 2, 3], 2))  # [(1,2), (1,3), (2,3)]
list(permutations([1, 2, 3], 2))  # [(1,2), (1,3), (2,1), (2,3), (3,1), (3,2)]

# Groupby (requires sorted input)
data = [("a", 1), ("a", 2), ("b", 3)]
for key, group in groupby(data, key=lambda x: x[0]):
    print(key, list(group))

# Slice iterator
list(islice(range(100), 5, 10))   # [5, 6, 7, 8, 9]

# Infinite iterators
for i in islice(count(10), 5):    # 10, 11, 12, 13, 14
    print(i)

functools - Higher-Order Functions

from functools import reduce, partial, lru_cache, wraps

# Reduce - apply function cumulatively
reduce(lambda a, b: a + b, [1, 2, 3, 4])  # 10

# Partial - freeze some arguments
def power(base, exp):
    return base ** exp
square = partial(power, exp=2)
square(5)  # 25

# lru_cache - memoization
@lru_cache(maxsize=128)
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

# wraps - preserve function metadata in decorators
def my_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

random - Random Numbers

import random

# Random float [0.0, 1.0)
random.random()

# Random integer [a, b] inclusive
random.randint(1, 10)

# Random from range
random.randrange(0, 100, 5)  # Multiples of 5

# Random choice
random.choice(["a", "b", "c"])
random.choices(["a", "b", "c"], k=5)        # With replacement
random.sample(["a", "b", "c", "d"], k=2)    # Without replacement

# Shuffle in place
items = [1, 2, 3, 4, 5]
random.shuffle(items)

# Random float in range
random.uniform(1.0, 10.0)

# Reproducible results
random.seed(42)

json - JSON Encoding/Decoding

import json

# Python to JSON
data = {"name": "Alice", "age": 30, "items": [1, 2, 3]}
json_str = json.dumps(data)
json_str = json.dumps(data, indent=2)  # Pretty print

# JSON to Python
data = json.loads('{"name": "Alice"}')

# File I/O
with open("data.json", "w") as f:
    json.dump(data, f)

with open("data.json") as f:
    data = json.load(f)

# Custom encoding
class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        return super().default(obj)

json.dumps(data, cls=DateEncoder)

subprocess - Run External Commands

import subprocess

# Simple command
result = subprocess.run(["ls", "-la"], capture_output=True, text=True)
print(result.stdout)
print(result.returncode)

# With shell
result = subprocess.run("ls -la | grep py", shell=True, capture_output=True, text=True)

# Check for errors
result = subprocess.run(["false"], check=True)  # Raises CalledProcessError

# Pipe input
result = subprocess.run(
    ["grep", "pattern"],
    input="line1\npattern here\nline3",
    capture_output=True,
    text=True
)

# Get output directly
output = subprocess.check_output(["ls", "-la"], text=True)

logging - Logging

import logging

# Basic setup
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)

logger = logging.getLogger(__name__)

# Log levels
logger.debug("Debug info")
logger.info("General info")
logger.warning("Warning")
logger.error("Error occurred")
logger.critical("Critical error")

# Log exception with traceback
try:
    risky_operation()
except Exception:
    logger.exception("Operation failed")

# File handler
handler = logging.FileHandler("app.log")
handler.setLevel(logging.ERROR)
logger.addHandler(handler)

argparse - Command Line Arguments

import argparse

parser = argparse.ArgumentParser(description="Process files")

# Positional argument
parser.add_argument("filename", help="Input file")

# Optional arguments
parser.add_argument("-o", "--output", help="Output file")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-n", "--count", type=int, default=1)
parser.add_argument("--format", choices=["json", "csv"])

args = parser.parse_args()

print(args.filename)
print(args.verbose)

urllib - URL Handling

from urllib.request import urlopen, Request
from urllib.parse import urlencode, urlparse, parse_qs

# Simple GET request
with urlopen("https://api.example.com/data") as response:
    data = response.read().decode()

# With headers
req = Request(
    "https://api.example.com/data",
    headers={"Authorization": "Bearer token"}
)
with urlopen(req) as response:
    data = response.read()

# Parse URL
parsed = urlparse("https://example.com/path?key=value")
parsed.scheme   # "https"
parsed.netloc   # "example.com"
parsed.path     # "/path"
parsed.query    # "key=value"

# Parse query string
params = parse_qs("key=value&foo=bar")  # {"key": ["value"], "foo": ["bar"]}

# Build query string
query = urlencode({"key": "value", "foo": "bar"})  # "key=value&foo=bar"

hashlib - Hashing

import hashlib

# Hash a string
text = "Hello, World!"
hash_obj = hashlib.sha256(text.encode())
hash_hex = hash_obj.hexdigest()

# Hash a file
def hash_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as f:
        while chunk := f.read(8192):
            h.update(chunk)
    return h.hexdigest()

# Available algorithms
hashlib.algorithms_available

threading - Threads

import threading
import time

def worker(name):
    print(f"{name} starting")
    time.sleep(1)
    print(f"{name} done")

# Create and start threads
threads = []
for i in range(3):
    t = threading.Thread(target=worker, args=(f"Thread-{i}",))
    threads.append(t)
    t.start()

# Wait for completion
for t in threads:
    t.join()

# Thread-safe counter
lock = threading.Lock()
counter = 0

def increment():
    global counter
    with lock:
        counter += 1

Quick Reference

ModuleUse For
datetimeDates and times
osOS interface, env vars
sysSystem parameters, argv
pathlibFile paths
reRegular expressions
jsonJSON encoding/decoding
csvCSV files
collectionsCounter, defaultdict, deque
itertoolsIterator utilities
functoolsFunction utilities, caching
randomRandom numbers
subprocessRun external commands
loggingApplication logging
argparseCLI arguments
urllibURL handling
hashlibHashing
threadingThreads
asyncioAsync I/O
unittestTesting
pdbDebugging