Vasu Patel logoVasu Patel
← Python Syntax & DSA Reference
All chapters in this track
  1. 011. Variables, Types & Operators
  2. 022. Strings
  3. 033. Lists
  4. 044. Tuples, Sets & Dicts
  5. 055. Control Flow
  6. 066. Error Handling
  7. 077. Functions & Lambdas
  8. 088. Comprehensions
  9. 099. Built-in Functions
  10. 1010. Type Hints (3.10+)
  11. 1111. Classes & Dataclasses
  12. 1212. Iterators & Generators
  13. 1313. DSA Standard Library
  14. 1414. Python 3.9–3.11 Features for DSA
  15. 1515. DSA Idioms & Gotchas
  16. 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

FunctionReturnsKey use
sorted(it, key=f, reverse=b)listAlways prefer over .sort() when needing a copy
reversed(it)iteratorWrap in list() if needed
enumerate(it, start=0)iterator of (i,v)
zip(*its, strict=False)iterator of tuplesstrict=True 3.10+
zip(*matrix)transpose
min/max(it, key=f)elementkey optional
sum(it, start=0)numberStart value for product: math.prod
any(it) / all(it)boolShort-circuit
map(f, it)iteratorPrefer comprehension
filter(f, it)iteratorPrefer comprehension
pow(b,e,mod)intFast modular exponentiation
divmod(a,b)(q,r)Quotient + remainder at once
abs(x)number
len(x)intO(1)O(1)
bin(n) / oct(n) / hex(n)strPrefixed (0b/0o/0x); slice [2:] to strip
int(s, base)intParse string in given base (2/8/16/...)
format(n, 'b')strNo prefix; '08b' zero-pads to width