Python 3 Cheat Sheet



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 SheetPython 3 Cheat Sheet

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

Variables¶

Python 3.6 introduced a syntax for annotating variables in PEP 526and we use it in most examples.

Python 3 Cheat Sheet

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”¶

Cheat

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.