How to Write Beautiful Python Code With PEP 8

28 February 2024 by
Vishnu Gopal. P
| No comments yet

Humans, been the most intelligent and creative, unlike machine generated codes, there is chance for different coding style for a single solution. The thought of one human might be different from another and the same will be reflected in the coding. This makes it hard for a different user to understand a code written by another person. The codes are more read than that is been written. This issue of no standard in coding makes the understanding of the code difficult. PEP is introduced so that this issue can be irradiated. PEP also known as Python Enhancement Proposal is a documentation which is meant to provide standardization to the python code. There are many different versions of it and one among these is PEP 8. This helps the community to focus on a design which aims on the style and design.

Naming of the variables and providing required whitespaces are the fundamentals and for the understanding of the requirement of the code, commented lines are to be provided. The rules of PEP 8 will make the code more readable. This guidelines will yield a professional developer style writing.

Naming style

There is many naming style rules set by the syntax as well as PEP 8. The combination will yield the most reliable coding standard. While defining a variable, all the letters should be in lowercase as a single letter. If needed, each word should be separated by underscores so as to enhance readability. While defining a function, all the letters should be in lowercase. The each word should be separated by underscores so as to enhance readability. While defining a class, Camel case should be followed. The first letter should be capital letter and if there is a second word in the name, instead of underscores, capital letters should be used. While defining a module, all the letters should be in lowercase as a single letter. Each word should be separated by underscores so as to enhance readability. While considering the usage for package, short lowercases should be used without underscores.

Choosing names

While choosing name for a specific function, the name should define and give an output for the user to understand the purpose of the function. The naming should be favourable to the common sense of the humans. The variable name should also be given so that an idea about what should be and will be stored in the variable is obtained from the name assigned for the variable. Considerations should be provided while using abbreviations since, a logical decision while making a name might not be reflected in a long run and turn back.

Blank spaces

The usage of blank spaces should be done in such a way that the codes are not bunched together making it hard to read and understand the flow. While defining and writing a class or a function, it is preferred to leave 2 vertical lines among respect to each other. This makes a clear cut understanding about the prevailing structure of the class. If 2 or more method is been include within a class, there should be 1 vertical line space among the methods. While in complex functions, where there are chance for confusion caused by variant of codes, 1 whitespace is to be used to separate the logical steps.

Maximum line and breaking

79 character per line is noted as standard and if a ‘()’, ’[], or ‘:’ is used, it is noted as that the continuation is on the next line. If in a binary operation and line break is needed, the new line should contain the binary operation followed by the operant. 


The need for indentation is high priority in python coding. The indentation provides detail about in which statement each line of code is assigned to. In PEP 8, 4 consecutive spaces are to be given to indicate indentation. Tabs shouldn’t be used to do so. Line breaking and indentation are related to each other since the line break and perfect usage of the intention decides the scope of the code written and where the code should be executed.


While doing a comment, there is a need for making sure that the idea of the code are explained as needed. This is found to be idle if is done in 72 words. The comment should start with a capital letter. The comment should be updated while to code is updated. While commenting a single line, # commenting is preferred. Start each line with a # followed by a single space. Separate paragraphs by a line containing a single #.

While doing an inline commenting, a # and a single space is used to comment. It should be minimal and an explanation should not be given. Documentation strings (“””) or (‘’’) are used for long commenting which are in need for documentation writings. 

Whitespaces and binary operations

While using “=”, white spaces should not be provided. In an operation which are confusing, the spaces can be used to defer and identify the workflow of the operation.

Programming recommendations

Using the Boolean values True or False, for equivalence operator (==) is unnecessary. If PEP 8 is found to break the compatibility of the existing software, it is recommend not to follow PEP 8. To be compatible with older versions, PEP shouldn’t be used. Imports are preferred to be on separate line rather than separated by “,”. The identifiers should have the ASCII compatibility.

To ensure that PEP 8 style management is done, one can use tools like, pycodestyle, flakes, pylint, etc. to make this alignment automated, auto formatters like autopep8, yapf, black, etc. can be used. Ruff is another tool which can help in combining a lint tool and a auto formatter.

Start writing here...

Vishnu Gopal. P 28 February 2024
Share this post
Sign in to leave a comment