100% Client-Side β’ 0 B Data Leaves Browser
python
Python 3 Modern Syntax & Standard Methods Cheat Sheet
Quick reference guide for Python 3 syntax, list/dict comprehensions, slicing, unpacking, decorators, and context managers.
Data Structures & Comprehensions
List comprehension with conditional filtering[x**2 for x in nums if x % 2 == 0]
Dictionary comprehension transforming keys and values{k: v.upper() for k, v in data.items()}
Reverse list, tuple, or string in O(N) using slice step -1nums[::-1]
Merge two dictionaries into a single new dictmerged = {**dict_a, **dict_b} # or dict_a | dict_b
Useful Built-ins & Itertools
Loop over collection yielding index and item pairenumerate(iterable, start=0)
Pair elements across multiple iterables concurrentlyzip(names, ages, strict=True)
Sort collection by specific attribute or key callbacksorted(users, key=lambda u: u["age"], reverse=True)
Specialized containers for auto-defaulting keys and tallying frequenciesfrom collections import defaultdict, Counter
Context Managers & File I/O
Safely read file with automatic descriptor cleanupwith open("file.json", "r", encoding="utf-8") as f: data = json.load(f)
Safely write file content with auto-closewith open("output.txt", "w") as f: f.write("Hello World")
Frequently Asked Questions
β’ What is the fastest way to format strings in modern Python?
Use f-strings (e.g. f"Hello {name}, score is {score:.2f}"). Introduced in Python 3.6, they are evaluated at runtime directly in bytecode and are faster than % formatting or str.format().