Essential Standard Library Modules
Checking access...
Python’s standard library is extensive. Here are the most essential modules.
os — Operating System Interface
import os
# Current directoryos.getcwd() # Current working directoryos.chdir("/tmp") # Change directory
# File operationsos.listdir(".") # List directory contentsos.mkdir("new_dir") # Create directoryos.makedirs("a/b/c", exist_ok=True) # Create nested directoriesos.remove("file.txt")os.rename("old.txt", "new.txt")
# Environment variablesos.environ.get("HOME", "/default/path")os.environ["MY_VAR"] = "value"
# Path infoos.path.join("dir", "file.txt") # Platform-correct pathos.path.exists("file.txt")os.path.isfile("file.txt")os.path.isdir("mydir")sys — System-Specific Parameters
import sys
# Command-line arguments# sys.argv = ["script.py", "--verbose", "input.txt"]script_name = sys.argv[0]args = sys.argv[1:]
# Exit programsys.exit(0) # Successsys.exit(1) # Error
# Python versionprint(sys.version) # 3.12.0 ...print(sys.version_info) # sys.version_info(major=3, minor=12, ...)
# Module search pathprint(sys.path) # Directories Python searches for imports
# Standard streamssys.stdout.write("Hello\n")sys.stderr.write("Error!\n")data = sys.stdin.read()datetime — Date and Time
from datetime import datetime, date, time, timedelta, timezone
# Current timenow = datetime.now()today = date.today()
# Create specific datesd = date(2024, 12, 25)t = time(14, 30, 0)dt = datetime(2024, 12, 25, 14, 30)
# Formattingnow.strftime("%Y-%m-%d %H:%M:%S") # '2024-12-25 14:30:00'now.strftime("%A, %B %d") # 'Wednesday, December 25'
# Parsingparsed = datetime.strptime("2024-01-15", "%Y-%m-%d")
# Arithmetictomorrow = today + timedelta(days=1)next_week = today + timedelta(weeks=1)age = today - date(1990, 5, 15)
# Timezone awarefrom zoneinfo import ZoneInfo # Python 3.9+dt_utc = datetime.now(ZoneInfo("UTC"))dt_ny = dt_utc.astimezone(ZoneInfo("America/New_York"))json — JSON Encoding/Decoding
import json
data = {"name": "Alice", "scores": [85, 92, 78], "active": True}
# String conversionjson_str = json.dumps(data, indent=2)parsed = json.loads(json_str)
# File operationswith open("data.json", "w") as f: json.dump(data, f, indent=2)
with open("data.json") as f: data = json.load(f)
# Custom serializationfrom datetime import datetime
def json_serializer(obj): if isinstance(obj, datetime): return obj.isoformat() raise TypeError(f"Type {type(obj)} not serializable")
data = {"time": datetime.now()}json.dumps(data, default=json_serializer)re — Regular Expressions
import re
# Basic matchingpattern = r"\d+" # One or more digitstext = "Order 123: $45.99"
match = re.search(pattern, text)if match: print(match.group()) # '123'
# All matchesre.findall(r"\d+", text) # ['123', '45', '99']re.findall(r"\d+\.\d+", text) # ['45.99']
# Substitutionre.sub(r"\d+", "NUM", text) # 'Order NUM: $NUM.NUM'
# Splittingre.split(r"[,;\s]+", "a,b;c d") # ['a', 'b', 'c', 'd']
# Named groupspattern = r"(?P<area>\d{3})-(?P<num>\d{4})"m = re.search(pattern, "Call 555-1234")print(m.group("area")) # '555'
# Compile for performancephone_pattern = re.compile(r"\d{3}-\d{4}")phone_pattern.findall("555-1234, 555-5678")
# Common patternsEMAIL = r"[\w.+-]+@[\w-]+\.[\w.]+"URL = r"https?://[\w./-]+"PHONE = r"\d{3}[-.]?\d{3}[-.]?\d{4}"Tip
Raw strings (r"...") prevent Python from interpreting backslashes, making regex patterns much cleaner.
collections — Specialized Containers
from collections import Counter, defaultdict, namedtuple, deque, OrderedDict
# Counter — count occurrencescolors = ["red", "blue", "red", "green", "blue", "blue"]counts = Counter(colors)print(counts) # Counter({'blue': 3, 'red': 2, 'green': 1})print(counts.most_common(2)) # [('blue', 3), ('red', 2)]
# defaultdict — default values for missing keysdd = defaultdict(list)dd["a"].append(1) # No KeyError — auto-creates list
# namedtuple — lightweight objectsPoint = namedtuple("Point", ["x", "y"])p = Point(3, 4)print(p.x, p.y) # 3 4
# deque — double-ended queuedq = deque([1, 2, 3])dq.appendleft(0) # deque([0, 1, 2, 3])dq.append(4) # deque([0, 1, 2, 3, 4])dq.popleft() # 0itertools — Iterator Tools
from itertools import chain, product, permutations, combinations, groupby, cycle
# chain — combine iterableslist(chain([1, 2], [3, 4])) # [1, 2, 3, 4]
# product — Cartesian productlist(product([1, 2], ["a", "b"]))# [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
# permutationslist(permutations([1, 2, 3], 2))# [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
# combinationslist(combinations([1, 2, 3], 2))# [(1, 2), (1, 3), (2, 3)]
# groupby — group sorted datadata = [("a", 1), ("a", 2), ("b", 3)]for key, group in groupby(data, lambda x: x[0]): print(key, list(group))
# cycle — infinite loopcolors = cycle(["red", "green", "blue"])next(colors) # 'red'next(colors) # 'green'math, random, statistics
import mathimport randomimport statistics
# mathmath.pi # 3.14159...math.sqrt(16) # 4.0math.ceil(3.2) # 4math.floor(3.8) # 3
# randomrandom.randint(1, 10) # Random integer 1-10random.choice(["a", "b", "c"]) # Random elementrandom.shuffle(items) # Shuffle in placerandom.sample(population, 3) # 3 unique elements
# statisticsdata = [2, 4, 4, 4, 5, 5, 7, 9]statistics.mean(data) # 5.0statistics.median(data) # 4.5statistics.mode(data) # 4statistics.stdev(data) # Standard deviationhashlib and secrets
import hashlibimport secrets
# Hashingtext = "hello".encode()hashlib.md5(text).hexdigest()hashlib.sha256(text).hexdigest()
# Secure random tokenssecrets.token_hex(16) # 32-char hex stringsecrets.token_urlsafe(16) # URL-safe base64secrets.choice("abcd") # Cryptographically secure choiceKey Takeaways
osandsysfor system interaction; usepathlib.Pathinstead ofos.pathdatetimewithZoneInfofor timezone-aware datetimejsonfor data serialization;refor pattern matchingcollections:Counter,defaultdict,namedtuple,dequeitertools:chain,product,permutations,combinations,groupbymath,random,statisticsfor numerical operationshashlibfor hashing,secretsfor cryptographic randomness