All chapters in this track
- 011. Variables, Types & Operators
- 022. Strings
- 033. Lists
- 044. Tuples, Sets & Dicts
- 055. Control Flow
- 066. Error Handling
- 077. Functions & Lambdas
- 088. Comprehensions
- 099. Built-in Functions
- 1010. Type Hints (3.10+)
- 1111. Classes & Dataclasses
- 1212. Iterators & Generators
- 1313. DSA Standard Library
- 1414. Python 3.9–3.11 Features for DSA
- 1515. DSA Idioms & Gotchas
- 1616. Master Quick Reference
Chapter 9 of 16
9. Built-in Functions
Tutorial
# ── sorted / reversed ──────────────────────────────────────────
sorted([3,1,4]) # [1,3,4] — new list
sorted(arr, reverse=True)
sorted(arr, key=abs)
sorted(words, key=len)
sorted(pairs, key=lambda p: p[1]) # by second element
arr.sort(key=lambda x: (x[0],-x[1]))# in-place, multi-key
list(reversed(arr)) # reversed copy
# ── enumerate / zip ────────────────────────────────────────────
for i, v in enumerate(arr): # index + value
for i, v in enumerate(arr, start=1): # start index from 1
for a, b in zip(lst1, lst2): # pair up elements
for a, b in zip(lst1, lst2, strict=True): # 3.10+: raises if lengths differ
# Transpose matrix
transposed = list(zip(*matrix)) # works on any 2D iterable
matrix_t = [list(row) for row in zip(*matrix)]
# Build dict from two lists
d = dict(zip(keys, values))
# ── min / max ──────────────────────────────────────────────────
min(3, 1, 4)
max([3,1,4,1,5])
min(words, key=len) # shortest word
max(pairs, key=lambda p: sum(p)) # pair with max sum
min(grid, key=lambda row: row[0]) # row with smallest first element
# ── sum / abs / round ──────────────────────────────────────────
sum([1,2,3]) # 6
sum([1,2,3], 10) # 16 — start from 10
sum(x**2 for x in arr) # generator (no temp list)
abs(-5) # 5
round(3.7) # 4; round(3.14159, 2) → 3.14
# ── any / all ──────────────────────────────────────────────────
any(x < 0 for x in nums) # True if any negative
all(x > 0 for x in nums) # True if all positive
# ── map / filter ──────────────────────────────────────────────
list(map(int, "1 2 3".split())) # [1, 2, 3]
a, b = map(int, input().split()) # read two ints (competitive programming)
list(filter(lambda x: x>0, nums)) # keep positives
# Prefer comprehensions for readability:
[int(x) for x in "1 2 3".split()] # same as map
[x for x in nums if x > 0] # same as filter
# ── pow / divmod ──────────────────────────────────────────────
pow(2, 10) # 1024
pow(2, 10, 1000) # 24 — (2^10)%1000 in O(log exp) — ALWAYS use for modular
divmod(17, 5) # (3, 2) — (quotient, remainder)
# ── number base conversion ──────────────────────────────────────
bin(10) # '0b1010'
oct(10) # '0o12'
hex(255) # '0xff'
bin(10)[2:] # '1010' — strip the '0b' prefix
int("1010", 2) # 10 — parse binary string
int("ff", 16) # 255 — parse hex string
int("12", 8) # 10 — parse octal string
format(10, 'b') # '1010' — no prefix
format(10, '08b') # '00001010' — zero-padded to 8 bits (fixed-width masks)
f"{255:x}" # 'ff' — f-string format spec works too
# ── range tricks ──────────────────────────────────────────────
range(n-1, -1, -1) # iterate backwards 0-indexed
range(len(arr)-1, -1, -1) # reverse indices of array
list(range(n)) # [0,1,...,n-1]
Reference
| Function | Returns | Key use |
|---|---|---|
sorted(it, key=f, reverse=b) | list | Always prefer over .sort() when needing a copy |
reversed(it) | iterator | Wrap in list() if needed |
enumerate(it, start=0) | iterator of (i,v) | |
zip(*its, strict=False) | iterator of tuples | strict=True 3.10+ |
zip(*matrix) | transpose | |
min/max(it, key=f) | element | key optional |
sum(it, start=0) | number | Start value for product: math.prod |
any(it) / all(it) | bool | Short-circuit |
map(f, it) | iterator | Prefer comprehension |
filter(f, it) | iterator | Prefer comprehension |
pow(b,e,mod) | int | Fast modular exponentiation |
divmod(a,b) | (q,r) | Quotient + remainder at once |
abs(x) | number | |
len(x) | int | |
bin(n) / oct(n) / hex(n) | str | Prefixed (0b/0o/0x); slice [2:] to strip |
int(s, base) | int | Parse string in given base (2/8/16/...) |
format(n, 'b') | str | No prefix; '08b' zero-pads to width |