Post

Python Built-in Functions: The Complete Reference Guide

Every Python built-in function explained with runnable examples, gotchas, and when to use each — abs, zip, map, enumerate, sorted, and 50+ more.

Python Built-in Functions: The Complete Reference Guide

Python ships with roughly seventy names you can call without ever writing an import line. They live in the builtins module, which the interpreter injects into every module’s namespace automatically — that’s why len([1, 2, 3]) just works, everywhere, with no setup. This guide walks through all 54 of the most commonly-used built-in functions (constructors like dict(), list(), and set() included, since in CPython they behave exactly like functions even though they’re technically type objects), with runnable examples, real output, and the gotcha that trips people up in practice.

If you ever want to see the full list yourself:

1
2
import builtins
print(dir(builtins))

And if you want to confirm a name really is a built-in rather than something defined in your own file, check vars(builtins) or simply 'sorted' in dir(builtins).

Numeric & math

divmod()

Returns (quotient, remainder) in one call — handy for anything involving pagination, clock arithmetic, or unit conversion.

1
2
q, r = divmod(47, 5)
print(q, r)
1
9 2

Gotcha: for negative numbers, Python floors the division, so divmod(-7, 2) gives (-4, 1), not (-3, -1).

pow()

Computes base ** exp, and with a third argument does fast modular exponentiation — critical for cryptography.

1
2
print(pow(2, 10))
print(pow(4, 13, 497))
1
2
1024
445

Use pow(base, exp, mod) instead of (base ** exp) % mod for large exponents — it’s vastly faster and avoids building a huge intermediate integer.

round()

Rounds to the nearest value, using banker’s rounding (round-half-to-even) for ties.

1
print(round(2.5), round(3.5), round(3.14159, 2))
1
2 4 3.14

Gotcha: round(2.5) is 2, not 3 — this surprises almost everyone the first time.

hex()

Converts an integer to its base-16 string representation, prefixed with 0x.

1
print(hex(255))
1
0xff

Use when formatting memory addresses, color codes, or debugging binary data.

float()

Converts a value to a floating-point number, or parses one from a string.

1
print(float("3.14"), float(10))
1
3.14 10.0

Gotcha: float("nan") and float("inf") are valid — always sanitize user-facing numeric input.

int()

Converts to an integer, truncating floats toward zero, and parses strings in any base.

1
print(int(3.9), int("101", 2))
1
3 5

Use int(x, base) for parsing hex/binary/octal strings without manual arithmetic.

Iterables & sequences

enumerate()

Pairs each item in an iterable with its index, avoiding manual counters.

1
2
for i, name in enumerate(["a", "b", "c"], start=1):
    print(i, name)
1
2
3
1 a
2 b
3 c

Use when you need the index and the value together in a loop — cleaner than range(len(x)).

zip()

Combines multiple iterables element-wise into tuples, stopping at the shortest one.

1
2
3
names = ["Alice", "Bob"]
ages = [30, 25]
print(list(zip(names, ages)))
1
[('Alice', 30), ('Bob', 25)]

Gotcha: mismatched lengths silently truncate — use itertools.zip_longest if that’s not what you want.

map()

Applies a function to every item of an iterable, returning a lazy iterator.

1
print(list(map(str.upper, ["a", "b", "c"])))
1
['A', 'B', 'C']

Use when a list comprehension would just be [f(x) for x in xs] — often map(f, xs) reads cleaner for simple transforms.

filter()

Keeps only the items for which a function returns truthy.

1
print(list(filter(lambda x: x % 2 == 0, range(10))))
1
[0, 2, 4, 6, 8]

Passing None as the function filters out falsy values directly: filter(None, [0, 1, "", "x"]).

sorted()

Returns a new sorted list from any iterable, leaving the original untouched.

1
print(sorted(["banana", "apple", "cherry"], key=len))
1
['apple', 'banana', 'cherry']

Use key= instead of a custom comparator — it’s faster and idiomatic. Add reverse=True for descending order.

