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
- abs()Returns the absolute value of a number, its distance from zero without a minus sign, useful for differences, distances and tolerances.
- 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.
- bin()Converts a whole number to a binary string that starts with 0b, such as 0b1010 for 10. Use int(text, 2) to convert back.
- bool()Tells you whether Python treats a value as true or false: empty things and zero are False, almost everything else is True.
- bytes()Creates a bytes object, an unchangeable sequence of numbers from 0 to 255, used for files, network data and encoded text.
- callable()Returns True if an object can be called with brackets, like a function, method or class, and False for plain values such as numbers.
- 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().
- dict()Builds a dictionary from keyword arguments, from a list of key-value pairs, from two lists zipped together, or as a copy of another dictionary.
- dir()Lists the names of the attributes and methods an object has, sorted alphabetically. With no argument, lists the names in the current scope.
- divmod()Divides two numbers and returns both the whole-number result and the remainder as a pair, the same as (a // b, a % b).
- enumerate()Adds a counter to any loop, giving you each item's position and the item itself, so you never need range(len(...)) again.
- eval()Evaluates a string as a Python expression and returns the result. Powerful but dangerous with untrusted text: prefer ast.literal_eval().
- filter()Goes through a list or other iterable and keeps only the items for which a test function returns true, or with None, only the truthy items.
- float()Turns text like "3.14" or a whole number into a floating-point number, the type Python uses for prices, measurements and other decimals.
- format()Turns a value into nicely formatted text with a format spec: decimals, thousands separators, percentages, padding, alignment or binary.
- frozenset()Creates a frozenset: a set that can never change after it is made, so it can be a dictionary key or an item inside another set.
- getattr()Reads an attribute of an object when the attribute name is in a string, with an optional default value if the attribute is missing.
- hasattr()Returns True if an object has an attribute or method with the given name, and False if not. The name is passed as a string.
- help()Shows the documentation of a function, class or module: its parameters and its docstring. Most useful in the interactive Python console.
- hex()Converts a whole number to a hexadecimal string that starts with 0x, such as 0xff for 255. Use int(text, 16) to convert back.
- 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.
- isinstance()Checks whether a value is of a given type, or of one of several types, and also accepts subclasses, which makes it the right way to test types.
- iter()Returns an iterator, an object that hands out the items of a list, string or file one at a time each time you call next() on it.
- 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.
- list()Builds a new list from anything you can loop over: the characters of a string, the numbers of a range, the keys of a dictionary, or another list.
- 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.
- next()Returns the next item from an iterator or generator. With a default value it returns that instead of raising StopIteration at the end.
- oct()Converts a whole number to an octal (base 8) string that starts with 0o, such as 0o755. Use int(text, 8) to convert back.
- 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.
- pow()Raises a number to a power, like the operator, and with a third argument finds the remainder of that power quickly (modular power).
- 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.
- repr()Returns a string that shows a value the way you would type it in code, with quotes and escape codes visible. Ideal for debugging.
- reversed()Gives the items of a list, string, range or dictionary in reverse order, one by one, without copying or changing the original.
- 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.
- set()Builds a set, a collection where every item appears only once: the quick way to remove duplicates and to find what two groups share.
- setattr()Sets an attribute on an object when the attribute name is in a string. It works like obj.name = value, but the name can come from data.
- 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.
- super()Gives access to the parent class's methods from inside a child class, most often to run the parent's __init__ before adding your own setup.
- tuple()Builds a tuple, an ordered sequence that cannot be changed after it is made, from a list, a string or anything else you can loop over.
- 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.