Coding Style Guide in Python: PEP 8

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.

  1. Use 4-space indentation and no tabs.

Example:

Coding Style Guide in Python for Data Science - PST Analytics

It should be noted that the 4 space rule is not compulsory, and we can let go off the rule.

  1. 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.

Example:

Coding Style Guide in Python for Data Science - PST Analytics

  1. 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.

Example:

Coding Style Guide in Python for Data Science - PST Analytics

  1. 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 #.

Example:

pst = pst  +  1                          #  Increment

  1. Using trailing commas:

    It is only mandatory when we make a tuple.

Example:

tup =  (“pst” , )

  1. Use Python’s default UTF-8 OR ASCII encodings: Use the UTF-8 or ASCII encodings in case of international environments.
  2. We should use spaces around operators and after commas, but should not be there directly inside bracketing constructs.
  3. 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.

Coding Style Guide in Python for Data Science - PST Analytics

  1. 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.
  2. 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.
  3. 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.

So, to learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *