Python cheat sheet
Table of Contents
Terminal
python3 MY_PY.py
python3 --version
pip list
pip install "package name"Virtual environment
python3 -m venv MY_ENV
source MY_ENV/bin/activate
deactivatePEP 8
Indent: 4 space.
Encoding: UTF-8.
Line Length: max 72 letters.
- Use \ for split the sentence.
- Split
- before operators(+, -, *, /, =, **, //, %).
- after boolean operators(and or not).
Whitespace
- Space before and after every operators.
- Do not space
- after (
- before )
- between ,)
- before , : ;
- slicing
Blank Line
- 2 line
- after function, class definitions.
- 1 line
- before and after function, method.
- logical sections in function.
Import
- One module per line.
- from: in single line.
__Dunder__(double underscore)
- Declared above import.
# Comments
- Detailed, accurate, clear.
- Write complete sentence, starting with uppercase, end with period.
- English.
- #
- only when necessary.
- 2 spaces before #.
- Use docstring for functions, class, module: purpose, usage.
if
if x is not None<-if x[]""{}(,)isn’tNone
x is not<-not x is- Use
return None,else: return Noneeverywhere.
Prefix, Suffix: use .startswith(), .endswith().
Avoid: is True, is False.
Naming
|:-:|:-:| | mypackages | flatcase | | MyClass | PascalCase | | MyException | PascalCase | | ExceptionError | PascalCase | | __global_variable__ | __snake_case__ | | my_variable | snake_case | | my_function | snake_case | | CONSTANT | SCREAM_CASE | | method param start with | self, cls |
Declaration
| |
| |
| |
| |
| |
Basic
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
Other Technic
| |
| |
Module
| |
| |
| |
| |
| |
| |
Package
my_package_folder
├── __init__.py
└── my_module.py
import my_package_folder.my_moduleIn Program
| |