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 |
min(a, b) |
Returns the lowest of |
max(a, b) |
Returns the highest of |
range(x) |
Creates a range of numbers. If you provide one actual parameter it gives the range from |
round(x) |
Rounds the number to the nearest whole number |
E.2. Type conversion functions
| Signature | Description |
|---|---|
bool(x) |
Converts |
chr(x) |
Converts an integer |
dict(x) |
Converts |
float(x) |
Converts |
int(x) |
Converts |
list(x) |
Converts |
set(x) |
Converts |
str(x) |
Converts |
type(x) |
Returns the type of |
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 |
E.4. List-related functions
| 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 |
sorted(list) |
Returns a sorted copy of the given |
sum(list) |
Returns the sum of all the numbers in the given |
zip(list1, list2) |
Pairs the lists together into a list of pairs. So |
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 |
list.extend(list2) |
Adds all the items from |
list.index(x) |
Returns the index of the first time that |
list.pop() |
Removes the last item from the list (and returns it). |
list.remove(x) |
Removes the first appearance of |
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 |
There are more list methods, which you can find in the online documentation: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists