Python
home | https://www.python.org/ |
wiki | https://en.wikipedia.org/wiki/Python_(programming_language) |
Language
#!/usr/bin/env python3 def main(): print("Hello World!") if __name__ == '__main__': main()
- 1991-
- Author: Guido van Rossum
- PEP - Python Enhancement Proposals
- .format() https://pyformat.info/
- f"string" https://fstring.help/
Changelog
year | ver | usage | PEP | description |
---|---|---|---|---|
2015 | 3.5 | typing package | 0484 | type hints |
zipapp package | 0441 | .pyz a way to package source code | ||
mat1 @ mat2 | 0465 | matrix multiplication | ||
async/await | 0492 | coroutines with async and await syntax | ||
*[1, 2] => 1, 2 | 0448 | additional unpacking generalizations | ||
**{'a': 1, 'c': 3} => a=1, c=3 | aka splat operator | |||
2016 | 3.6 | f"hello {name}" | 0498 | formatted string literals |
1_000_000 | 0515 | underscores in numeric literals | ||
captain: string | 0526 | type annotations(aka hints) for variables | ||
can use await and yield on 1 async function | 0525 | asynchronous generators | ||
[ i async for i in aiter() if i % 2] | 0530 | asynchronous comprehensions | ||
[await fun() for fun in functs if await cond()] | ||||
secrets package | 0506 | |||
2018 | 3.7 | breakpoint() | ||
__getattr__ | 0562 | |||
_ns | 0564 | |||
dicts remember insertion | ||||
@dataclass | 0567 | |||
importlib.resources | ||||
2019 | 3.8 | (:=) "walrus operator" | 0572 | assignment as an expression |
@functor.cached_property | ||||
importlib.metadata | ||||
typing.TypedDict | specify the keys | |||
typing.Final | mark as a constant | |||
typing.Literal | ||||
f'{name=}' | print the name and value | |||
(, /) positional only parameters | 0570 | forbids explicitly passing the name | ||
2020 | 3.9 | parser from LLI to PEG | 0617 | |
zoneinfo package | ||||
¦ dictionary merge | ||||
¦= dictionary update | ||||
str.removesuffix() | ||||
str.removeprefix() | ||||
generic typing.Dict as dict | 0585 | |||
generic typing.List as list | ||||
2021 | 3.10 | match/case | 0634 | a sort of "switch" |
with (ctx1() as e1, ctx2() as e2): | parentheses on with context managers | |||
(int ¦ float) instead of typing.Union[int,float] | 0604 | type union operator | ||
statistics.(covariance/correlation/lregression) | ||||
2022 | 3.11 | tomllib package | to read toml files, like pyproject.toml | |
x: NotRequired[str] | 0655 | for potentially missing keys on a TypedDict | ||
x: Required[str] | ||||
LiteralString type | 0675 | raises and error if the string is not static | ||
.add_not() to all exceptions | 0678 | to enrich exceptions without raise another |
Decorators (@)
@classmethod | def |
@dataclass | class |
Special Methods
https://docs.python.org/3/reference/datamodel.html#object.__setitem__
2nd | 3rd | 4th | Description | |
---|---|---|---|---|
__init__() | constructor | |||
__call__() | x | |||
__bool__() | ||||
__hash__() | ||||
__enter__() | with statement context manager | |||
__exit__() | exc_type | exc_value | traceback | |
__eq__() | other | |||
__lt__() | other | |||
__gt__() | other | |||
__repr__() | unambiguous representation | |||
__str__() | readable representation | |||
__len__() | len(a) | |||
__getitem__() | index | a[n] | ||
__setitem__() | index | element | ||
__contains__() | element | |||
__reversed__() | ||||
__getattribute__() | name | |||
__getattr__() | name | |||
__setattr__() | name | value | ||
__delattr__() | name | |||
__iter__() | iterator | |||
__next__() | iterator | |||
__add__() | other | emulating numeric types | ||
__sub__() | other |
Type Hints
- PEP 484 – Type Hints https://peps.python.org/pep-0484/
- PEP 483 – The Theory of Type Hints https://peps.python.org/pep-0483/
- PEP 482 – Literature Overview for Type Hints https://peps.python.org/pep-0482/
Standard Library
Concurrent Execution
concurrent | The concurrent package |
concurrent.futures | Launching parallel tasks |
contextvars | Context Variables |
multiprocessing | Process-based parallelism |
multiprocessing.shared_memory | Shared memory for direct access across processes |
queue | A synchronized queue class |
sched | Event scheduler |
subprocess | Subprocess management |
_thread | Low-level threading API |
threading | Thread-based parallelism |
Data Types
array | Efficient arrays of numeric values |
bisect | Array bisection algorithm |
calendar | General calendar-related functions |
collections | Container datatypes |
collections.abc | Abstract Base Classes for Containers |
copy | Shallow and deep copy operations |
datetime | Basic date and time types |
enum | Support for enumerations |
graphlib | Functionality to operate with graph-like structures |
heapq | Heap queue algorithm |
pprint | Data pretty printer |
reprlib | Alternate repr() implementation |
types | Dynamic type creation and names for built-in types |
weakref | Weak references |
zoneinfo | IANA time zone support |
File and Directory Access
filecmp | File and Directory Comparisons |
fileinput | Iterate over lines from multiple input streams |
fnmatch | Unix filename pattern matching |
glob | Unix style pathname pattern expansion |
linecache | Random access to text lines |
os.path | Common pathname manipulations |
pathlib | Object-oriented filesystem paths |
shutil | High-level file operations |
stat | Interpreting stat() results |
tempfile | Generate temporary files and directories |
File Formats
csv | CSV File Reading and Writing |
configparser | Configuration file parser |
tomllib | Parse TOML files |
netrc | netrc file processing |
plistlib | Generate and parse Apple .plist files |
Generic Operating System Services
argparse | Parser for command-line options, arguments and sub-commands |
ctypes | A foreign function library for Python |
curses | Terminal handling for character-cell displays |
curses.ascii | Utilities for ASCII characters |
curses.panel | A panel stack extension for curses |
curses.textpad | Text input widget for curses programs |
errno | Standard errno system symbols |
getopt | C-style parser for command line options |
getpass | Portable password input |
io | Core tools for working with streams |
logging | Logging facility for Python |
logging.config | Logging configuration |
logging.handlers | Logging handlers |
os | Miscellaneous operating system interfaces |
platform | Access to underlying platform’s identifying data |
time | Time access and conversions |
Graphical User Interfaces with Tk
tkinter | Python interface to Tcl/Tk |
tkinter.colorchooser | Color choosing dialog |
tkinter.font | Tkinter font wrapper |
tkinter.messagebox | Tkinter message prompts |
tkinter.scrolledtext | Scrolled Text Widget |
tkinter.dnd | Drag and drop support |
tkinter.ttk | Tk themed widgets |
tkinter.tix | Extension widgets for Tk |
Importing Modules
importlib | The implementation of import |
modulefinder | Find modules used by a script |
pkgutil | Package extension utility |
runpy | Locating and executing Python modules |
zipimport | Import modules from Zip archives |
Internet Data Handling
binascii | Convert between binary and ASCII |
base64 | Base16, Base32, Base64, Base85 Data Encodings |
An email and MIME handling package | |
json | JSON encoder and decoder |
mailbox | Manipulate mailboxes in various formats |
mimetypes | Map filenames to MIME types |
quopri | Encode and decode MIME quoted-printable data |
Internet Protocols and Support
ftplib | FTP protocol client |
http | HTTP modules |
http.client | HTTP protocol client |
http.cookiejar | Cookie handling for HTTP clients |
http.cookies | HTTP state management |
http.server | HTTP servers |
imaplib | IMAP4 protocol client |
ipaddress | IPv4/IPv6 manipulation library |
poplib | POP3 protocol client |
smtplib | SMTP protocol client |
socketserver | A framework for network servers |
urllib | URL handling modules |
urllib.error | Exception classes raised by urllib.request |
urllib.parse | Parse URLs into components |
urllib.request | Extensible library for opening URLs |
urllib.response | Response classes used by urllib |
urllib.robotparser | Parser for robots.txt |
uuid | UUID objects according to RFC 4122 |
webbrowser | Convenient web-browser controller |
wsgiref | WSGI Utilities and Reference Implementation |
xmlrpc | XMLRPC server and client modules |
xmlrpc.client | XML-RPC client access |
xmlrpc.server | Basic XML-RPC servers |
Python Runtime Services
abc | Abstract Base Classes |
atexit | Exit handlers |
builtins | Built-in objects |
contextlib | Utilities for with-statement contexts |
dataclasses | Data Classes |
__future__ | Future statement definitions |
gc | Garbage Collector interface |
inspect | Inspect live objects |
__main__ | Top-level code environment |
site | Site-specific configuration hook |
sys | System-specific parameters and functions |
sysconfig | Provide access to Python’s configuration information |
traceback | Print or retrieve a stack traceback |
warnings | Warning control |
Structured Markup Processing Tools
html | HyperText Markup Language support |
html.parser | Simple HTML and XHTML parser |
html.entities | Definitions of HTML general entities |
xml.etree.ElementTree | The ElementTree XML API |
xml.dom | The Document Object Model API |
xml.dom.minidom | Minimal DOM implementation |
xml.dom.pulldom | Support for building partial DOM trees |
xml.sax | Support for SAX2 parsers |
xml.sax.handler | Base classes for SAX handlers |
xml.sax.saxutils | SAX Utilities |
xml.sax.xmlreader | Interface for XML parsers |
xml.parsers.expat | Fast XML parsing using Expat |
Superseded Modules
aifc | Read and write AIFF and AIFC files |
asynchat | Asynchronous socket command/response handler |
asyncore | Asynchronous socket handler |
audioop | Manipulate raw audio data |
cgi | Common Gateway Interface support |
cgitb | Traceback manager for CGI scripts |
chunk | Read IFF chunked data |
crypt | Function to check Unix passwords |
imghdr | Determine the type of an image |
imp | Access the import internals |
mailcap | Mailcap file handling |
msilib | Read and write Microsoft Installer files |
nis | Interface to Sun’s NIS (Yellow Pages) |
nntplib | NNTP protocol client |
optparse | Parser for command line options |
ossaudiodev | Access to OSS-compatible audio devices |
pipes | Interface to shell pipelines |
smtpd | SMTP Server |
sndhdr | Determine type of sound file |
spwd | The shadow password database |
sunau | Read and write Sun AU files |
telnetlib | Telnet client |
uu | Encode and decode uuencode files |
xdrlib | Encode and decode XDR data |
Text Processing Services
difflib | Helpers for computing deltas |
re | Regular expression operations |
readline | GNU readline interface |
rlcompleter | Completion function for GNU readline |
string | Common string operations |
stringprep | Internet String Preparation |
textwrap | Text wrapping and filling |
unicodedata | Unicode Database |
dev / test / debug
Development Tools
typing | Support for type hints |
pydoc | Documentation generator and online help system |
doctest | Test interactive Python examples |
unittest | Unit testing framework |
unittest.mock | mock object library |
unittest.mock | getting started |
2to3 | Automated Python 2 to 3 code translation |
test | Regression tests package for Python |
test.support | Utilities for the Python test suite |
test.support.socket_helper | Utilities for socket tests |
test.support.script_helper | Utilities for the Python execution tests |
test.support.bytecode_helper | Support tools for testing correct bytecode generation |
test.support.threading_helper | Utilities for threading tests |
test.support.os_helper | Utilities for os tests |
test.support.import_helper | Utilities for import tests |
test.support.warnings_helper | Utilities for warnings tests |
Debugging and Profiling
bdb | Debugger framework |
faulthandler | Dump the Python traceback |
pdb | The Python Debugger |
timeit | Measure execution time of small code snippets |
trace | Trace or track Python statement execution |
tracemalloc | Trace memory allocations |
Python Language Services
ast | Abstract Syntax Trees |
symtable | Access to the compiler’s symbol tables |
token | Constants used with Python parse trees |
keyword | Testing for Python keywords |
tokenize | Tokenizer for Python source |
tabnanny | Detection of ambiguous indentation |
pyclbr | Python module browser support |
py_compile | Compile Python source files |
compileall | Byte-compile Python libraries |
dis | Disassembler for Python bytecode |
pickletools | Tools for pickle developers |