Indentation
*Use four spaces for indentation
*Use hanging indentation for big expressions
*If you see a code already using tabs/space stick with tabs or respectively
Maximum line length
*79 characters in general
*72 characters for comments
Blank lines
- Top-level classes and functions are separated by two lines
- Method definitions inside classes should be separated by one blank line
WhiteSpaces in expressions
*Avoid whitespaces. Example: Should use- x=y instead of x= y
Imports
*Always import libraries at the start of your script.
*If you do many imports, you should make sure to state each import on a single line
Example: Use-
import os
import sys
Instead of
import os, sys
*You should take into account that there is an order that you need to respect when you're importing libraries. In general, you can follow this order:
Standard library imports.
Related third-party imports.
Local application/library specific imports.
Absolute imports>Relative imports
Ignore wildcard imports
Comments
*Block comments to explain code that is more complex or unfamiliar to others
*Block comments apply to code that follows
*Block comments are indented at the same level as the code. Each line of a block comment begins with the hashtag # and a single space
You use inline comments on the same line of a statement, following the code itself. These comments also start with # and a single space.
You write documentation strings or docstrings at the start of public modules, files, classes, and methods. These type of comments start with """ and end with """
Module level dunder
*A module-level dunder like (all, author, version) should be placed at the module main docstring and should be before all the import statements.
Naming Conventions
*Module-lowercase
*Class- CapWords
*Functions-lowercase
*Methods-lowercase
*Package-lowercase
*Contstans- UPPERCASE
*Type variables- CapWords
(I basically made a summary. Didn't add two points i.e about the PEP-8 package and Source file encoding. Do read the link for better understanding)
Top comments (0)