Python PEP 8 guidelines are the official Python style guide for writing clean, readable, and consistent code. Below is a comprehensive list of guidelines organized by category.
1. Code Layout & Indentation
This section explains how Python code should be structured visually to improve readability and maintain consistency across projects.- Indentation: Use 4 spaces per indentation level. Avoid tabs.
- Continuation lines: Use 4 extra spaces for continued lines. Prefer implicit line continuation inside parentheses, brackets, or braces.
- Maximum line length: ≤79 characters for code, ≤72 characters for comments/docstrings.
- Blank lines:
- 2 blank lines before top-level functions or class definitions.
- 1 blank line inside class methods or between methods.
- No trailing whitespace at the end of lines.
2. Imports
These guidelines define how imports should be written and organized to keep dependencies clear and maintainable.
- One import per line. Example:
import os import sys - Group imports: standard library, third-party, local modules.
- Prefer absolute imports over relative imports.
- Avoid wildcard imports (
from module import *).
3. Naming Conventions
Consistent naming helps make Python code self-explanatory and easier to understand for other developers.
- Variables, functions, methods:
lowercase_with_underscores - Constants:
UPPERCASE_WITH_UNDERSCORES - Classes:
CapWords(PascalCase) - Modules/packages:
short_lowercase_names - Private methods/variables:
_single_leading_underscore - Magic methods:
__double_underscores__(e.g.,__init__) - Avoid ambiguous names like
l,O,I.
4. Strings & Quotes
This section outlines recommended practices for using strings and quotation marks consistently in Python.
- Single or double quotes are allowed, but be consistent.
- Triple quotes (
"""or''') for docstrings. - Prefer raw strings (
r"string") for regex or Windows paths.
5. Whitespaces
Proper whitespace usage improves code clarity and prevents unnecessary visual clutter.
- Avoid extra spaces inside parentheses, brackets, or braces.
- No spaces before commas, semicolons, or colons.
- Around assignment operators only when necessary for readability.
6. Comments
Comments and docstrings should clearly explain code intent without stating the obvious.- Block comments start with
#followed by a space. - Inline comments: use sparingly, separated by at least two spaces.
- Docstrings: triple quotes, describe purpose, parameters, return values.
7. Docstring Conventions
Docstrings provide structured documentation for modules, classes, and functions.
- One-line docstrings for simple functions:
"""Return the sum of two numbers.""" - Multi-line docstrings for longer explanations:
""" Compute the sum of two numbers. Parameters: a (int): first number b (int): second number Returns: int: sum of a and b """
8. Programming Recommendations
These general recommendations help avoid common mistakes and improve overall code quality.
- Comparisons to None: use
isoris not. - Boolean comparisons: avoid
== Trueor== False. - Avoid single-letter variable names except for counters or loops.
- Do not use mutable default arguments.
9. Function & Method Guidelines
Well-designed functions and methods make Python programs easier to test and maintain.
- Function definitions: small, single-purpose, with docstrings.
- Argument spacing: no spaces around
=in keyword arguments. - Keep functions small; each should do one thing.
10. Classes
Class-related guidelines ensure object-oriented code remains clean and predictable.
- CapWords naming for classes.
- Use
selffor instance variables,clsfor class methods. - Private attributes prefixed with
_. - Class constants: uppercase at class level.
11. Exception Handling
Proper exception handling helps manage errors gracefully without hiding real issues.
- Use specific exceptions; avoid bare
except:. - Do not use exceptions for flow control unnecessarily.
12. Other Guidelines
These additional rules cover common formatting and readability best practices.
- Binary operators: place at the start of continued lines for readability.
- Boolean operators: same rule for readability (
and,or,not). - Avoid multiple statements on one line.
Summary: Python PEP 8 Guidelines
PEP 8 emphasizes readability, consistency, and maintainability. Key takeaways:- Indent with 4 spaces
- Keep lines ≤79 characters
- Use meaningful names
- Group imports logically
- Document code properly
- Handle exceptions specifically
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.