Pythonic Patterns in Python
From the Python cheat sheet ยท Best Practices ยท verified Jul 2026
Pythonic Patterns
Write idiomatic Python code.
python
# Swap variables
a, b = b, a
# Ternary
x = "yes" if condition else "no"
# Check empty
if not items: # not len(items) == 0
# EAFP over LBYL
try:
value = d[key]
except KeyError:
value = default๐ก EAFP (try/except) is more Pythonic than LBYL (check before acting)
โก Use _ as a throwaway variable for values you don't need
๐ "Explicit is better than implicit" โ readability counts (PEP 20)
๐ข Run "import this" in the REPL to read The Zen of Python
best-practicespythonicpatterns