reversed()

Returns a reverse iterator over a sequence without copying it.

1
print(list(reversed([1, 2, 3])))
1
[3, 2, 1]

Works on anything implementing __reversed__ or supporting indexing — not on arbitrary iterators.

range()

Generates an immutable, memory-efficient sequence of integers, evaluated lazily.

1
print(list(range(2, 10, 2)))
1
[2, 4, 6, 8]

range objects don’t store all values in memory — range(10**18) is instant to create.

len()

Returns the number of items in a container by calling its __len__.

1
print(len("hello"), len([1, 2, 3]))
1
5 3

Gotcha: raises TypeError on objects without __len__, including plain iterators and generators.

sum()

Adds up the items of an iterable, with an optional start value.

1
print(sum([1, 2, 3], 100))
1
106

Avoid sum() for string concatenation — use "".join() instead; it’s far faster.

min() and max()

Return the smallest/largest item, optionally by a custom key.

1
2
words = ["kiwi", "banana", "fig"]
print(min(words, key=len), max(words, key=len))
1
fig banana

Both accept multiple positional arguments too: max(3, 7, 2).

iter()

Turns an iterable into an iterator, or repeatedly calls a function until a sentinel value.

1
2
it = iter([1, 2, 3])
print(next(it), next(it))
1
1 2

The two-argument form iter(callable, sentinel) is a clean way to read a file or stream until a marker.

next()

Advances an iterator and returns the next item, with an optional default.

1
2
it = iter([])
print(next(it, "done"))
1
done

Without a default, next() raises StopIteration on exhaustion — always provide one when unsure.

slice()

Creates a reusable slice object, useful when the same slicing logic is applied repeatedly.

1
2
s = slice(1, 5, 2)
print([0, 1, 2, 3, 4, 5, 6][s])
1
[1, 3]

Use named slice objects to document intent when indexing logic gets complex (e.g., parsing fixed-width fields).

Type constructors

str()

Converts any object to its human-readable string form via __str__.

1
print(str(3.14), str([1, 2]))
1
3.14 [1, 2]

Different from repr()str() favors readability, repr() favors unambiguous reconstruction.

list()

Builds a list from any iterable, or an empty list with no arguments.

1
print(list("abc"), list(range(3)))
1
['a', 'b', 'c'] [0, 1, 2]

Use list(x) to materialize a generator or map/filter result you need to iterate more than once.

dict()

Constructs a dictionary from keyword arguments, a mapping, or an iterable of key-value pairs.

1
print(dict(a=1, b=2), dict([("x", 1), ("y", 2)]))
1
{'a': 1, 'b': 2} {'x': 1, 'y': 2}

Since 3.7, insertion order is guaranteed — dicts are safe to treat as ordered.

set()

Builds an unordered collection of unique, hashable items.

1
print(set([1, 2, 2, 3]))
1
{1, 2, 3}

Use for O(1) membership tests and deduplication — x in my_set beats x in my_list for large collections.

frozenset()

An immutable, hashable version of set — usable as a dict key or set member.

1
2
fs = frozenset([1, 2, 3])
print(hash(fs) is not None)
1
True

Use when you need a set-like value that itself needs to be hashable, e.g. as a cache key.

tuple()

Builds an immutable ordered sequence from an iterable.

1
print(tuple([1, 2, 3]))
1
(1, 2, 3)

Tuples are hashable when their contents are, making them natural dict keys and set members.

memoryview()

Exposes a buffer’s memory without copying it — critical for performance with large binary data.

1
2
3
4
data = bytearray(b"hello")
mv = memoryview(data)
mv[0] = ord("H")
print(data)
1
bytearray(b'Hello')

Use when slicing large bytes/bytearray objects repeatedly — memoryview avoids the copy each slice would otherwise make.

object()

Creates a bare instance of the base class every Python object inherits from.

1
2
sentinel = object()
print(sentinel is object())
1
False

