Python reference

Python built-in functions

Functions you can use anywhere without importing anything: print, len, range, input, sorted and more.

Built-in functions are always there: you never import them. They print and read text, convert between types, measure and sort collections, and loop with counters. A handful of them (print, len, range, input, int, str) appear in almost every Python program.

Most used

all()Returns True when every item in a list or other iterable is true, and False as soon as one item is false. An empty iterable gives True.any()Returns True when at least one item in a list or other iterable is true, and False when every item is false or the iterable is empty.chr()Turns a Unicode code point, a whole number such as 65, into the character it stands for, such as A. It is the opposite of ord().enumerate()Adds a counter to any loop, giving you each item's position and the item itself, so you never need range(len(...)) again.format()Turns a value into nicely formatted text with a format spec: decimals, thousands separators, percentages, padding, alignment or binary.input()Shows an optional prompt, waits for the user to type a line and press Enter, and returns what they typed as a string.int()Turns text like "42" or a decimal number like 3.9 into a whole number, and can read binary, hexadecimal and other bases too.len()Returns how many items something holds: characters in a string, elements in a list or tuple, keys in a dictionary or members of a set.map()Calls a function on every item of a list or other iterable and gives back the results, for example to turn a list of strings into numbers.max()Returns the largest item of a list or of several arguments, with a key function to choose what to compare and a default for empty data.min()Returns the smallest item of a list or of several arguments, with a key function to decide what smallest means and a default for empty data.open()Opens a file so you can read its text, write new text, or add lines to the end. Use it with a with block so the file closes itself.ord()Turns one character into its Unicode code point, the whole number that stands for it, such as 65 for A. chr() goes the other way.print()Writes text, numbers and any other values to the screen, with control over the separator, the line ending and where the output goes.range()Produces a sequence of whole numbers from a start to an end, with an optional step, mostly used to repeat a for loop a set number of times.round()Rounds a number to the nearest whole number or to a chosen number of decimals, with two surprises: halves go to the even side and floats are not exact.sorted()Returns a new sorted list from any collection, smallest first or reversed, with a key function to sort by length, by a field or ignoring case.str()Turns numbers, lists and any other value into readable text, so you can join it with other strings, print it or save it to a file.sum()Adds up all the numbers in a list, tuple or any other iterable, with an optional starting value, in one short and fast call.type()Tells you the type of any value, such as int, str, list or dict, which helps when a program does not behave the way you expect.zip()Pairs up items from two or more lists by position, so you can loop over them together, build a dictionary, or split pairs apart again.

All 49 built-in functions

Spotted a mistake on this page? Tell us