In any programming language coding and applying logic forms the base but also the coding style matters. So, in Python, a strict way of order and format of scripting is maintained for Data Science.
In Python PEP 8 is the style guide which most of the projects follow. It enables us to make readable and pleasant coding. Below we have provided some of the important points of the PEP 8 coding guide.
- Use 4-space indentation and no tabs.
It should be noted that the 4 space rule is not compulsory, and we can let go off the rule.
- Use docstrings: In Python, we have the freedom to use both single and multi-line docstrings. We will use triple quotes in both cases. These are used for defining a particular program or a function.
- Wrap lines to avoid exceeding 79 characters: Python standard library is conservative in nature, and it limits lines to 79 characters. We can wrap the lines using parenthesis, braces, and brackets. We should use them in preference to backslashes.
- Using regular and updated comments is of value to both coders and users: We have various types and conditions which, when followed, will be of tremendous use from the programs and user’s perspective. Comments should form sentences that are complete. The first word of such a comment should be capitalized unless it is an identifier beginning with a lower case. When we are using short comments, we will be able to omit the period at the end of the comment. In the case of block comments, we have more than one paragraph, and each sentence will end with a period. The block and inline comments can be written followed by single #.
pst = pst + 1 # Increment
Using trailing commas:
It is only mandatory when we make a tuple.
tup = (“pst” , )
- Use Python’s default UTF-8 OR ASCII encodings: Use the UTF-8 or ASCII encodings in case of international environments.
- We should use spaces around operators and after commas, but should not be there directly inside bracketing constructs.
- Naming conventions: These are there for decreasing the complexity and increasing readability. To be honest in Python naming convention is a mess but few conventions can be followed.
We have an overriding principle which states that names visible to public parts of API has to follow conventions reflecting usage and not implementation.
Below are a few naming conventions which are easy to use.
- Characters not used as identifiers: ‘l’(lowercase L), ‘O’ and ‘I’ (uppercase i) used as single character variable name is similar to numerals one and zero.
- Do not use non-ASCII characters in identifiers: We should not use non-ASCII characters if people who speak a different language will be using the code.
- Name the classes and functions consistently: According to the convention for classes, CamelCase should be used, and for function and methods, lower_case_with_underscores should be used. We should use self as name of first method argument.