In this article, we will explain PEP8 and its uses in python. Also, we will see its advantages while coding.

What is PEP8?

PEP is an abbreviation for Python Enterprise Proposal. Writing code with good logic is a crucial aspect of programming, but many other important elements can affect the quality of the code. The developer's coding style makes the code more reliable, and every developer should remember that Python rigidly follows the string's order and format.

A good coding style makes the code more readable. The code is simplified for the end user

PEP 8 is a document that contains various guidelines for writing readable Python code. PEP 8 discusses how to build beautiful code as a developer. It was officially written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary goal of PEP is to improve code readability and consistency.

PEP 8 has evolved as the style guide that most Python projects follow to; it supports a very readable and visually appealing code style. Every Python coder should read it at some time;

Here are some of the key points for you.


In Python, unlike other programming languages, indentation is utilized to define code blocks. The level of lines of code is determined by indentations, which are an important feature of the Python programming language. In general, we take the 4 spaces for indentation.

The four-space rule is not always required and can be disregarded for continuation lines.


def functionName(first_variable, second_variable, third_variable, fourth_variable): # giving 4 spaces for indentation print(first_variable)

Use docstrings

In Python, you can use both single-line and multi-line docstrings. The single-line comment, on the other hand, fits on one line; triple quotes are utilized in both scenarios. These are used to define a specific program or a specific function.


def tutorialspoint(): """It is an example for single line docstring""" """It is an example for multiline comment"""

Lines should be wrapped so that they do not exceed 79 characters

The Python standard library is conservative, requiring lines to be limited to 79 characters. Parenthesis, brackets, and braces can be used to wrap the lines. They should be used in preference instead of backslashes.


with open('/path/from/where/you/want/to/read/file') as fileOne, \ open('/path/where/you/want/the/file/to/be/written', 'w') as fileTwo: file_two.write(

Naming Conventions

To make the program less difficult and more readable, a few naming standards should be observed. At the same time, Python's naming conventions are a bit confusion, but here are a few that can be readily followed.

The names that are visible to the user as public elements of API should follow conventions that represent usage rather than implementation, according to an overarching principle.

Single lowercase letter e =2
Single upper case letter E = 2
lowercase num = 5
lowercase with underscores input_number = 5
uppercase NUM = 2
uppercase with underscores INPUT_NUMBER = 5
CapitalizedWords/ CamelCase InputNumber = 10


When using abbreviations with CapWords, make sure to capitalise all of the letters in the abbreviation. Thus, HTTPServerError is preferable to HttpServerError.

Naming conventions in python

The following table shows some of the most common naming styles in Python −

Type Naming convention Examples
Function It can be lowercase words or separate words by the underscore. demofunction, demo_function
Variable To improve readability, we can use a lowercase letter, words, or separate words. n, num, input_num
Class The first letter of the class name should be capitalized or written in camel case. The underscore should not be used to separate words. DemoClass, Model, Tutorialspoint
Method To improve readability, we can use lowercase letters, words, or separate words. demo_method, methodname
Constant It can be short, an uppercase letter, words, or separate words DEMOCONSTANT, CONSTANT, DEMO_CONSTANT
Module It cab be a lowercase letter, words, or separate words,
Package It can be a lowercase letter, words, or separate words. The underscore should not be used as separate words. package, demopackage

Blank Lines

The readability of Python code can be increased by using blank lines. When multiple lines of code are grouped together, the code becomes more difficult to read. We can get rid of it by utilizing a lot of blank vertical lines, but the reader may h**e to scroll more than necessary. To add vertical whitespace, follow the steps below.

Top-level functions and classes with two lines − Add extra vertical space around them to make them more readable.

class MyFirstClass: pass class MySecondClass: pass def main_function(): return None

Single blank line within classes − The functions defined in the class are related to one another.

class MyFirstClass: def first_method(self): return None def second_method(self): return None

Use blank lines within the function − There are times when we need to create a complicated function that includes numerous steps before the return statement. As a result, we can insert a blank line between each step.


Comments are an important element of any programming language. These are the most effective ways to describe the code. When we documented our code with appropriate comments, anyone could comprehend it. But keep the following points in mind.

  • Begin with the capital letter and write the entire sentence.

  • If the code changes, update the comment.

  • Comment and docstring line lengths are limited to 72 characters.

Block comments

Block comments are an excellent solution for a small section of code. Such comments are useful when writing multiple lines of code to achieve a single action, such as loop iteration. They assist us in comprehending the code's purpose.

  • Block comments should be indented at the same level.

  • Begin each line with # and a single space.

  • Use a single # to separate each line.


# Assigning the value of name (Block Line comment) name = "TutorialsPoint"

Inline Comments

Inline comments are used in code to describe a single statement in a part of code. We can easily see why we developed that specific line of code. The following restrictions for inline comments are specified in PEP 8.

  • Begin your comments with a and a single space.

  • Use inline comments with care

  • We should separate the inline comments on the same line as the statement to which they refer.

name = "TutorialsPoint" # Assigning the value of name (InLine comment)


We learned what Pep8 is and how to write quality code using various methods in this article.


分享链接 分享海报

用户评论 (0)