Introduction to Python Programming

# 2.6The math module

## 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

1.
In the above example, when evaluating math, why did the interpreter raise a NameError?
1. The math module was not available.
2. The variable pie was spelled incorrectly.
3. The math module was not imported.
2.
Which of these functions is builtin and does not need to be imported?
1. log()
2. round()
3. sqrt()
3.
Which expression results in an error?
1. math.abs(1)
2. math.log(1)
3. math.sqrt(1)

## 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

math.e

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

math.pi

$π=3.14159…π=3.14159…$ The ratio of the circumference to the diameter of a circle.

math.tau

$τ=6.28318…τ=6.28318…$ The ratio of the circumference to the radius of a circle. Tau is equal to 2π.
Table 2.3 Example constants in the math module.
Function Description Examples
Number-theoretic

math.ceil(x)

The ceiling of x: the smallest integer greater than or equal to x.

math.ceil(7.4) $→→$ 8
math.ceil(-7.4) $→→$ -7

math.floor(x)

The floor of x: the largest integer less than or equal to x.

math.floor(7.4) $→→$ 7
math.floor(-7.4) $→→$ -8

Power and logarithmic

math.log(x)

The natural logarithm of x (to base e).

math.log(math.e) $→→$ 1.0
math.log(0) $→→$ ValueError: math domain error

math.log(x, base)

The logarithm of x to the given base.

math.log(8, 2) $→→$ 3.0
math.log(10000, 10) $→→$ 4.0

math.pow(x, y)

x raised to the power y. Unlike the ** operator, math.pow() converts x and y to type float.

math.pow(3, 0) $→→$ 1.0
math.pow(3, 3) $→→$ 27.0

math.sqrt(x)

The square root of x.

math.sqrt(9) $→→$ 3.0
math.sqrt(-9) $→→$ ValueError: math domain error

Trigonometric

math.cos(x)

The cosine of x radians.

math.cos(0) $→→$ 1.0
math.cos(math.pi) $→→$ -1.0

math.sin(x)

The sine of x radians.

math.sin(0) $→→$ 0.0
math.sin(math.pi/2) $→→$ 1.0

math.tan(x)

The tangent of x radians.

math.tan(0) $→→$ 0.0
math.tan(math.pi/4) $→→$ 0.999
(Round-off error; the result should be 1.0.)

Table 2.4 Example functions in the math module.

## Concepts in Practice

### Using math functions and constants

4.
What is the value of math.tau/2?
1. approximately 2.718
2. approximately 3.142
3. approximately 6.283
5.
What is the value of math.sqrt(100)?
1. the float 10.0
2. the integer 10
3. ValueError: math domain error
6.
What is πr2 in Python syntax?
1. pi * r**2
2. math.pi * r**2
3. math.pi * r*2
7.
Which expression returns the integer 27?
1. 3 ** 3
2. 3.0 ** 3
3. math.pow(3, 3)

## Try It

In algebra, a quadratic equation is written as $ax2+bx+c=0ax2+bx+c=0$. The coefficients a, b, and c are known values. The variable x represents an unknown value. Ex: $2x2+3x−5=02x2+3x−5=0$ has the coefficients $a=2a=2$, $b=3b=3$, and $c=−5c=−5$. The quadratic formula provides a quick and easy way to solve a quadratic equation for x:

$x= −b± b 2 −4ac 2a x= −b± b 2 −4ac 2a$

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:

$x 1 = −b+ b 2 −4ac 2a x 1 = −b+ b 2 −4ac 2a$
$x 2 = −b− b 2 −4ac 2a x 2 = −b− b 2 −4ac 2a$

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
Table 2.5

## Try It

### Cylinder formulas

In geometry, the surface area and volume of a right circular cylinder can be computed as follows:

$A=2πrh+2π r 2 A=2πrh+2π r 2$

$V=π r 2 h V=π r 2 h$

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
Table 2.6

If you get an error, try to look up what that error means.

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.