Skip to content

PEP 8 & Pythonic Style

Code is read much more often than it is written. PEP 8 (Python Enhancement Proposal #8) is the definitive style guide for Python. Adhering to PEP 8 is not just about aesthetics; it’s about making your code accessible to the millions of other Python developers worldwide.


  • Use 4 Spaces: Never use Tabs.
  • Maximum Line Length: 79 characters for code, 72 for docstrings.
  • Blank Lines:
    • Two blank lines between top-level functions and classes.
    • One blank line between methods inside a class.
  • Always put imports at the top of the file.
  • Group them in this order:
    1. Standard library (e.g., os, sys).
    2. Related third-party imports (e.g., django, requests).
    3. Local application/library specific imports.

TypeConventionExample
Variablesnake_caseuser_id
Functionsnake_casecalculate_total()
ClassPascalCaseUserAccount
ConstantUPPER_CASEMAX_RETRIES
Package/Modulelowercaseutils

Beyond formatting, Python has a philosophy. You can read it by running import this in your terminal.

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • Simple is better than complex.
  • Readability counts.

In a professional setting, you don’t manually check for PEP 8. You use automation.

Ruff is an extremely fast Python linter that checks for hundreds of style and logic errors.

Terminal window
pip install ruff
ruff check .

Black is an “uncompromising” code formatter. It takes your code and completely rewrites it to follow PEP 8 perfectly.

Terminal window
pip install black
black my_script.py

FeatureGuideline
Indentation4 Spaces (No Tabs).
Namingsnake_case for everything except Classes.
QuotesUse single or double consistently (Black prefers double).
WhitespaceAvoid extra spaces inside parentheses or before commas.