Chapter 2 · Practice 1 of 5
Follow PEP 8 and let a formatter apply it
Why it matters
PEP 8 is the shared style guide for Python. The parts that matter most day to day:
| Thing | Convention |
|---|---|
| Indentation | 4 spaces, no tabs |
| Functions, variables, modules | snake_case |
| Classes | CapWords |
| Constants | UPPER_CASE at module level |
| Line length | 79 by default; a team may agree on up to 99 |
PEP 8 itself says consistency within a project matters more than the guide, and consistency within a module matters most.
Do not enforce any of this in code review. Run a formatter on save and in CI, so style never appears in a review comment again. Ruff formats with Black-compatible output and lints in the same tool.
Example
# pyproject.toml
[tool.ruff]
line-length = 99
target-version = "py314"
[tool.ruff.lint]
select = ["E", "F", "I", "B", "UP", "SIM"]