Built-In Functions and Methods#

Built-in functions are, as the name implies, already a part of the Python library. These are functions that are so commonly used that you just need to call them in order to use them yourself! We have already seen a few examples of built-in functions including print() and type(). There are some mathematical built-in functions, for example we can find the maximum or minimum of given numbers, or the absolute value of a single number. We can even round floating point numbers to a specified decimal. Additionally, there are functions that give us general information about the input, such as the length of a string or the type of the input object.

To call a built-in function, we use the function name with parentheses around the function input. For example the function name for maximum is max() and calling this function takes the format below.


max(...)

Functions take arguments as input, and if we are unsure what arguments a function accepts, there’s a built-in function for that! The help() function. This function accepts a function as input and outputs the key characterics, namely what it takes as input and what it outputs. Calling the help function on max, we see we can enter two or more arguments and we expect the largest argument to be returned. We can alternately enter an iterable argument, like a sequence, dicussed in detail in Chapter 4: Data Structures.

help(max)
Help on built-in function max in module builtins:

max(...)
    max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its biggest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the largest argument.

So we can call the max function with multiple arguments and it returns the max. Below, we call the max function with three arguments and the largest value is returned.

max(2, 4.1, -3)
4.1

Below is a short list of common built-in functions. For a more complete list see the Python documentation here: Built-in Function Documentation

Built-in Python Functions

Description

max(…)

Maximum function: Returns the maximum of the given inputs.

min(…)

Minimum function: Returns the maximum of the given inputs.

sum(…)

Sum function: Returns the sum of a given iterable.

abs(…)

Absolute value function: Returns the absolute value of the given input.

round(…)

Rounding function: Returns the rounded input.

len(…)

Length function: Returns the length of given input.

type(…)

Data Type function: Returns the datatype of the input.

print(…)

Print function: Prints the input

Libraries#

There are a lot of functions and tools that are not contained directly in Python. If we want to access more specific functions or if we want to manipulate particular objects, we may have to import a library or a module. A module is a collection of functions and code that are related in some way, while a library is a collection of modules that are helpful in performing tasks. While there is a distinction, we will use library throughout this textbook to encompass both.

For example, there are numerous mathematical functions that are not built-in to Python, but are still important and common. We can gain access to them by importing the math library. This gives us access to the exponential, logarithmic, square root, and even floor and ceiling functions, in addition to storing the number pi or computing factorials. Hence we can think of these functions as being built-in to their respective libraries.

We will see later that we can import a library, matplotlib, to assist with graphing or visualing data, or the numpy library which allows us access to different types of objects and their manipulation. Throughout this textbook, we will import libraries as needed.

Math Library#

To access the functions built in to the math library, we first import the library by running the line below.

import math

Since these functions are coming from the math library, to call them we use

math.name_of_function

For example, suppose we want to compute log(5). Using the math library, we write the following line.

math.log(5)
1.6094379124341003

To check the key aspects of the function, we can again use the help function to view the documentation regarding

math.log(...)

Note, to ensure the input to the help function is itself a function, we do not input an argument for log.

help(math.log)
Help on built-in function log in module math:

log(...)
    log(x, [base=math.e])
    Return the logarithm of x to the given base.
    
    If the base not specified, returns the natural logarithm (base e) of x.

Did the log function do as we intended? Well, based on the documentation, if we do not specifiy the base,

math.log(5)

is the natural logarithm, \(\ln(5)\). Typing \(\log(5)\) on your calculator assumes base 10, which we can mimic by specifing the argument for the base for the optional argument of the math.log function.

math.log(5, 10)
0.6989700043360187

So it is important to know the default values for these built-in functions!

A few of the more common math functions are included below; for a complete list see the documentation here Built-in Math Function Documentation.

Built-in Math Functions

Description

math.log(…)

Returns log of input. If no base is specified uses base \(e\).

math.exp(…)

Returns \(e\) to the power of the input.

math.sqrt(…)

Returns the square root of the input.

math.floor(…)

Returns the floor of the input.

math.ceil(…)

Returns the ceiling of the input.

math.factorial(…)

Returns the factorial of the input.

math.pi

Returns pi.

math.e

Returns \(e\).

Since the output of these math functions are in fact numbers, we can manipulate them however we want!

math.factorial(6)/math.floor(12.9879) + math.pi
63.1415926535898

Methods#

Methods are similar to functions in that they provide an output based on some input. However, a major difference is that methods are built on specific objects and can easily modify the original input object. The format for a method is

object.method()

where method performs some action on object. Objects can be datatypes like strings, or more complicated data structures. As we previously saw, we can define the string candy and use the .replace() method to modify the original string object.

candy = "candy is my favorite treat. my favorite candy is BUBBLE GUM."
candy.replace("BUBBLE GUM", "LICORICE")
'candy is my favorite treat. my favorite candy is LICORICE.'

And there are many methods made for string datatypes. Some common string methods are seen in the table below.

Built-in Methods on Strings

Description

string.replace(‘old’, ‘new’)

Replaces any ‘old’ elements with ‘new’ in the given string

string.upper()

Returns an uppercase version of the given string

string.lower()

Returns a lowercase version of the given string

string.strip()

Returns the given string with additional spaces at beginning or end removed

We will see methods acting on different objects throughout this book as we delve deeper into the structure of Python.

Note all of the tools used in this section are built-in to various libraries or directly within Python. If we want to repeat our own process or perform an action and we cannot find a premade function, we also have the option to create our own functions!