Introduction to the math module: pi and sqrt()

Published

2023-07-31

The math module

We’ve seen that Python provides us with quite a few conveniences “right out of the box.” Among these are built-in functions, that we as programmers can use—and reuse—in our code with little effort. For example, print(), and there are many others.

We’ve also learned about how to define constants in Python. For example, Newton’s gravitational constant:

G = 6.67 * 10 ** -11  # N m^2 / kg^2

Here we’ll learn a little about Python’s math module. The Python math module is a collection of constants and functions that you can use in your own programs—and these are very, very convenient. For example, why write your own function to find the principal square root of a number when Python can do it for you?1

Unlike built-in functions, in order to use functions (and constants) provided by the Python math module, we must first import the module (or portions thereof).2 You can think of this as importing features you need into your program.

Python’s math module is rich with features. It provides many functions including

function calculation
sqrt(x) \sqrt{x}
exp(x) e^x
log(x) \ln x
log2(x) \log_2 x
sin(x) \sin x
cos(x) \cos x

and many others.

Using the math module

To import a module, we use the import keyword. Imports should appear in your code immediately after the starting docstring, and you only need to import a module once. If the import is successful, then the imported module becomes available.

>>> import math

Now what? Say we’d like to use the sqrt() function provided by the math module. How do we access it?

If we want to access functions (or constants) within the math module we use the . operator.

>>> import math
>>> math.sqrt(25)
5.0

Let’s unpack this. Within the math module, there’s a function named sqrt(). Writing math.sqrt() is accessing the sqrt() function within the math module. This uses what is called dot notation in Python (and many other languages use this as well).

Let’s try another function in the math module, sin(), which calculates the sine of an angle. You may remember from pre-calculus or trigonometry course that \sin 0 = 0, \sin \frac{\pi}{2} = 1, \sin \pi = 0, \sin \frac{3\pi}{2} = -1, and \sin 2\pi = 0. Let’s try this out.

>>> import math
>>> PI = 3.14159
>>> math.sin(0)
0.0

So far, so good.

>>> math.sin(PI / 2)
0.999999998926914

That’s close, but not quite right. What went wrong? (Hint: Representation error is not the problem.) Our approximation of \pi (defined as PI = 3.14159, above) isn’t of sufficient precision. Fortunately, the math module includes high-precision constants for \pi and e.

>>> math.sin(math.pi / 2)
1.0

Much better.

It is left to the reader to test other arguments to the sin() function.

math module documentation

As noted, the math module has many ready-made functions you can use. For more information, see the math module documentation.

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).

Footnotes

  1. The only reasonable answer to this question is “for pedagogical purposes”, and in fact, later on, we’ll do just that—write our own function to find the principal square root of a number. But let’s set that aside for now.↩︎

  2. We’re going to ignore the possibility of importing portions of a module for now.↩︎