A classic use is a unique “sentinel” value guaranteed not to equal anything else, useful as a default marker instead of None.

Introspection & attributes

type()

Returns an object’s type, or — with three arguments — dynamically creates a new class.

1
2
3
print(type(42), type("hi"))
Dynamic = type("Dynamic", (object,), {"x": 1})
print(Dynamic().x)
1
2
<class 'int'> <class 'str'>
1

The three-argument form is how metaclasses and ORMs generate classes at runtime.

isinstance()

Checks whether an object is an instance of a class or any of a tuple of classes.

1
print(isinstance(5, (int, float)))
1
True

Prefer isinstance() over type(x) == SomeClass — it respects inheritance and duck typing.

issubclass()

Checks a class relationship rather than an instance relationship.

1
print(issubclass(bool, int))
1
True

Gotcha: in Python, bool is a subclass of int — that’s why True == 1 evaluates to True.

dir()

Lists the names in the current scope, or the attributes/methods of an object.

1
print([n for n in dir([]) if not n.startswith("_")][:5])
1
['append', 'clear', 'copy', 'count', 'extend']

Great for interactive exploration in a REPL, but not a reliable API contract — use the inspect module for tooling.

vars()

Returns an object’s __dict__, or acts like locals() with no arguments.

1
2
3
4
class P:
    def __init__(self):
        self.x = 1
print(vars(P()))
1
{'x': 1}

Fails on objects using __slots__, since they have no __dict__.

hasattr()

Checks whether an attribute exists on an object without raising.

1
print(hasattr("hi", "upper"), hasattr("hi", "nope"))
1
True False

Internally it just calls getattr() and catches AttributeError — prefer EAFP (try/except) in hot paths for performance.

getattr()

Fetches an attribute by name, with an optional default to avoid an exception.

1
print(getattr("hi", "upper")(), getattr("hi", "missing", "N/A"))
1
HI N/A

Use when the attribute name is only known at runtime — e.g. dynamic dispatch based on a config string.

setattr()

Sets an attribute by name, the dynamic counterpart to getattr().

1
2
3
4
class C: pass
obj = C()
setattr(obj, "value", 42)
print(obj.value)
1
42

Use for building objects from dynamic data (e.g. deserializing a dict into an instance).

id()

Returns an object’s unique identity — in CPython, its memory address.

1
2
3
a = [1, 2]
b = a
print(id(a) == id(b))
1
True

Use id() (or the is operator, which uses it) to check object identity, never for equality — that’s what == is for.

hash()

Returns an integer hash for hashable objects — the basis for dict and set lookups.

1
print(hash("hello") == hash("hello"))
1
True

Only hashable (usually immutable) objects support hash() — lists and dicts raise TypeError.

repr()

Returns an unambiguous, developer-facing string representation of an object.

1
print(repr("hi\nthere"))
1
'hi\nthere'

Good __repr__ implementations should, ideally, look like valid Python that recreates the object.

oct()

Converts an integer to its octal string representation, prefixed with 0o.

1
print(oct(64))
1
0o100

Common in file-permission handling, e.g. oct(os.stat("f").st_mode).

ord()

Converts a single character into its Unicode code point.

1
print(ord("A"), ord(""))
1
65 8364

Gotcha: raises TypeError if given a string of length other than one.

Execution & I/O

print()

Writes text to standard output (or any file-like object via file=).

1
print("a", "b", sep="-", end="!\n")
1
a-b!

Use sep, end, and file to control formatting instead of building strings manually before printing.

input()

Reads a line of text from standard input, optionally showing a prompt.

1
name = input("Name: ")
1
Name: (waits for user input, returns the typed string)

Always returns a str — cast explicitly with int()/float() if you need a number.

open()

Opens a file and returns a file object; almost always used with a with block.

1
2
with open("example.txt", "w") as f:
    f.write("hello")
1
(writes "hello" to example.txt)

Always use with open(...) as f: — it guarantees the file is closed even if an exception occurs.

format()

