What is PEP 8?

Problem

Working in programming is a difficult task. The efficiency and accuracy of a program are important, but readability is also fundamental as it’s important to understand what is written in a program. The accessibility of a program can last for years; however, if you write a piece of code but don’t come back to it for a few weeks, you might forget what you have written. But don’t worry, there is a way around that too.

Introduction

Python provides a coding style known as PEP 8. PEP refers to Python Enhancement Proposals and is one of the most readable and eye-pleasing coding styles. Since it is used by many projects all across the world, programmers should familiarize themselves with it.

Features:

1. Indentation

Indentation is one of the most significant features for enhancing code readability. Indentation helps users to separate loops, conditions, and to select case statements to visualize what is going on in a particular piece of code. Many IDEs provide auto-indentation.

2. Blank lines

Adding blank lines as part of your code makes it easier to separate print statements or any two parts of code. This enhances the readability of the code.

3. Comments

Comments about any piece of code, line, loop, or condition can be summarized in a line or two. These comments help newcomers, who are looking at the code for the first time, to easily understand it. Moreover, as mentioned above, it might be difficult for a person to understand something he/she wrote awhile ago. In this case, comments will help you recall why you used a specific logic or function.

4. Encoding

Encoding is basically converting any information in a particular form. As a user, you should try to do most of your coding in Utf-8. Python, by default, is a language that uses Utf-8, which is a good, accepted practice.

5. Naming conventions

The importance of having naming conventions that are correct is immense. Names of functions, classes, and all variables should be meaningful. For example, if you want to define two variables to store student ID, a good approach would be to name them ID_student1 and ID_student2 (you can always use an underscore to separate two words). This makes sure that, even if you do not have a comment added, the variable name itself is self-explanatory. A similar​ approach should be used in naming functions and classes.

name="User" # I am initialising the name variable with string User.
# I am adding three blank lines
if name=="User":
print("User can enter") # if condition is easier to read
# with indentation.
# this is a comment.
# this program compares the variable name with a predefined
# value User to check validation.
# One can add all kinds of comments. Helps one understand your
# approach, logic and what you're trying to achieve.
def Print_square(number):
print(number * number)
# Always try adding spaces between operator
Copyright ©2024 Educative, Inc. All rights reserved