Master Python Code Style: Essential PEP8 Rules and Auto‑formatting Tips
This article explains what PEP8 is, outlines key Python coding conventions such as indentation, line length, spacing, comments, imports, and naming, and shows how to automatically apply these standards in Jupyter Notebook using the Autopep8 formatter.
1. What is PEP8
PEP stands for Python Enhancement Proposals, documents that convey information about Python, such as new specifications. PEP numbers start at 0 and increase; PEP8 is the style guide that defines conventions for writing Python code.
2. Common Standards
2.1 Indentation
PEP8 recommends using 4 spaces for indentation and avoiding tabs.
# Recommended
if a > b:
print("a is max")
# Not recommended
if a > b:
print("a is max")Avoid using the Tab key instead of spaces.
2.2 Maximum Line Length
Limit lines to 79 characters. When a line exceeds this limit, break it using implicit continuation inside parentheses, brackets, or braces rather than a backslash.
When using parentheses for continuation, align the closing bracket with the first character of the multi‑line construct.
my_list = [
1,2,3,
4,5,6
]2.3 Operators and Line Breaks
Place the line break before the binary operator.
# Recommended
income = (income1
+ income2
- cost1
- cost2)
# Not recommended
income = (income1 +
income2 -
cost1 -
cost2)2.4 Blank Lines
Separate top‑level function and class definitions with two blank lines.
2.5 Spaces
Use a single space around binary operators and after commas, but not inside parentheses, brackets, or before commas, colons, or semicolons.
# Recommended
a = 1
a = 1 + 2
a > b
# Not recommended
a=1
a=1+2
a>b2.6 Comments
Write complete sentences, capitalize the first word, and end with a period when appropriate. Inline comments should start with # and be separated from code by at least two spaces.
x = max(a,b) # select big one2.7 Imports
Import each module on a separate line and place all imports at the top of the file, after module docstrings and before global variables.
# Recommended
import pandas
import numpy
# Not recommended
import pandas,numpy2.8 Naming Conventions
Avoid using Python keywords as identifiers and single‑character names that can be confused with digits. Use lowercase with underscores for function names, and follow PEP8 naming guidelines for variables, classes, and modules.
3. Using Autopep8 in Jupyter Notebook
Install the Autopep8 formatter and the Jupyter nbextensions plugin:
pip install Autopep8Enable the Autopep8 extension in the Nbextensions configuration page. Then, select the code cell to format and click the “hammer” button to automatically apply PEP8 style.
Example before and after formatting:
# before
a=1+2
---
# after
a = 1+2 # before
def test_pep():
print("this is test")
---
# after
def test_pep():
print("this is test")For more details, see the official PEP8 documentation at https://www.python.org/dev/peps/pep-0008/ and the Chinese translation at https://blog.csdn.net/ratsniper/article/details/78954852.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
