Lesson 99 +10 XP

Cheatsheet: Built-in Functions

Cheatsheet: Built-in Functions

The functions Python always has available, no import needed.

Everyday essentials

FunctionJob
print()Print to the screen
len()Length of a collection
type()Type of a value
int(), float(), str()Casting
input()Read user input
range()Generate number sequences
enumerate()Index and item pairs
zip()Pair up multiple sequences
min(), max()Smallest / largest
sum()Add up a sequence
abs()Absolute value
round()Round a number
sorted()Return a sorted copy
reversed()Return a reversed iterator

Collection constructors

list(), tuple(), set(), dict(), frozenset().

Check types

isinstance(x, int) checks a value's type.

Example

nums = [3, 1, 2]
print(sum(nums))          # 6
print(sorted(nums))       # [1, 2, 3]
print(list(enumerate(nums)))  # [(0, 3), (1, 1), (2, 2)]

TL;DR

  • Built-ins need no import.
  • len, print, type, and casting are the core set.
  • sorted() copies; sort() sorts in place.
  • min, max, sum, abs, round handle numbers.