PEP 8 (Python Enhancement Proposal 8) is the style guide for writing Python code. It helps you write code that is clean, readable, and consistent with the wider Python community.
โ PEP 8 โ
๐ 1. Indentation โ
- Use 4 spaces per indentation level.
python
def my_function():
x = 1
if x > 0:
print("Positive")
๐ด Avoid tabs or mixing tabs and spaces.
๐ 2. Maximum Line Length โ
- Keep lines โค 79 characters (for code)
- Keep comments/docstrings โค 72 characters
python
# This is a comment that is short and sweet
For long expressions:
python
# OK - Implicit line continuation inside parentheses
total = (first_variable
+ second_variable
+ third_variable)
๐งพ 3. Blank Lines โ
- Top-level functions/classes: 2 blank lines
- Methods inside a class: 1 blank line
python
class MyClass:
def method_one(self):
pass
def method_two(self):
pass
๐ค 4. Imports โ
- Import standard libraries, third-party libraries, and local imports in separate sections with blank lines in between.
- Always import each module on a separate line
python
# Correct
import os
import sys
# Wrong
import sys, os
Ordering:
python
import os
import sys
import requests
import my_local_module
๐ง 5. Naming Conventions โ
Type | Convention | Example |
---|---|---|
Variable | lower_case_with_underscores | total_count |
Function | lower_case_with_underscores | calculate_sum() |
Class | CapitalizedWords | MyClass |
Constants | ALL_CAPS | MAX_RETRIES |
Modules | short_lowercase | utils.py |
Private | _single_leading_underscore | _internal |
Special | __double_underscores__ | __init__ |
โ 6. Use of Whitespace โ
๐ Correct: โ
python
x = 1
y = 2
z = x + y
๐ Wrong: โ
python
x=1
y =2
z= x+y
More guidelines: โ
No space before a comma, semicolon, colon:
pythonprint(x, y)
Yes space after:
pythonprint(x, y)
No space inside brackets:
pythonmy_list = [1, 2, 3]
๐งพ 7. Comments โ
Inline comments: โ
python
x = x + 1 # Increment x
Block comments: โ
python
# This function adds two numbers
# and returns the result
def add(x, y):
return x + y
Docstrings (triple quotes): โ
python
def add(x, y):
"""Add two numbers and return the result."""
return x + y
โ 8. Comparison to None and Booleans โ
- Use
is
/is not
withNone
, not==
or!=
:
python
if x is None:
...
- Avoid comparisons to
True
orFalse
:
python
# Good
if is_valid:
...
# Bad
if is_valid == True:
...
๐งผ 9. Avoid Unnecessary Expressions โ
python
# Good
if not x:
...
# Bad
if x == False:
...
๐งฐ 10. Function and Class Definitions โ
Leave 2 blank lines before top-level functions and classes.
python
def my_function():
...
class MyClass:
...
๐งน 11. Using linters โ
Tools like flake8
, pylint
, or black
automatically check or format code according to PEP 8.
black
auto-formats your code:
bash
pip install black
๐งฉ 12. Continuation Line Indentation โ
Use 4 spaces for wrapped lines, aligning naturally or with parentheses:
python
# Align with open delimiter
my_function(param_one, param_two,
param_three, param_four)
# Or aligned with first argument
my_function(
param_one, param_two,
param_three
)
๐งฎ 13. Avoid Lambda for Complex Logic โ
python
# OK
squares = map(lambda x: x*x, numbers)
# Better for readability
def square(x): return x*x
squares = map(square, numbers)
โ 14. Don't Use Wildcard Imports โ
python
# Bad
from math import *
# Good
from math import sqrt, pi
โ๏ธ 15. Consistent Exception Handling โ
python
try:
...
except ValueError as e:
print(e)
๐ฆ 16. Avoid Mutable Defaults โ
python
# Bad
def add_item(item, my_list=[]):
my_list.append(item)
return my_list
# Good
def add_item(item, my_list=None):
if my_list is None:
my_list = []
my_list.append(item)
return my_list
๐งโ๐ป Summary โ
Concept | Rule/Best Practice |
---|---|
Indentation | 4 spaces |
Line length | โค 79 characters |
Imports | Grouped: stdlib, 3rd-party, local |
Naming | Use snake_case, PascalCase, UPPER_CASE |
Comments | Use inline & block, write meaningful |
Whitespace | Be consistent, avoid extra spaces |
Docstrings | Use triple quotes for functions/classes |
Tools | Use black , flake8 , isort , etc. |