SyntaxStudy
Sign Up
Python Standard Library Overview
Python Beginner 8 min read

Standard Library Overview

Standard Library Overview

Python's "batteries included" standard library covers an enormous range of tasks — no pip install needed.

Data & Math

import math, statistics, decimal, fractions, random
print(math.factorial(10))          # 3628800
print(statistics.mean([1,2,3,4]))  # 2.5
print(random.choice(["a","b","c"]))

Date & Time

from datetime import datetime, timedelta, date
import time
now = datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now + timedelta(days=7))

OS & Files

import os, shutil, glob, tempfile
print(os.getcwd())
print(os.environ.get("HOME", "unknown"))
shutil.copy("src.txt", "dst.txt")

Text & Data

import re, json, csv, textwrap, string
print(re.findall(r"\d+", "abc 123 def 456"))  # ['123', '456']
print(textwrap.wrap("A very long line...", width=30))

Networking & Web

import urllib.request, http.server, socket
from urllib.parse import urlparse, urlencode
result = urlparse("https://example.com/path?q=1")
print(result.netloc)  # example.com

Concurrency

import threading, multiprocessing, asyncio, concurrent.futures
Example
import os, sys, platform, datetime

print("Python:", sys.version)
print("Platform:", platform.system())
print("CWD:", os.getcwd())
print("Date:", datetime.date.today())
print("CPU cores:", os.cpu_count())

import math
print("Primes via trial:", [n for n in range(2, 30) if all(n%i != 0 for i in range(2, int(math.sqrt(n))+1))])
Pro Tip

Run python -m module_name to use many stdlib modules as command-line tools: python -m json.tool, python -m http.server, python -m timeit.