This document is a quick cheat sheet showing how the PEP 484 typeannotation notation represents various common types in Python 3.
Python 3 Cheat Sheet. Free Bonus: Python Cheat Sheet. Get a Python Cheat Sheet (PDF) and learn the basics of Python 3, like working with data types, dictionaries. Welcome to Python Cheatsheet! ☕️ Anyone can forget how to make character classes for a regex, slice a list or do a for loop. This cheat sheet tries to provide a basic reference for beginner and advanced developers, lower the entry barrier for newcomers and help veterans refresh the old tricks.
Note
![Python 3 Cheat Sheet Python 3 Cheat Sheet](https://preview.redd.it/e1lxmo33jdk41.jpg?width=640&crop=smart&auto=webp&s=3cfd0b20f2bbc81af3a1f06572b9d684dc6ecff4)
![Python 3 Cheat Sheet Python 3 Cheat Sheet](https://miro.medium.com/max/4096/1*ebqtgwbBj193lBM4Of0tfQ.png)
Technically many of the type annotations shown below are redundant,because mypy can derive them from the type of the expression. Somany of the examples have a dual purpose: show how to write theannotation, and show the inferred types.
![Python Python](https://miro.medium.com/max/3650/1*totJoCc3l7BdeY-mEQ6HHQ.png)
Variables¶
Python 3.6 introduced a syntax for annotating variables in PEP 526and we use it in most examples.
![Python 3 Cheat Sheet Python 3 Cheat Sheet](https://i.redd.it/rr28tn7ho9x01.png)
Built-in types¶
Functions¶
Python 3 supports an annotation syntax for function declarations.
When you’re puzzled or when things are complicated¶
Python 3 Cheat Sheet 2020
Standard “duck types”¶
In typical Python code, many functions that can take a list or a dictas an argument only need their argument to be somehow “list-like” or“dict-like”. A specific meaning of “list-like” or “dict-like” (orsomething-else-like) is called a “duck type”, and several duck typesthat are common in idiomatic Python are standardized.
You can even make your own duck types using Protocols and structural subtyping.
Classes¶
Python 3 Cheat Sheet Laurent Pointal
Coroutines and asyncio¶
See Typing async/await for the full detail on typing coroutines and asynchronous code.
Miscellaneous¶
Free Python 3 Cheat Sheet
Decorators¶
Decorator functions can be expressed via generics. SeeDeclaring decorators for more details.
![](https://cdn-ak.f.st-hatena.com/images/fotolife/r/ruriatunifoefec/20200910/20200910011341.png)