Python Cheat Sheet

Core Python syntax, data types, and common libraries.

Core Types

Concept / Tag Code Snippet Description
List Comp
new_list = [x for x in old if x > 0]
Powerful collection building.
Dict Comp
{k: v for k, v in zip(keys, vals)}
One-line dictionaries.

Functions

Concept / Tag Code Snippet Description
Args/Kwargs
def fn(*args, **kwargs): ...
Variable arguments.
Lambda
square = lambda x: x ** 2
Anonymous functions.

Data Structures

Concept / Tag Code Snippet Description
Set
unique = set([1, 1, 2])
Unique values only.

Classes

Concept / Tag Code Snippet Description
Init
def __init__(self, name): self.name = name
Constructor method.
Str Method
def __str__(self): return self.name
Printable representation.

Explore More Cheat Sheets