What is PEP 8?
Learn what PEP 8 is, its key formatting and naming conventions, why it matters for readable Python code, and which tools enforce it automatically.
Expected Interview Answer
PEP 8 is Python's official style guide, published as Python Enhancement Proposal 8, that defines conventions for formatting and naming code — things like indentation, line length, whitespace, and identifier casing — so that code written by different people looks and reads consistently.
PEP 8 covers layout rules like using 4 spaces per indentation level, keeping lines under about 79-99 characters, and using blank lines to separate logical sections, as well as naming conventions like snake_case for functions and variables, PascalCase for classes, and UPPER_CASE for constants. It also gives guidance on whitespace around operators, import ordering, and when to use comments versus docstrings. PEP 8 is a guideline, not a hard language rule, but most teams enforce it automatically with linters and formatters like flake8, ruff, black, or autopep8 as part of CI or pre-commit hooks, so style debates are resolved by tooling rather than review comments. Following it makes code easier to read across a team and easier to onboard new contributors into.
- Consistent formatting makes code readable across a whole team
- Automatable via linters/formatters (ruff, black, flake8) in CI
- Reduces bikeshedding in code review over style choices
- Establishes clear naming conventions (snake_case, PascalCase, UPPER_CASE)
- Widely adopted, so it is a shared baseline across the ecosystem
AI Mentor Explanation
PEP 8 is like the laws of cricket governing field markings and kit — every ground uses the same pitch dimensions and crease markings so a player from any club instantly recognizes the layout. It does not dictate how you play your shots, only the shared conventions that let any two teams play on the same ground without confusion.
Step-by-Step Explanation
Step 1
What it is
PEP 8 is Python's official style guide for formatting and naming conventions, authored as an enhancement proposal.
Step 2
Layout rules
4-space indentation, a reasonable line-length limit, and blank lines separating logical sections.
Step 3
Naming conventions
snake_case for functions/variables, PascalCase for classes, UPPER_CASE for constants.
Step 4
Guideline, not enforced by the interpreter
Python runs code regardless of style; PEP 8 is a convention, not a syntax requirement.
Step 5
Enforced by tooling
Linters/formatters like ruff, flake8, and black check or auto-fix PEP 8 compliance in CI and pre-commit hooks.
What Interviewer Expects
- Knows PEP 8 is Python's style guide, not a language rule
- Can name at least a few concrete conventions (indentation, naming, line length)
- Mentions tooling (black, ruff, flake8) used to enforce it automatically
- Understands the goal is team-wide readability and consistency
- Distinguishes PEP 8 from other PEPs like PEP 20 (Zen of Python) or PEP 484 (type hints)
Common Mistakes
- Claiming PEP 8 is enforced by the Python interpreter itself
- Confusing PEP 8 with PEP 20 (the Zen of Python)
- Thinking PEP 8 dictates architecture or design patterns rather than style
- Not knowing automated formatters exist and manually formatting to match it
Best Answer (HR Friendly)
“PEP 8 is Python's official style guide — a set of agreed-upon rules for formatting and naming code so that any Python developer's code looks familiar to any other. Most teams use automated tools to apply it, which keeps code reviews focused on logic instead of formatting arguments.”
Code Example
# Not PEP 8 compliant
def CalcTotal(a,b):
return a+b
# PEP 8 compliant
def calc_total(a, b):
"""Return the sum of two numbers."""
return a + b
MAX_RETRIES = 3 # UPPER_CASE constant
class OrderProcessor: # PascalCase class name
def process_order(self, order_id): # snake_case method/args
passFollow-up Questions
- What tools automatically enforce PEP 8 in a CI pipeline?
- What is the difference between a linter and a formatter?
- What does PEP 20 (the Zen of Python) say, and how does it differ from PEP 8?
- What is the recommended maximum line length in PEP 8?
- How would you configure black or ruff for a new Python project?
MCQ Practice
1. PEP 8 primarily governs which aspect of Python code?
PEP 8 is the official style guide covering formatting, layout, and naming conventions for readable code.
2. What naming convention does PEP 8 recommend for function names?
PEP 8 recommends snake_case (lowercase words separated by underscores) for function and variable names.
3. Does the Python interpreter enforce PEP 8 rules at runtime?
PEP 8 is a style guideline; Python runs non-compliant code fine. Enforcement comes from external linters/formatters.
Flash Cards
What is PEP 8? — Python's official style guide for code formatting and naming conventions.
Naming convention for classes under PEP 8? — PascalCase, e.g. OrderProcessor.
Does Python enforce PEP 8 at runtime? — No — it's a convention, typically enforced by external tools like black or ruff.
Name two tools that enforce PEP 8 automatically. — black (formatter) and ruff or flake8 (linters).