Python Syntax & Built-ins Cheat Sheet — Modern Python 3 Reference

Core Python syntax, list comprehensions, dictionary operations, string slicing, lambda functions, and context managers.

Data Structures & Comprehensions

[x**2 for x in nums if x % 2 == 0]

List comprehension: filter even numbers and square them into a new list

{k: v for k, v in zip(keys, values)}

Dictionary comprehension: combine two separate lists into a key-value dictionary

s[::-1]

Reverse a string or list using negative slice stepping

for i, item in enumerate(items, start=1):

Iterate over collection with an automatic incrementing counter index

File I/O & Exception Handling

with open("file.txt", "r", encoding="utf-8") as f:

Context manager safely auto-closes file descriptor even on error

try ... except ValueError as e: ... finally:

Catch specific exception type and execute guaranteed cleanup block

sorted(users, key=lambda u: u["age"], reverse=True)

Sort collection of dictionaries by specific property descending