Appearance
3.13.16 — Pythonic Idioms & the Standard Library
Two engineers write the same webhook handler for payment events. The first writes sixty lines: nested if checks on dictionary keys, a manual loop to count failures, a hand-rolled "top five orders" sort, string concatenation for log lines, and a timestamp with no time zone. The second writes twenty lines that do the same thing, using a match statement, a Counter, heapq.nlargest, a logger and a time-zone-aware datetime. The second version is not just shorter. It has fewer places for bugs to hide, and every reader can see what it does at a glance. This page collects the features and standard-library tools that make that difference. None of them are exotic. They are the tools a senior Python engineer reaches for every week.
1. Structural pattern matching: match
Python 3.10 added the match statement (PEPs 634 to 636). It looks like a switch from other languages, but it does much more: it checks the shape of a value and pulls out its parts in one step. That makes it a natural fit for handling events, messages and parsed JSON.
Here is a payment webhook handler. Each case describes a shape of event.
python
from dataclasses import dataclass
@dataclass
class Card:
last4: str
network: str
@dataclass
class Upi:
vpa: str
def describe(event):
match event:
case {"type": "order.paid", "amount": int(amount)} if amount >= 100_000: # (1)
return f"large payment of {amount}"
case {"type": "order.paid", "method": Card(network="visa" | "mastercard" as network, last4=last4)}: # (2)
return f"paid by {network} card ending {last4}"
case {"type": "order.paid", "method": Upi(vpa=vpa)}: # (3)
return f"paid by UPI {vpa}"
case {"type": "order.refunded", "lines": [first, *rest]}: # (4)
return f"refund of {first} and {len(rest)} more"
case {"type": "order.refunded", "lines": []}:
return "empty refund"
case {"type": str(kind)}: # (5)
return f"ignored {kind}"
case _: # (6)
return "not an event"
print(describe({"type": "order.paid", "amount": 250_000}))
print(describe({"type": "order.paid", "amount": 500, "method": Card("4242", "visa")}))
print(describe({"type": "order.paid", "amount": 500, "method": Upi("asha@okbank")}))
print(describe({"type": "order.refunded", "lines": ["mug", "pen", "lamp"]}))
print(describe({"type": "cart.updated"}))
print(describe(["not", "a", "dict"]))text
large payment of 250000
paid by visa card ending 4242
paid by UPI asha@okbank
refund of mug and 2 more
ignored cart.updated
not an eventEach line uses a different kind of pattern.
(1)A mapping pattern matches a dict that has these keys, ignoring any extra keys.int(amount)checks the value is anintand binds it toamount. Theifafter the pattern is a guard: an extra condition checked only after the shape matches.(2)A class pattern,Card(...), checksisinstance(value, Card)and then matches its attributes."visa" | "mastercard"is an or-pattern.as networkbinds whichever one matched.(3)Another class pattern, pulling out one attribute.(4)A sequence pattern matches a list with at least one item, binding the first and collecting the rest, just like unpacking in 3.13.3.(5)A catch-all for any other event type, as long astypeis a string.(6)_is the wildcard. It matches anything and binds nothing.
How do class patterns know which attribute is which when you write them by position, as in Card("4242", "visa")? Through __match_args__, which @dataclass generates for you (3.13.8).
The trap: a bare name captures
A bare name in a pattern does not compare against a variable. It captures the value into that name, and matches anything.
python
OK = 200
def status(code):
match code:
case OK: # (1)
return "ok"
print(status(404))text
ok(1)This does not mean "equal toOK". It means "match anything and call itOK". A 404 is reported as ok.
To compare against a constant, the name must contain a dot, such as HTTPStatus.OK or Status.PAID. Python reads a dotted name as a value pattern and compares with ==. That is one more reason to keep constants in an Enum.
Use match when you branch on the structure of data. For a simple choice between a few values, an if/elif chain or a dict of handlers is still perfectly clear.
2. f-strings, and the new t-strings
f-strings
f-strings, added in Python 3.6, are the standard way to build strings. Inside {} you can put any expression, followed by an optional format spec after a colon.
python
from datetime import datetime, timezone
price, sku, qty = 1234.5, "mug", 3
print(f"{sku!r:>8} x{qty:03d} = {price:,.2f}") # (1)
print(f"{qty=}, {price * qty = :.1f}") # (2)
print(f"{datetime(2026, 10, 21, 9, 5, tzinfo=timezone.utc):%Y-%m-%d %H:%M}") # (3)text
'mug' x003 = 1,234.50
qty=3, price * qty = 3703.5
2026-10-21 09:05(1)!rusesrepr().>8right-aligns in 8 characters.03dpads an integer with zeros.,.2fadds thousands separators and two decimals.(2)A trailing=prints the expression and its value, which is perfect for quick debugging.(3)Objects can define their own format spec.datetimeacceptsstrftimecodes.
Since Python 3.12 (PEP 701), you may reuse the same kind of quote inside the braces, as in f"{items["mug"]}", and put comments and line breaks inside long expressions.
t-strings: templates that are not strings yet
An f-string becomes a plain string immediately, so any value inside it is already mixed into the text. That is exactly how injection bugs happen: a customer's input lands inside an SQL query or an HTML page unescaped. Python 3.14 added template strings, or t-strings (PEP 750). A t"..." literal does not produce a string. It produces a Template object that keeps the fixed text and the inserted values separate, so code can process the values safely before joining them.
python
from html import escape
from string.templatelib import Template, Interpolation
name = "<script>alert(1)</script>"
greeting = t"<p>Hello {name}</p>" # (1)
print(type(greeting).__name__, greeting.strings)
def render_html(template: Template) -> str:
parts = []
for item in template: # (2)
if isinstance(item, Interpolation):
parts.append(escape(str(item.value))) # (3)
else:
parts.append(item)
return "".join(parts)
print(render_html(greeting))text
Template ('<p>Hello ', '</p>')
<p>Hello <script>alert(1)</script></p>(1)ATemplate, not a string. The fixed parts and the value are stored apart.(2)Iterating gives fixed strings andInterpolationobjects in order.(3)Every inserted value is escaped. The attacker's script tag becomes harmless text.
The same idea makes database queries safe. A function can turn each interpolation into a ? placeholder and send the values separately, which is how parameterised queries prevent SQL injection (Part 8).
python
from string.templatelib import Interpolation
def sql(template):
query, params = [], []
for item in template:
if isinstance(item, Interpolation):
query.append("?")
params.append(item.value)
else:
query.append(item)
return "".join(query), params
email = "x' OR '1'='1"
print(sql(t"SELECT * FROM customers WHERE email = {email}"))text
('SELECT * FROM customers WHERE email = ?', ["x' OR '1'='1"])The attack string never touches the SQL text. Expect database and templating libraries to accept t-strings directly over the next few years.
3. collections: better containers
Counter
python
from collections import Counter
sold = ["mug", "pen", "mug", "lamp", "mug", "pen"]
counts = Counter(sold) # (1)
print(counts)
print(counts.most_common(2)) # (2)
print(counts["sofa"]) # (3)text
Counter({'mug': 3, 'pen': 2, 'lamp': 1})
[('mug', 3), ('pen', 2)]
0(1)Counts every item in one pass.(2)The top items, sorted by count.(3)A missing key gives 0 instead of aKeyError.
defaultdict
python
from collections import defaultdict
orders_by_customer = defaultdict(list) # (1)
for customer_id, order_id in [("c1", "ORD-1"), ("c2", "ORD-2"), ("c1", "ORD-3")]:
orders_by_customer[customer_id].append(order_id) # (2)
print(dict(orders_by_customer))text
{'c1': ['ORD-1', 'ORD-3'], 'c2': ['ORD-2']}(1)When a missing key is read, the dict callslist()to create a default value and stores it.(2)So grouping needs no "if the key is missing, create an empty list" check.
deque with maxlen, and ChainMap
python
from collections import deque, ChainMap
recent = deque(maxlen=3) # (1)
for order_id in ["ORD-1", "ORD-2", "ORD-3", "ORD-4"]:
recent.append(order_id)
print(recent)
defaults = {"currency": "INR", "timeout": 5}
from_env = {"timeout": 2}
config = ChainMap({}, from_env, defaults) # (2)
print(config["timeout"], config["currency"])
print(defaults | from_env) # (3)text
deque(['ORD-2', 'ORD-3', 'ORD-4'], maxlen=3)
2 INR
{'currency': 'INR', 'timeout': 2}(1)A bounded deque drops the oldest item automatically. It is ideal for "last N events".(2)ChainMaplooks up keys in each dict in turn, without copying them. The first dict that has the key wins. This is the classic layering of command-line, environment and default settings.(3)Since 3.9,|merges two dicts into a new one, with the right side winning.
4. Sorting, top-k and sorted lookups
Sorting with keys
sorted() and list.sort() take a key function. Python's sort, called Timsort after Tim Peters who wrote it in 2002, is stable: items with equal keys keep their original order. That lets you sort by several keys at once with a tuple.
python
customers = [("asha", "Pune", 31), ("ravi", "Delhi", 25), ("meera", "Pune", 25)]
print(sorted(customers, key=lambda c: (c[1], -c[2]))) # (1)text
[('ravi', 'Delhi', 25), ('asha', 'Pune', 31), ('meera', 'Pune', 25)](1)Sort by city, then by age from oldest to youngest. Negating a number reverses its order within the tuple.
heapq: top-k and priority queues
To find the five biggest orders among a million, sorting all million wastes work. heapq.nlargest keeps only a small heap of the current best k items, so it costs O(n log k) instead of O(n log n). A heap also makes a simple priority queue, where the smallest item always comes out first. Part 4 explains heaps in depth.
python
import heapq
orders = [("ORD-1", 450), ("ORD-2", 12000), ("ORD-3", 900), ("ORD-4", 7000)]
print(heapq.nlargest(2, orders, key=lambda o: o[1])) # (1)
jobs = []
heapq.heappush(jobs, (2, "send newsletter")) # (2)
heapq.heappush(jobs, (0, "refund ORD-9"))
heapq.heappush(jobs, (1, "ship ORD-4"))
print([heapq.heappop(jobs)[1] for _ in range(3)])text
[('ORD-2', 12000), ('ORD-4', 7000)]
['refund ORD-9', 'ship ORD-4', 'send newsletter'](1)The two biggest orders.(2)Tuples compare by their first item, so the priority number decides the order.
bisect: lookups in sorted lists
bisect uses binary search to find where a value belongs in a sorted list, in O(log n). A classic use is turning a number into a band.
python
import bisect
thresholds = [500, 2000, 10000]
tiers = ["free", "standard", "priority", "white-glove"]
for total in (120, 500, 4500, 25000):
print(total, tiers[bisect.bisect_right(thresholds, total)]) # (1)text
120 free
500 standard
4500 priority
25000 white-glove(1)bisect_rightreturns how many thresholds are less than or equal to the total, which is exactly the index of its shipping tier.
5. Small built-ins that replace whole loops
enumerate(items, start=1)gives position and item together. Never writefor i in range(len(items)).zip(a, b, strict=True)walks two sequences together and fails loudly if their lengths differ (3.13.9).any(...)andall(...)stop at the first item that decides the answer.min(values, default=0)andmax(values, default=0)avoid crashing on an empty list.sum(order.total for order in orders)adds up with a generator, building no list."ORD-123".removeprefix("ORD-")and.removesuffix(), added in 3.9, remove an exact prefix or suffix. Do not confuse them with.strip("ORD-"), which removes any of those characters from both ends.
6. Files and paths: pathlib
pathlib, added in Python 3.4, treats a file path as an object instead of a string. It uses the / operator to join parts, and it works on Windows and Linux alike.
python
from pathlib import Path
report = Path("exports") / "2026" / "orders.csv" # (1)
print(report.name, report.stem, report.suffix, report.parent)
report.parent.mkdir(parents=True, exist_ok=True) # (2)
report.write_text("ORD-1,paid\n", encoding="utf-8") # (3)
print(report.read_text(encoding="utf-8").strip())
print([p.name for p in Path("exports").rglob("*.csv")]) # (4)text
orders.csv orders .csv exports/2026
ORD-1,paid
['orders.csv'](1)/joins path parts with the right separator for the operating system.(2)Create the folder and any missing parents. No error if it already exists.(3)Always passencoding="utf-8". Otherwise Python uses the system's default encoding, which on some Windows machines is not UTF-8, and a customer's name with an accent turns into garbage.(4)rglobsearches all subfolders.
7. Dates and times: always time-zone aware
Python's datetime objects come in two kinds.
- A naive datetime has no time zone. "9:00 on 21 October" means nothing on a server that serves customers in several countries.
- An aware datetime carries its time zone, so it identifies one exact instant.
The rule for services is simple: store and compute in UTC, and convert to a local zone only for display. The zoneinfo module, added in 3.9, provides real time zones with their daylight-saving rules.
python
from datetime import datetime, timezone, timedelta
from zoneinfo import ZoneInfo
placed = datetime(2026, 10, 21, 9, 0, tzinfo=ZoneInfo("Asia/Kolkata")) # (1)
print(placed.isoformat())
print(placed.astimezone(timezone.utc).isoformat()) # (2)
naive = datetime(2026, 10, 21, 9, 0)
try:
placed - naive # (3)
except TypeError as error:
print("TypeError:", error)
print(datetime.now(timezone.utc).tzinfo) # (4)text
2026-10-21T09:00:00+05:30
2026-10-21T03:30:00+00:00
TypeError: can't subtract offset-naive and offset-aware datetimes
UTC(1)An aware time in India.(2)The same instant in UTC.(3)Python refuses to mix the two kinds, which protects you from an off-by-hours bug.(4)Get the current time withdatetime.now(timezone.utc). The olderdatetime.utcnow()returns a naive value and has been deprecated since 3.12.
On Windows, zoneinfo needs the tzdata package from PyPI, because Windows does not ship the standard time-zone database. Without it, ZoneInfo("Asia/Kolkata") raises ZoneInfoNotFoundError. Add tzdata to your dependencies so the code works everywhere.
The daylight-saving trap
Arithmetic on an aware datetime works on the wall clock, not on real elapsed time. Across a daylight-saving change, that gives a wrong answer.
python
from datetime import datetime, timezone, timedelta
from zoneinfo import ZoneInfo
new_york = ZoneInfo("America/New_York")
start = datetime(2026, 3, 8, 1, 30, tzinfo=new_york) # (1)
print((start + timedelta(hours=1)).isoformat()) # (2)
print((start.astimezone(timezone.utc) + timedelta(hours=1)).astimezone(new_york).isoformat()) # (3)text
2026-03-08T02:30:00-05:00
2026-03-08T03:30:00-04:00(1)Half an hour before clocks in New York jump from 2:00 to 3:00.(2)Adding one hour on the wall clock gives 2:30, a time that does not exist that night.(3)Doing the maths in UTC and converting back gives the correct 3:30.
This is the practical reason behind the UTC rule.
8. JSON with real-world types
The json module only knows dicts, lists, strings, numbers, booleans and None. Real payloads contain Decimal amounts and dates.
python
import json
from datetime import date
from decimal import Decimal
payload = {"id": "ORD-1", "total": Decimal("1999.50"), "placed": date(2026, 10, 21)}
def to_json(value): # (1)
if isinstance(value, Decimal):
return str(value)
if isinstance(value, date):
return value.isoformat()
raise TypeError(f"cannot serialise {type(value).__name__}")
print(json.dumps(payload, default=to_json))
print(json.loads('{"total": 1999.50}', parse_float=Decimal)) # (2)text
{"id": "ORD-1", "total": "1999.50", "placed": "2026-10-21"}
{'total': Decimal('1999.50')}(1)defaultis called for any valuejsondoes not understand. RaiseTypeErrorfor anything unexpected, so mistakes are not hidden.(2)parse_float=Decimalreads numbers with decimals as exactDecimalvalues instead of floats, so money is never rounded on the way in.
In a FastAPI service, Pydantic handles both directions for you (3.14.2), but these two hooks are what it is doing underneath.
9. Logging, not printing
print writes to the screen with no level, no time and no source. The logging module adds all three, and lets operators turn detail up or down without changing code.
python
import logging
import sys
logging.basicConfig(level=logging.INFO, stream=sys.stdout,
format="%(levelname)s %(name)s: %(message)s") # (1)
logger = logging.getLogger("shop.checkout") # (2)
logger.debug("cart contents: %s", ["mug"]) # (3)
logger.info("charged %s for order %s", 499, "ORD-7") # (4)
try:
1 / 0
except ZeroDivisionError:
logger.exception("tax calculation failed for %s", "ORD-7") # (5)text
INFO shop.checkout: charged 499 for order ORD-7
ERROR shop.checkout: tax calculation failed for ORD-7
Traceback (most recent call last):
File "checkout.py", line 11, in <module>
1 / 0
ZeroDivisionError: division by zero(1)Configure logging once, at the program's entry point. Libraries must never callbasicConfig.(2)Name loggers after modules, usually withlogging.getLogger(__name__). The dotted names form a tree, soshopsettings apply toshop.checkouttoo.(3)Below the configured level, so it is skipped.(4)Pass values as arguments, not with an f-string. Formatting then happens only if the message is actually emitted, and log tools can group messages by their fixed template.(5)logger.exceptionlogs at ERROR level and adds the full traceback, including any chained causes (3.13.10).
Production services usually log structured JSON, one object per line, so a log system can search by field. 3.14.9 sets that up.
10. Small modules worth knowing
secretsmakes values that must be unguessable:secrets.token_urlsafe(32)for password-reset links and API keys. Never userandomfor these, because its output can be predicted after observing enough values.uuid.uuid4()makes random unique IDs, such as'3f1c…', for orders and events. Python 3.14 also addeduuid.uuid7(), which starts with a timestamp, so IDs sort by creation time and database indexes stay compact.functools.cache,partialandsingledispatchwere covered in 3.13.5.itertoolswas covered in 3.13.9.dataclassesandenumwere covered in 3.13.8.
What the interviewer will push on
"What does case OK: do in a match statement?" It captures any value into a new name called OK, so it always matches. Constants need a dotted name, such as Status.OK, to be compared. The tell is knowing why: bare names are capture patterns by design.
"How do you find the top 10 items from a stream of millions?" heapq.nlargest(10, stream, key=...), which keeps a heap of size 10 and costs O(n log k). Sorting everything is O(n log n) and needs all items in memory.
"How should a service handle time zones?" Use aware datetimes, store and compute in UTC, convert only for display with zoneinfo, and never use utcnow(). Bonus: show the daylight-saving gap bug and the fix of doing arithmetic in UTC.
"Why use logger.info("x %s", value) instead of an f-string?" Formatting is skipped when the level is disabled, and the constant template lets log tools group identical messages. Mention logger.exception for tracebacks, and getLogger(__name__).
"What are t-strings?" Python 3.14 template strings, which produce a Template with the fixed text and the values kept apart, so libraries can escape or parameterise values safely before building the final string. The point is preventing injection.
Something to volunteer that nobody asks for: str.strip("ORD-") removes any of the characters O, R, D and - from both ends, not the prefix, so "ORD-DROID".strip("ORD-") returns "I". removeprefix is the correct tool. It is a small detail that catches real bugs in review.
Recall
matchchecks shape and extracts parts: mapping, class (via__match_args__), sequence with*rest, or-patterns withas, guards withif, and_as the wildcard. A bare name captures. Compare constants with dotted names.- f-strings:
!r, alignment,,.2f,{expr=}, nested quotes since 3.12. Template strings (t"...", 3.14) keep text and values apart for safe HTML and SQL. Counter,defaultdict,deque(maxlen=n),ChainMap, and|for dict merge.- Timsort is stable, so sort by tuple keys.
heapq.nlargestfor top-k,heappush/heappopfor a priority queue.bisectfor bands in sorted lists. enumerate,zip(strict=True),any/all,min/max(default=),removeprefix.pathlib.Pathwith/, and always passencoding="utf-8".- Use aware datetimes, never naive datetimes, in services. Compute in UTC and display with
zoneinfo(installtzdataon Windows). Do arithmetic in UTC across daylight-saving changes. json.dumps(default=...),json.loads(parse_float=Decimal).logging.getLogger(__name__), lazy%sarguments,logger.exception. Configure once, at the entry point.secretsfor tokens,uuid4/uuid7for IDs.
Self-test: Why does case OK: match a 404? What does a t-string give you that an f-string cannot? Why is heapq.nlargest faster than sorting for top-k? Why does adding one hour to 1:30 a.m. in New York on 8 March give a time that does not exist?
Quiz Bank
FoundationalWhen is a match statement better than if/elif?
When you branch on the structure of data and need to pull pieces out of it at the same time, such as JSON events, messages with optional fields, or several dataclass types. A single case both checks the shape, for example "a dict with type 'order.paid' and a Card method", and binds the parts you need. With if/elif, that becomes nested key checks, isinstance calls and separate lookups. For a simple comparison against a few values, if/elif or a dict of handlers is just as clear.
AppliedA report shows orders placed 'yesterday' but includes some from today for customers in India. What is the likely bug and fix?
The code probably computes "yesterday" with naive datetimes, or in the server's time zone, usually UTC, while the business means India time, which is 5 hours 30 minutes ahead. Orders placed between midnight and 5:30 a.m. India time fall on the previous UTC date. Fix it by storing timestamps as aware UTC values, computing the day boundaries in the business time zone with zoneinfo, for example midnight to midnight in Asia/Kolkata, converting those boundaries to UTC, and querying with them. Make the reporting time zone an explicit setting.
InterviewCount the five most common product codes in a 10 GB log file without loading it into memory.
Iterate over the file line by line, which reads from a buffer and keeps memory small. Extract the product code from each line, and feed the codes into a collections.Counter with counter.update(codes_from_chunk) or counter[code] += 1. Memory grows with the number of distinct codes, not with the file size. Then counter.most_common(5) returns the answer, and it uses a heap internally when asked for a few items. If the number of distinct codes is too large for memory, split the work by hashing each code into one of several partition files, count each partition separately, and merge the top results. Part 4 covers approximate structures for even bigger data.
StaffWhat standards would you set for logging across a team's Python services?
Configure logging in exactly one place per service, at start-up. Libraries only call getLogger(__name__). Emit structured JSON lines with consistent fields: timestamp in UTC, level, logger name, message template, request or correlation ID from a context variable, and the service version. Use lazy %s arguments so templates stay constant and disabled levels cost nothing. Use logger.exception inside handlers so tracebacks and chained causes are kept. Ban print in service code through a lint rule. Never log secrets or full payment details, and redact them in a central filter. Set levels by environment variable, so operators can turn on debug for one module without a deploy. Log an error once, at the boundary that handles it, not at every layer.
Next: the Python chapter is complete. You can now read any Python codebase and explain what happens underneath each line. Chapter 3.14 puts it all to work: 3.14.1 explains how a Python web server talks to your code, from the old WSGI standard to the ASGI standard that FastAPI is built on.