Python
Python
- Python 2 is no longer supported (as of January 1, 2020)
- Python 3 is the latest version (as of 2023)
- Python 3 is the recommended version for new projects
create python virtual environemnt
install packages
Python Basics
False, True, no x++, use x += 1
.pyfile extension- Indentation is significant (4 spaces or 1 tab)
- Use
#for comments - Use
"""for docstrings - Use
__init__for constructors - Use
__str__for string representation - Use
__repr__for official string representation - Use
__name__for module name - Use
__main__for main module - to exit, use
exit()orquit() input()to pause the program and wait for user inputprint()to print to the console- if you mix space and tab, you will get an
IndentationError
Reserved Keywords
| Keyword | Keyword | Keyword | Keyword |
|---|---|---|---|
| False | None | True | and |
| as | assert | async | await |
| break | class | continue | def |
| del | elif | else | except |
| finally | for | from | global |
| if | import | in | is |
| lambda | nonlocal | not | or |
| pass | raise | return | try |
| while | with | yield |
DataType Declaration
Although Python is dynamically typed, you can use type hints to indicate the expected type of a variable.