It contains some conventions in coding with Python. They make the codes clearer and more beautiful. Read the full doc here. Below are just some of them in my choice.
- Package & module names & function & variable:
all_lower_caseorshortwithunderscore.
- Class names: use
CapWords.
- Constant:
ALL_CAPITAL_LETTERS.
- Avoid these:
Use 4 spaces per indentation level. Note that, in this site, for a better view, I use 2 spaces for the code highlight.
Vertical align when break a continuous line:
Distinguish arguments from the rest:
Spaces are preferred. Don't mix tabs and spaces (not allowed in Python 3).
Max of 79 characters.
Operators should go with operands
Imports should usually be on separate lines:
Avoid extraneous whitespace:
For slices
Add open parenthesis/bracket right after:
No need to have verticle alignment:
With operators:
Def of a function:
Using
not inside if:Using Use
.startswith() and .endswith() instead of string slicing:For sequences, (strings, lists, tuples), use the fact that empty sequences are false:
Don't compare boolean values to True or False using
==: