Learning objectives
By the end of this section you should be able to
 Distinguish between builtin functions and math functions.
 Use functions and constants defined in the math module.
Importing modules
Python comes with an extensive standard library of modules. A module is previously written code that can be imported in a program. The import statement defines a variable for accessing code in a module. Import statements often appear at the beginning of a program.
The standard library also defines builtin functions such as print()
, input()
, and float()
. A builtin function is always available and does not need to be imported. The complete list of builtin functions is available in Python's official documentation.
A commonly used module in the standard library is the math module. This module defines functions such as sqrt()
(square root). To call sqrt()
, a program must import math
and use the resulting math
variable followed by a dot. Ex: math.sqrt(25)
evaluates to 5.0
.
The following program imports and uses the math module, and uses builtin functions for input and output.
Example 2.1
Calculating the distance between two points
import math
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
distance = math.sqrt((x2x1)**2 + (y2y1)**2)
print("The distance is", distance)
Concepts in Practice
Builtin functions and math module
Mathematical functions
Commonly used math functions and constants are shown below. The complete math module listing is available in Python's official documentation.
Constant  Value  Description 


$e=2.71828\dots $  Euler's number: the base of the natural logarithm. 

$\pi =3.14159\dots $  The ratio of the circumference to the diameter of a circle. 

$\tau =6.28318\dots $  The ratio of the circumference to the radius of a circle. Tau is equal to 2π. 
Function  Description  Examples 

Numbertheoretic  

The ceiling of 


The floor of 

Power and logarithmic  

The natural logarithm of 


The logarithm of 





The square root of 

Trigonometric  

The cosine of 


The sine of 


The tangent of 

Concepts in Practice
Using math functions and constants
Try It
Quadratic formula
In algebra, a quadratic equation is written as $a{x}^{2}+bx+c=0$.
The coefficients a, b, and c are known values.
The variable x represents an unknown value.
Ex: $2{x}^{2}+3x5=0$ has the coefficients $a=\text{2}$, $b=\text{3}$, and $c=\text{5}$.
The quadratic formula provides a quick and easy way to solve a quadratic equation for x:
The plusminus symbol indicates the equation has two solutions.
However, Python does not have a plusminus operator.
To use this formula in Python, the formula must be separated:
Write the code for the quadratic formula in the program below.
Test your program using the following values for a, b, and c:
Provided input  Expected output  

a 
b 
c 
x1 
x2 
1 
0 
4 
2.0 
2.0 
1 
2 
3 
1.0 
3.0 
2 
1 
1 
0.5 
1.0 
0 
1 
1 
division by zero 

1 
0 
1 
math domain error 
Try It
Cylinder formulas
In geometry, the surface area and volume of a right circular cylinder can be computed as follows:
Write the code for these two formulas in the program below. Hint: Your solution should use both math.pi
and math.tau
. Test your program using the following values for r and h:
Provided input  Expected output  

r 
h 
area 
volume 
0 
0 
0.0 
0.0 
1 
1 
12.57 
3.14 
1 
2 
18.85 
6.28 
2.5 
4.8 
114.67 
94.25 
3.1 
7.0 
196.73 
211.33 
If you get an error, try to look up what that error means.