Skip to content

Built-in functions

There are a set of built-in functions that are a part of any Python installation, i.e., they can be used and recognised in any Python code. The built-in functions do not have to be imported.

We will describe a few common functions here, but there are many more, that are described at https://docs.python.org/3/library/functions.html.

abs

The abs() function will return the absolute value of a real or complex number, e.g.:

print(abs(-2.3))
2.3
print(abs(4.5 + 1.2j))
4.65725240888

dict

The dict() function is used to create a new dictionary, e.g.:

d = dict(value1=1, value2=2)
print(d)
{'value1': 1, 'value2': 2}

enumerate

The enumerate() function is used when iterating over an object such as a list or array. For each iteration, it returns a tuple containing an index, and the value of the element at that index, e.g.:

x = [7.4, 1.0, 5.6, 13.2]
y = ["a", "b", "c", "d"]

for i, x_val in enumerate(x):
    print(x_val, y[i])

float

The float() function creates a floating point number object from a number or a string, e.g.:

num = float("4.5")  # convert a string containing 4.5 into a float representing 4.5

int

The int() function creates an integer object from a number or a string, e.g.:

num = int("3")  # convert a string containing 3 into an int representing 3

input

The input() function allows you to get user input from the keyboard, e.g.:

name = input("What is your name? ")

isinstance

The isinstance() function allows you to check whether a variable is of a particular class (or subclass of a given class), e.g.,:

x = 2
# check x is an integer
test = isinstance(x, int)
print(test)
True

len

The len() function returns the length of an object, such as a list, if this is meaningful for that object, e.g.,:

x = [4, 9, 2, 6]
len(x)
4

list

The list() function (actually a "mutable sequence" rather than a function) is used to create a new list, e.g.,:

l = list((1, 2, 3))
print(l)
[1, 2, 3]

max

The max() function returns the largest value in a list of values, or the largest value from a set of arguments, e.g.:

print(max([5, 7, 2, 1, 7]))
7
print(max(5, 7, 2, 1, 7))
7

min

The min() function returns the smallest value in a list of values, or the smallest value from a set of arguments, e.g.:

print(min([5, 7, 2, 1, 7]))
1
print(min(5, 7, 2, 1, 7))
1

print

The print() function allows you to print text to the screen or a file, e.g.:

print("Hello")
Hello

Note

print just prints text to the screen (or to a file if specified). It does not return that text to a variable.

range

The range() function returns a sequence of values that can be iterated through. It can be used to create a list of integer values starting at zero and going up to one less than the supplied value, e.g.,:

idxs = list(range(10))
print(idx)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

and is useful for iterating over a range of indices in a for-loop, e.g.:

x = [6.6, 7.4, 3.4, 2.1]

for i in range(len(x)):
    print(x[i])

str

The str() function returns a string representation of an object, e.g.:

numstr = str(3)  # get a string version of the integer number object 3

sum

The sum() function will sum the items within a list (or other iterable), e.g.:

sum([2, 3, 4, 5, 6])
20

type

The type() function return the type of a variable.

type("Hello")
str

Keywords

Along with the built-in functions, there are a set of reserved keywords in Python that have a specific meaning and cannot be used for variable or function names.

A list of the current keywords can be found by importing the [keyword] module and printing the kwlist value, e.g.:

import keyword
for i in range(len(keyword.kwlist) // 5): 
    print("\t".join(keyword.kwlist[(i * 5):(i * 5 + 5)]))
False   None    True    and as
assert  async   await   break   class
continue    def del elif    else
except  finally for from    global
if  import  in  is  lambda
nonlocal    not or  pass    raise
return  try while   with    yield

Many of these keywords will be defined in other tutorials, but a good resource defining them all can be found here.