Python Tutorials: How to Write an Amazing Python Code using PEP 8

Python Tutorials: Let'e check what is PEP 8 and how to use it for writing Python Codes.
Written by Paayi Tech |17-Oct-2020 | 0 Comments | 779 Views

PEP 8 is a style of coding that python emphasizes to program a clean code. It is not necessary, but it is recommended. If your code does not look clean, it would be hard to understand even by yourself after a few months. In python, unlike other languages, indentation is necessary. It is a rule to give four tabs indentation when starting a statement or after defining a function, class, or loops. Otherwise, python will generate a syntax error. This indentation error was not found in any languages like C, C++, C#, Java, or javascript.

Python defined some protocols on how to write neat and clean code that anyone can understand. These protocols are called PEP8, and the coding style is called PEP 8 compliant code.

In modern IDE's there are PEP 8 modules along with the pylint module, which assure the PEP 8 compliant code. This doesn't raise syntax errors or other errors. Just raise warnings with a message to arrange the code accordingly. Now we will see some examples of how to make our code compliant.


Never make a string too long:

According to the official documentation, the string should not be bigger than 70 characters per line. After that, break the line and arrange it on the next line. The following example will clarify this concept:


String = 'Today the weather is cold. I am thinking of making soup.'



String = '''Today the weather is cold.

I am thinking of making soup.'''

This will make the string for not going out of the site.


Nested Lists:

It is very messy to show a nested list in a single line. You cant what is happening and what are the values of nested lists.


Messy Way:

list1 = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]



PEP 8:


list1 = [







Too Many Arguments:

When there are too many arguments of a function, then it is suggested to write the argument in a hanging position just like we write in a string PEP8 method above:

Bad Coding:


func1(value1, value2, value3, value4, value5)











Only a good coding practice can make a great programmer, and these protocols make coding easy to understand.


User Input:

As we saw in the variable, we have to initialize the variable to get print its value in the console. But it is not always the scenario. Sometimes users have to input the value after the program is executed. This practice is called input from the user. In C++, there is a keyword cin to input user value, and in java, it is system.writeline. In python, it is very easy to input a value from the user; no external library is needed to fetch the value.

Following program is the implementation of fetching the value from user.

first_name = input("Enter your first name: ")

last_name = input("Enter your last name: ")


print("Welcome {first_name} {last_name}")


The above-mentioned program first takes the first name of a person and then the last name. After fetching the value, it will print the welcome message with the first and last name. The output snippet is as follows:

Welcome Full Name in Python


Adding two numbers:

first_number = input("Enter first number: ")

second_number = input("Enter second number: ")


result = int(first_number) + int(second_number)



For adding two numbers, we have to typecast the two variables first because the input keyword takes as a string, so for adding two, we have to convert it into an integer by using the int keyword.

Adding Number on Run Time Python

Passing Argument:

Argument passing is another way of making an argument from the user. This is a way of inputting the value in a command line and can only be applicable when running a program from the command line. This is used when making command lines for Linux, mac, or windows. The functionality of Linux is largely based on the command line argument. Following is the example of command-line argument passing.

import sys


add = int(sys.argv[1])+int(sys.argv[2])

print(f"Result of {sys.argv[1]} + {sys.argv[2]}={add}")


We first have to import the system module of python; that is why they are also known as system argument. The output can only be printed in terminal or command prompt in case of windows.


passing argument in python

It is to be noted that the sys.argv[0] is the name of that particular program.

More clearly, we can see the index of the argument by the following program in which the argument is printed corresponding to its index.

import sys


for i in range(len(sys.argv)):

    print(f"Argument = {sys.argv[i]} ||  Index = {i}")


In the above program, we start a loop from zero to the length of arguments. The sys.argv resides within a list, so it can only be accessed by the index. The output of the above code is following.

passing values in python paayi



Login/Sign Up


Related Posts

© Copyright 2020, All Rights Reserved.

This site uses cookies. By continuing to use this site or clicking "I Agree", you agree to the use of cookies. Read our cookies policy and privacy statement for more information.