Introduction to the math module: pi and sqrt()

Author

Clayton Cafiero

Published

2025-06-12

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} (x non-negative)
exp(x) e^x
log(x) \ln x (x positive)
log2(x) \log_2 x (\log base two)
log10(x) \log_{10} x (\log base ten)
floor(x) \lfloor x \rfloor
ceil(x) \lceil x \rceil
sin(x) \sin x (x in radians)
cos(x) \cos x (x in radians)
degrees(x) x \times \frac{180}{\pi}
radians(x) x \times \frac{\pi}{180}

and many others. The math module also supplies values for \pi and e.

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.

More examples using the math module

Here are examples of other functions provided by the math module.

Cosine

>>> math.cos(0)       # cosine of argument in radians
1.0
math.cos(3.141592653589793)
-1.0

Convert radians to degrees

>>> math.degrees(math.pi / 2.0)
90.0
>>> math.degrees(3 * math.pi / 4.0)
135.0

Convert degrees to radians

>>> math.radians(180.0)
3.141592653589793
>>> math.radians(45.0)
0.7853981633974483

Floor and ceiling

The floor function, written \lfloor x \rfloor, returns the greatest integer that’s less than or equal to x. This is implemented with math.floor().

>>> math.floor(4.2)
4
>>> math.floor(0.0)
0
>>> math.floor(-4.2)
-5

Notice that math.floor() always returns an object of type int. Also notice the result for \lfloor -4.2 \rfloor is -5, because -5 is the greatest integer less than or equal to -4.2.

The ceiling function, written \lceil \rceil, returns the least integer that’s greater than or equal to x. This is implemented with math.ceil).

>>> math.ceil(5)
4
>>> math.ceil(0.0)
0
>>> math.ceil(-4.2)
-4

Like math.floor(), math.ceil() always returns an object of type int.

Natural logarithm (\ln) and exponential function

>>> math.log(42)                   # ln of argument
3.7376696182833684
>>> math.exp(3.7376696182833684)   # exp of argument
42.00000000000001

Logarithm base 10 and base 2

>>> math.log10(1000)  # log base 10 of argument
3.0
>>> math.log2(4096)   # log base 2 of argument
12.0
>>> math.log2(64)
6.0
>>> math.log2(2)
1.0

Fun fact: When computer scientists speak of logarithms, they almost always mean \log base 2. Can you think why this might be?

\pi and e

Here are two values provided by the math module. You should always use these in your code and never define these as constants yourself.

>>> math.e
2.718281828459045
>>> math.pi
3.141592653589793

math module documentation

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

Copyright © 2023–2025 Clayton Cafiero

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

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.↩︎

Reuse