Fundamentals 8 min read

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 extension.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python Code Style: Essential PEP8 Rules and Auto‑Formatting Tips

1. What is PEP8

PEP stands for Python Enhancement Proposals, which are documents that convey information such as new guidelines. PEP numbers start at 0 and increase; PEP8 specifically defines the style guide for Python code.

2. Common Standards

Key conventions for amateur programmers include:

2.1 Indentation

PEP8 recommends using four spaces for indentation and avoiding tabs.

# Recommended
if a > b:
    print("a is max")

# Not recommended
if a > b:
print("a is max")

Avoid mixing tabs and spaces.

2.2 Maximum Line Length

Limit lines to 79 characters; when a line is longer, break it using implicit continuation inside parentheses, brackets, or braces rather than a backslash.

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, but not before commas, colons, or inside brackets.

# Recommended
a = 1
a = 1 + 2
a > b

# Not recommended
a=1
a=1+2
a>b

2.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 one

2.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,numpy

2.8 Naming Conventions

Avoid using Python keywords or ambiguous single‑character names. Use lowercase with underscores for functions, and follow PEP8 naming rules for variables, classes, and modules.

3. Using Autopep8 in Jupyter Notebook

Install the formatter and the Jupyter nbextensions plugin:

pip install Autopep8

Enable the Autopep8 extension in the Nbextensions configuration. Then select code in a notebook cell and click the “hammer” button to automatically reformat it according to PEP8.

PEP8 official site: https://www.python.org/dev/peps/pep-0008/

Chinese translation: https://blog.csdn.net/ratsniper/article/details/78954852

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

best practicescode styleformattingpep8autopep8
Python Crawling & Data Mining
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.