Formats a single value according to a format spec — the mechanism behind f-strings.

1
print(format(3.14159, ".2f"), format(255, "#x"))
1
3.14 0xff

For anything beyond a single value, an f-string (f"{x:.2f}") is usually clearer than calling format() directly.

eval()

Evaluates a string as a Python expression and returns its result.

1
print(eval("2 + 3 * 4"))
1
14

Gotcha: never call eval() on untrusted input — it executes arbitrary code with full interpreter privileges.

exec()

Executes a string as Python statements — a superset of eval(), with no return value.

1
exec("x = 5\nprint(x * 2)")
1
10

Same security warning as eval(), doubly so — exec() can define functions, classes, and imports.

help()

Launches Python’s interactive help system for a module, function, or object.

1
help(str.strip)
1
(prints the docstring for str.strip in an interactive pager)

Best used in a REPL session; in scripts, prefer reading documentation directly.

globals()

Returns a mutable dictionary of the current module’s global namespace.

1
2
x = 1
print("x" in globals())
1
True

Mutating the dict returned by globals() actually changes global variables — powerful and dangerous.

locals()

Returns a snapshot dictionary of the current local namespace.

1
2
3
4
def f():
    y = 10
    print(locals())
f()
1
{'y': 10}

Gotcha: inside a function, mutating the dict from locals() does not reliably update local variables — CPython treats it as a snapshot.

import()

The low-level function that the import statement calls under the hood.

1
2
math = __import__("math")
print(math.sqrt(16))
1
4.0

Use importlib.import_module() instead in real code — __import__() exists mainly for the interpreter’s own use.

OOP helpers

property()

Turns a method into a managed attribute, enabling computed values with attribute-style access.

1
2
3
4
5
6
7
8
class Circle:
    def __init__(self, r):
        self.r = r
    @property
    def area(self):
        return 3.14159 * self.r ** 2

print(Circle(2).area)
1
12.56636

Use property to add validation or computed values to an attribute without breaking existing obj.attr call sites.

staticmethod()

Marks a method that doesn’t receive self or cls — effectively a plain function namespaced in a class.

1
2
3
4
5
6
class Math:
    @staticmethod
    def add(a, b):
        return a + b

print(Math.add(2, 3))
1
5

Use when a method logically belongs to a class but never touches instance or class state.

super()

Returns a proxy object for calling methods on a parent or sibling class — essential for cooperative multiple inheritance.

1
2
3
4
5
6
7
8
9
class Animal:
    def speak(self):
        return "..."

class Dog(Animal):
    def speak(self):
        return super().speak() + " Woof!"

print(Dog().speak())
1
... Woof!

Always prefer super() over hardcoding the parent class name — it correctly follows the MRO in multiple-inheritance hierarchies.

Summary table

CategoryFunctions
Numeric & mathdivmod, pow, round, hex, oct, float, int
Iterables & sequencesenumerate, zip, map, filter, sorted, reversed, range, len, sum, min, max, iter, next, slice
Type constructorsstr, list, dict, set, frozenset, tuple, memoryview, object
Introspection & attributestype, isinstance, issubclass, dir, vars, hasattr, getattr, setattr, id, hash, repr, oct, ord
Execution & I/Oprint, input, open, format, eval, exec, help, globals, locals, __import__
OOP helpersproperty, staticmethod, super

Where to go next

Once you’re comfortable with the built-in toolkit, the next step is usually applying it inside real data and AI pipelines. If you’re building anything that touches large models locally, see Quantization for LLMs: Run Big Models on Small Hardware — many of the functions above (bytes, memoryview, buffer handling) show up constantly when working with tensor data. If you’re evaluating retrieval pipelines, Building a RAG Evaluation Pipeline with Python leans heavily on zip(), enumerate(), and sorted() for scoring and ranking results. And if async code is next on your list, Python’s async ecosystem builds directly on the iterator protocol these built-ins expose — iter() and next() are the same mechanism async for uses under the hood.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.