PEP 8 in Python | what is the purpose of PEP 8 in Python?In this tutorial, we will learn what PEP-8 is and how we can use it in Python coding. We will discuss the guidelines for using PEP in programming-this tutorial is aimed at beginners to intermediate. We will also discuss the benefits of using PEP-8 while coding. What is PEP?The PEP is an abbreviation form of Python Enterprise Proposal. Writing code with proper logic is a key factor of programming, but many other important factors can affect the code's quality. The developer's coding style makes the code much reliable, and every developer should keep in mind that Python strictly follows the way of order and format of the string. Adaptive a nice coding style makes the code more readable. The code becomes easy for end-user. PEP 8 is a document that provides various guidelines to write the readable in Python. PEP 8 describes how the developer can write beautiful code. It was officially written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The main aim of PEP is to enhance the readability and consistency of code. Why PEP 8 is Important?PEP 8 enhances the readability of the Python code, but why is readability so important? Let's understand this concept. Creator of Python, Guido van Rossum said, "Code is much more often than it is written." The code can be written in a few minutes, a few hours, or a whole day but once we have written the code, we will never rewrite it again. But sometimes, we need to read the code again and again. At this point, we must have an idea of why we wrote the particular line in the code. The code should reflect the meaning of each line. That's why readability is so much important. We will describe few important guidelines for writing effective code that can be read by others as well. Naming ConventionWhen we write the code, we need to assign name to many things such as variables, functions, classes, packages, and a lot more things. Selecting a proper name will save time and energy. When we look back to the file after sometime, we can easily recall what a certain variable, function, or class represents. Developers should avoid choosing inappropriate names. The naming convention in Python is slightly messy, but there are certain conventions that we can follow easily. Let's see the following naming convention. Example - Single lowercase letter Single upper case letter Lowercase Lower_case_with_underscores UPPERCASE UPPER_CASE_WITH_UNDERSCORES CapitalizedWords (or CamelCase) Note: While using abbreviations in CapWords, capitalize all the letters of the abbreviation. Thus HTTPServerError is better than HttpServerError.Name StyleBelow is the table that specifies some of the common naming styles in Python. Consider the following table.
Above are some common naming conventions that are useful to beautify the Python code. For additional improvement, we should choose the name carefully. Code LayoutThe code layout defines how much the code is readable. In this section, we will learn how to use whitespace to improve code readability. IndentationUnlike other programming languages, the indentation is used to define the code block in Python. The indentations are the important part of the Python programming language and it determines the level of lines of code. Generally, we use the 4 space for indentation. Let's understand the following example. Example - In the above example, the indented print statement will get executed if the condition of if statement is true. This indentation defines the code block and tells us what statements execute when a function is called or condition trigger.
We can also use the tabs to provide the consecutive spaces to indicate the indentation, but whitespaces are the most preferable. Python 2 allows the mixing of tabs and spaces but we will get an error in Python 3. Indentation following Line BreakIt is essential to use indentation when using line continuations to keep the line to fewer than 79 characters. It provides the flexibility to determining between two lines of code and a single line of code that extends two lines. Let's understand the following example. Example - We can use the following structure. Use docstringPython provides the two types of document strings or docstring - single line and multiple lines. We use the triple quotes to define a single line or multiline quotes. Basically, these are used to describe the function or particular program. Let's understand the following example. Example - Should a Line Break Before or After a Binary Operator?The lines break before or after a binary operation is a traditional approach. But it affects the readability extensively because the operators are scattered across the different screens, and each operator is kept away from its operand and onto the previous line. Let's understand the following example. Example - As we can see in the above example, it seems quite messy to read. We can solve such types of problems by using the following structure. Example - Python allows us to break line before or after a binary operator, as long as the convention is consistent locally. Importing moduleWe should import the modules in the separates line as follows. Wrong We can also use the following approach. The import statement should be written at the top of the file or just after any module comment. Absolute imports are the recommended because they are more readable and tend to be better behaved. However, we can use the explicit relative imports instead of absolutes imports, especially dealing with complex packages. Blank LinesBlank lines can be improved the readability of Python code. If many lines of code bunched together the code will become harder to read. We can remove this by using the many blank vertical lines, and the reader might need to scroll more than necessary. Follow the below instructions to add vertical whitespace.
The above way can remove the whitespaces to improve the readability of code. Put the Closing BracesWe can break lines inside parentheses, brackets using the Line continuations. PEP 8 allows us to use closing braces in implies line continuations. Let's understand the following example.
Both methods are suitable to use, but consistency is key, so choose any one and continue with it. CommentsComments are the integral part of the any programming language. These are the best way to explain the code. When we documented our code with the proper comments anyone can able to understand the code. But we should remember the following points.
Block CommentBlock comments are the good choice for the small section of code. Such comments are useful when we write several line codes to perform a single action such as iterating a loop. They help us to understand the purpose of the code. PEP 8 provides the following rules to write comment block.
Let's see the following code. We can use more than paragraph for the technical code. Let's understand the following example. Inline CommentsInline comments are used to explain the single statement in a piece of code. We can quickly get the idea of why we wrote that particular line of code. PEP 8 specifies the following rules for the inline comments.
Following is the example of inline comments. Sometimes, we can use the naming convention to replace the inline comment. We can use the following naming convention. Inline comments are essential but block comments make the code more readable. Avoid Unnecessary Adding WhitespacesIn some cases, use of whitespaces can make the code much harder to read. Too much whitespaces can make code overly sparse and difficult to understand. We should avoid adding whitespaces at the end of a line. This is known as trailing whitespaces. Let's see the following example. Example - 1 Example - 3 Programming RecommendationAs we know that, there are several methods to perform similar tasks in Python. In this section, we will see some of the suggestions of PEP 8 to improve the consistency. Avoid comparing Boolean values using the equivalence operatorWe shouldn't use the equivalence operator == to compare the Boolean values. It can only take the True or False. Let's see following example. This approach is simple that's why PEP 8 encourages it. Empty sequences are false in if statementsIf we want to check whether a given list is empty, we might need to check the length of list, so we need to avoid the following approach. However, if there is any empty list, set, or tuple. We can use the following way to check. The second method is more appropriate; that's why PEP 8 encourages it. Don't use not is in if statementThere are two options to check whether a variable has a defined value. The first option is with x is not None, as in the following example. A second option is to evaluate x is None and if statement based on not the outcome. Both options are correct but the first one is simple, so PEP 8 encourages it. ConclusionWe have discussed the PEP 8 guidelines to make the code remove ambiguity and enhance readability. These guidelines improve the code, especially when sharing the code with potential employees or collaborators. We have discussed what PEP is and why it uses, how to write code that is PEP 8 compliant. Moreover, we have a brief introduction to the naming conventions. If you want more information regarding the PEP 8, you can read the full documentation or visit PEP8.org. Next TopicPython User Groups |