PY
Python Cheatsheet
Core Python 3 syntax - data structures, comprehensions, and the built-in functions used constantly.
Data structures
[1, 2, 3]List - ordered, mutable
(1, 2, 3)Tuple - ordered, immutable
{1, 2, 3}Set - unordered, unique values
{"key": "value"}Dictionary - key-value pairs
Comprehensions
[x*2 for x in range(5)]List comprehension
{x: x*2 for x in range(5)}Dict comprehension
[x for x in items if x > 0]List comprehension with filter
String formatting
f"{name} is {age}"f-string interpolation
"{}-{}".format(a, b).format() method
str.strip()Remove leading/trailing whitespace
str.split(",")Split string into a list
",".join(list)Join a list into a string
Common built-ins
len(x)Length of a list/string/dict
range(start, stop, step)Generate a sequence of numbers
enumerate(list)Loop with index and value
zip(list1, list2)Pair up elements from two lists
sorted(list, key=fn)Return a sorted copy
map(fn, list)Apply a function to every item