Python Cheat Sheet
A quick reference for Python syntax, built-in functions, and data structures.
Data Structures
| List | fruits = ['apple', 'banana', 'cherry'] |
| Tuple | point = (10, 20) (immutable) |
| Set | unique = {1, 2, 3} (no duplicates) |
| Dict | user = {'name': 'John', 'age': 30} |
| List comprehension | [x**2 for x in range(10) if x % 2 == 0] |
| Dict comprehension | {k: v for k, v in zip(keys, values)} |
| Slicing | arr[1:4] — indices 1, 2, 3; arr[::-1] — reversed |
Functions & Control
| Function | def greet(name: str) -> str: return f'Hello {name}' |
| Lambda | lambda x: x * 2 |
| *args | def fn(*args): # variable positional args |
| **kwargs | def fn(**kwargs): # variable keyword args |
| Decorator | @staticmethod / @classmethod / @property |
| Context manager | with open('file.txt') as f: |
Common Stdlib Modules
| os | os.path.join(), os.environ, os.listdir() |
| json | json.loads(), json.dumps(), json.dump() |
| re | re.search(), re.match(), re.findall(), re.sub() |
| datetime | datetime.now(), date.today(), timedelta |
| pathlib | Path('dir/file.txt').read_text(), .write_text() |
| collections | defaultdict, Counter, namedtuple, deque |