PEP 8 & Code Linting
PEP 8 is Python's official style guide. Linters (flake8, pylint) and formatters (black, autopep8) enforce consistent, readable code automatically.
10 min•By Priygop Team•Updated 2026
PEP 8 & Linting
PEP 8 & Linting
# PEP 8 key rules:
# 1. Indentation: 4 spaces
def good_function():
if True:
print("4 spaces")
# 2. Line length: max 79 (or 120)
# 3. Blank lines: 2 before functions/classes, 1 between methods
# 4. Imports at the top, grouped:
# stdlib → third-party → local
# 5. Naming conventions:
# variables/functions: snake_case
# classes: PascalCase
# constants: UPPER_SNAKE_CASE
# private: _leading_underscore
MAX_RETRIES = 3 # constant
user_name = "Alice" # variable
class UserAccount: # class
def get_balance(self): # method
_internal = 42 # private
return _internal
# 6. String quotes: consistent (either ' or ")
# 7. Trailing whitespace: none
# 8. Spaces around operators: yes
# Linting tools:
print("=== Linting Tools ===")
tools = {
"flake8": "Style checker (PEP 8)",
"pylint": "Comprehensive linter",
"black": "Opinionated formatter",
"isort": "Import sorter",
"mypy": "Static type checker",
"ruff": "Fast linter (Rust-based)",
}
for tool, desc in tools.items():
print(f" pip install {tool:8s} # {desc}")
# Setup commands:
print("\n=== Usage ===")
print(" flake8 myfile.py # check style")
print(" black myfile.py # auto-format")
print(" mypy myfile.py # check types")
print(" ruff check . # fast lint all")Tip
Tip
Set up Git hooks with pre-commit to run linting and tests before each commit. It catches issues early.
Diagram
Loading diagram…
BEM keeps CSS flat, readable, and avoids specificity wars
Common Mistake
Warning
Skipping code review. Even your own code benefits from a second look. Use pull requests even on solo projects.
Quick Quiz
Practice Task
Note
(1) Install pre-commit hooks. (2) Set up a CI pipeline. (3) Add automated testing to your workflow.