Learning objectives
By the end of this section you should be able to
- Distinguish between built-in 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 built-in functions such as print()
, input()
, and float()
. A built-in function is always available and does not need to be imported. The complete list of built-in 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 built-in 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((x2-x1)**2 + (y2-y1)**2)
print("The distance is", distance)
Concepts in Practice
Built-in 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 |
---|---|---|
|
Euler's number: the base of the natural logarithm. | |
|
The ratio of the circumference to the diameter of a circle. | |
|
The ratio of the circumference to the radius of a circle. Tau is equal to 2π. |
Function | Description | Examples |
---|---|---|
Number-theoretic | ||
|
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 .
The coefficients a, b, and c are known values.
The variable x represents an unknown value.
Ex: has the coefficients , , and .
The quadratic formula provides a quick and easy way to solve a quadratic equation for x:
The plus-minus symbol indicates the equation has two solutions.
However, Python does not have a plus-minus 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.