The functions discussed in the previous module are built-in functions. They are always available because they are part of the Python language itself.
Python also provides many additional functions that are grouped into
libraries (also called modules). Each library contains functions
that are designed for a particular purpose. For example, the
math library contains functions for performing common mathematical
calculations.
Before you can use a function from a library, you must first import the library.
import math
Once a library has been imported, its functions are accessed using dot notation:
math.function_name(arguments)
The name before the period identifies the library, while the name after the period identifies the function you wish to use.
math.sqrt() function
The sqrt() function calculates the square root of a number.
import math
x = math.sqrt(25)
print(x)
Output
5.0
math.log10() function
The log10() function calculates the common (base-10) logarithm of
a number. This function is especially useful in chemistry because quantities
such as pH and pOH are defined using base-10 logarithms.
import math
x = math.log10(1000)
print(x)
Output
3.0
Python contains many libraries besides math. Throughout this
course you will use additional libraries to create graphs, process data,
communicate with laboratory equipment, and perform scientific calculations.
The process is always the same: import the library, then call its
functions using dot notation.
Big idea: Built-in functions are always available, while library functions must first be imported. Libraries allow Python to perform tasks far beyond the capabilities of the built-in functions alone.
No programmer memorizes every function in every library. Fortunately, Python provides several ways to learn about a library and the functions it contains.
The built-in dir() function lists the names of the objects
contained in a library.
import math
print(dir(math))
This displays a long list containing the names of all the functions and
constants available in the math library, such as
sqrt, log10, sin,
cos, and pi.
The built-in help() function displays documentation about a
library or one of its functions.
import math
help(math.sqrt)
Python displays information describing what the function does, the arguments it expects, and the value that it returns.
You can also ask for help on an entire library:
help(math)
This displays documentation for all of the functions and constants in the
math library.
Tip: In VS Code, hovering your mouse over a function name or typing an opening parenthesis after the function name often displays the function's documentation and expected arguments without leaving the editor.
Python includes hundreds of libraries containing thousands of useful functions. No programmer memorizes all of them! Instead, programmers learn how to explore a library and read its documentation.
The built-in dir() function displays the names of the objects
contained in a library.
import math
print(dir(math))
This command displays a long list containing the names of the functions and
constants available in the math library. For example, you will
see names such as
sqrt,
log10,
sin,
cos,
pi, and
e.
The built-in help() function displays documentation for a
library or one of its functions.
import math
help(math.sqrt)
Python displays information describing what the function does, the arguments it expects, and the value that it returns.
You can also request help for an entire library.
help(math)
Libraries often contain useful constants in addition to functions. Constants are accessed using the same dot notation.
import math
print(math.pi)
print(math.e)
Output
3.141592653589793
2.718281828459045
When using Visual Studio Code, you often do not need to leave the editor to learn about a function. As you type a function call, or when you hover your mouse over the function name, VS Code displays information about the function, including its purpose and the arguments it expects.
For example, typing
math.log10(
will display a tooltip showing the function's expected argument and a brief description of what it does.
Remember: Experienced programmers do not memorize every library function. They know how to explore a library, read its documentation, and find the information they need.
Hydrochloric acid (HCl) is a strong acid, so we assume that it dissociates completely in water. Therefore, the hydronium ion concentration is equal to the initial acid concentration.
Calculate the pH of a 0.0135 M solution of HCl using Python. Recall that
\[ \mathrm{pH}=-\log_{10}[H^+] \]
Rather than importing the entire math library, we can import only
the function we need. The statement below imports the
log10() function and gives it the shorter name
log.
from math import log10 as log
We can now write the program.
from math import log10 as log
H_conc = 0.0135
pH = -log(H_conc)
print(f"The pH of the solution is {pH:.3f}.")
Output
The pH of the solution is 1.870.
from math import log10 as log imports only the
log10() function from the math library and gives it
the shorter name log.
H_conc stores the hydronium ion concentration in
moles per liter.
log(H_conc) calculates the common (base-10)
logarithm of the concentration.
pH = -log10[H+].
.3f displays the pH rounded to
three digits after the decimal point.
The name log is an alias for the
math.log10() function. Using an alias can make a program easier
to read, especially when a function will be used many times.
The following two statements perform exactly the same calculation.
from math import log10 as log
pH = -log(H_conc)
is equivalent to
import math
pH = -math.log10(H_conc)
Both styles are commonly used in Python. Choose the one that makes your code the clearest and easiest to read.
| Problem 1 | |
|---|---|
Before using a function from the math library, what must you do?
|
|
| Import the library or function. | |
| Convert it to a built-in function. | |
| Define the function yourself. | |
Call help() first. |
|
| Problem 2 | |
|---|---|
What does the following statement do?
|
|
Imports the entire math library. |
|
Renames the math library to log. |
|
Imports the log10() function and gives it the name log. |
|
Creates a new logarithm function named log. |
|
| Problem 3 | |
|---|---|
After executing
H_conc?
|
|
pH = math.log10(H_conc) |
|
pH = -log(H_conc) |
|
pH = log(H_conc) |
|
pH = -math.log(H_conc) |
|
| Problem 4 | |
|---|---|
| Which built-in function displays information about a library or one of its functions? | |
type() |
|
dir() |
|
print() |
|
help() |
|
import math.
from math import log10.
as keyword:
from math import log10 as log.
log(H_conc).
dir() function lists the contents of a library,
while help() displays documentation for a library or one of
its functions.
math library provides
math.pi and math.e.
Big picture: Python's built-in functions are only the beginning. By importing libraries, you gain access to thousands of specialized functions for mathematics, data analysis, graphing, scientific computing, and controlling laboratory equipment.