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:
This will make the string for not going out of the site.
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.
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:
Only a good coding practice can make a great programmer and these protocols make coding easy to understand.
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.
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:
Adding two numbers:
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.
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.
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.
It is to be noted that the sys.argv 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.
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.