the full guide

Built-in functions reference

Built-in functions are always available in Python, with no import needed. They cover maths (abs, sum, max), type conversion (int, str, list), inspecting things (type, isinstance, dir), and working with iterables (len, sorted, enumerate, zip, any, all).

10 min Intermediate

What you need: The earlier topics of this module.

Definition

Built-in functions are always available in Python, with no import needed. They cover maths (abs, sum, max), type conversion (int, str, list), inspecting things (type, isinstance, dir), and working with iterables (len, sorted, enumerate, zip, any, all).

In simple words

They're the tools already on your desk before you open any toolbox. Knowing them saves you from writing code that Python has already written.

Why it matters

They're fast, they're clear, and everyone recognises them. They come up in nearly every coding interview too.

How it works

GroupFunctions
Mathsabs, sum, min, max, round, pow, divmod
Typesint, float, str, bool, list, tuple, set, dict
Inspecttype, isinstance, len, dir, help, id
Iterablesrange, enumerate, zip, map, filter, sorted, reversed, any, all, iter, next
Input and outputprint, input, open
Charactersord, chr, bin, hex

In code

builtins_demo.py
marks = [40, 75, 90, 62]
print(sum(marks), max(marks), min(marks), round(sum(marks) / len(marks), 1))
print(sorted(marks, reverse=True))
print(any(m < 50 for m in marks), all(m > 30 for m in marks))
print(divmod(17, 5))
print(isinstance(marks, list), type(marks).__name__)
print(max(["pen", "notebook"], key=len))
print(bin(5), hex(255), ord("A"), chr(66))

Output

267 90 40 66.8
[90, 75, 62, 40]
True True
(3, 2)
True list
notebook
0b101 0xff 65 B

Common mistakes

  • Naming a variable list, max or sum. It hides the built-in for the rest of your code.
  • Using type(x) == int where isinstance(x, int) is better, as it also accepts subclasses.

Check yourself

Question 1

1. What is any([0, 0, 1])?

Question 2

2. What is divmod(7, 2)?

Summary

  • Built-ins are always available.
  • Don't reuse their names for variables.

Finished reading?

Sources: Python docs: Built-in Functions