Appendix E: Built-in functions

Python has many built-in functions. Built-in functions are automatically available for use in Python, without needing to import them. (There are other functions which are included in Python but need an import, such as the random number functions.)

This is a selective list of a few of the built-in functions that we think you might find particularly useful. You can find all the functions, plus more details on each, in the online Python documentation at https://docs.python.org/3/library/ Some functions can take extra optional parameters that we have omitted here for simplicity.

E.1. Miscellaneous functions

Signature Description

abs(x)

Returns the absolute value of x (discards the minus sign; -3 becomes 3).

min(a, b)

Returns the lowest of a and b. You can pass any number of actual parameters to this function and it will return the lowest of them all.

max(a, b)

Returns the highest of a and b. You can pass any number of actual parameters to this function and it will return the highest of them all.

range(x)

Creates a range of numbers. If you provide one actual parameter it gives the range from 0 up to x-1. If you provide two actual parameters (x and y) it goes from the x to y-1.

round(x)

Rounds the number to the nearest whole number

E.2. Type conversion functions

Signature Description

bool(x)

Converts x into a boolean (True or False)

chr(x)

Converts an integer x into a single character string with that Unicode code.

dict(x)

Converts x into a dict (a dictionary)

float(x)

Converts x into a floating point number (one that can have a decimal point)

int(x)

Converts x into an int (a whole number)

list(x)

Converts x into a list. The x item must already be list-like; if you want to make a list containing x, write [x].

set(x)

Converts x into a set (a collection where each item appears at most once).

str(x)

Converts x into its string representation.

type(x)

Returns the type of x, useful for debugging.

E.3. Console functions

Signature Description

input(prompt)

Prints the prompt then returns the next string the user inputs (once they press enter).

print(s)

Prints s to the console. You can pass multiple actual parameters to this function.

Signature Description

all(list)

Returns True if all of the values in the list are True when converted to boolean.

any(list)

Returns True if any of the values in the list are True when converted to boolean.

len(list)

Returns the number of items in the given list.

sorted(list)

Returns a sorted copy of the given list.

sum(list)

Returns the sum of all the numbers in the given list.

zip(list1, list2)

Pairs the lists together into a list of pairs. So zip([1, 2], [3, 4]) gives [(1, 3), (2, 4)]. Can take any number of lists to form larger tuples.

See the previous appendix on operators for a description of operators that work on lists, and slicing.

E.5. List methods

Lists have methods that can be called on a list object, using the syntax list_name.method_name(). We summarise some of the most important methods below:

Signature Description

list.append(item)

Adds the given single item to the end of the list.

list.clear()

Removes all items from the list.

list.copy()

Makes a copy of the list.

list.count(x)

Returns the number of times that x appears in the list.

list.extend(list2)

Adds all the items from list2 to the end of list.

list.index(x)

Returns the index of the first time that x appears in the list. Gives an error if it does not appear.

list.pop()

Removes the last item from the list (and returns it).

list.remove(x)

Removes the first appearance of x from the list. Gives an error if it does not appear.

list.reverse()

Reverses the list; the first element becomes the last and the last becomes the first and so on.

list.sort()

Sorts the list. (Unlike the sorted(list) function, this changes the list itself, rather than returning a sorted copy).

There are more list methods, which you can find in the online documentation: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists


1. https://shakespeareoxfordfellowship.org/shakespeare-by-the-numbers-what-stylometrics-can-and-cannot-tell-us