Ten patterns every Python developer should know

Posted 2026-03-02 on the pythondeck.com blog

These aren't "design patterns" in the Gang-of-Four sense - they're Python idioms that come up week after week. Internalising them will make your code shorter and more readable.

1. Truthiness over explicit comparisons

if items:        # not  if len(items) > 0:
if name is None: # not  if name == None:

2. enumerate, not range(len(...))

for i, item in enumerate(items, start=1):
    print(i, item)

3. zip for parallel iteration

for name, score in zip(names, scores):
    ...

4. dict.get / dict.setdefault

value = cfg.get("timeout", 5)
cfg.setdefault("retries", []).append(t)

5. unpacking, including *rest

first, *middle, last = [1, 2, 3, 4, 5]

6. with for resources

with open(p) as f:
    data = f.read()

7. f-strings with =

print(f"{x=}, {y=}")

8. dataclasses for records

from dataclasses import dataclass
@dataclass
class Point: x: float; y: float

9. pathlib over os.path

from pathlib import Path
for p in Path(".").rglob("*.py"):
    ...

10. EAFP, not LBYL

# preferred
try:
    v = d[k]
except KeyError:
    v = default

« all blog posts Browse tutorials