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

Let'e check what is PEP 8 and how to use it for writing Python Codes.
Written by Paayi Tech |09-Apr-2019 | 0 Comments | 711 Views

PEP 8 is a style of coding that python emphasize 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 few months. In python unlike other languages indentation is necessary. It is a rule to give 4 tabs indentation when start a statement or after defining a function, class or loops, otherwise python will generate syntax error. This indentation error was not found any languages like C, C++, C#, Java or javascripts.

Python defined some protocol that how to write neat and clean code that any one can understand. These protocols are called PEP8 and coding style is called PEP 8 compliant code.

In modern IDE’s there are PEP 8 module along with the pylint module which assure the PEP 8 compliant code. This dont raise syntax error or other errors just raise warnings with a message to arrange the code accordingly. Now we will see some example 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 next line. Following example will clear 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 understand 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 than 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 variable, we have to initialize the variable to get print its value in console. But it is not always the scenario. Sometime user have to input the value after the program is executed. This practice is called input fro 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 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 take first name of a person and than the last name. After fetching the value it will print the welcome message with the first and last name. The output snippet is as following:

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 type cast the two variables first because input keyword take as a string so for adding two we have to convert it into integer by using int keyword.

Adding Number on Run Time Python



Passing Argument:

Argument passing is another way taking 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 command line. This is used when making command lines for linux, mac or windows. Functionality of linux is largely based on 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 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 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 access 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.