Exploring the Python math Module (Overview)

In this course, you’ll learn all about Python’s math module. Mathematical calculations are an essential part of most Python development. Whether you’re working on a scientific project, a financial application, or any other type of programming endeavor, you just can’t escape the need for math.

For straightforward mathematical calculations in Python, you can use the built-in mathematical operators, such as addition (+), subtraction (-), division (/), and multiplication (*). But more advanced operations, such as exponential, logarithmic, trigonometric, or power functions, are not built in. Does that mean you need to implement all of these functions from scratch?

Fortunately, no. Python provides a module specifically designed for higher-level mathematical operations: the math module.

By the end of this course, you’ll learn:

  • What the Python math module is
  • How to use math module functions to solve real-life problems
  • What the constants of the math module are, including pi, tau, and Euler’s number
  • What the differences between built-in functions and math functions are
  • What the differences between math, cmath, and NumPy are

A background in mathematics will be helpful here, but don’t worry if math isn’t your strong suit. This course will explain the basics of everything you need to know.

For more information on concepts covered in this lesson, you can check out:


Sample Code (.zip)

4.0 KB

Course Slides (.pdf)

2.8 MB

00:00 Hi, I’m Cesar. Welcome to this Real Python video course on the math module.

00:06 What is the math module? The math module provides access to some of the mathematical functions defined by the C standard library, and much more.

00:16 The module contains functions that perform computations from number theory, combinatorics, trigonometry, geometry, and classification. Many math module functions are thin wrappers around C math library functions.

00:33 Let me show you one.

00:35 If you go to the GitHub repository for Python, go over to the CPython directory, you’ll find the mathmodule.c function. In that file, you’ll see this definition for the m_log() function, which implements the logarithm function that we’ll talk about in this course.

00:53 If you take a look at this code, you see that this is a very thin wrapper around the logarithm function in the C standard library.

01:02 But the math module functions do more than just this. There are some nice functions that were added in Python 3.8, and we’ll discuss these. So, why use the math module? Well, for one thing, it’s part of the standard library of Python, so there’s no need for a separate installation.

01:19 All you do is import the module with import math. The math module functions have been tested and optimized to give accurate results, so there’s no need to reinvent the wheel to perform basic math.

01:31 Just load the module and get going.

01:35 Here are the topics that we’ll discuss in the course. We’ll go over some of the constants in the math module. Then we’ll talk about arithmetic functions, and then power functions, the exponential functions, logarithmic functions, and other functions in the math module, and then we’ll take a look at some of the new additions in Python 3.8.

01:54 Then we’ll discuss the differences between the cmath module—which deals with complex numbers—and the math module, and NumPy and the math module. We’ll wrap things up with the summary.

02:07 Now, I’m eager to get going and I bet you are too, but one last important thing before we do: All of the code in this course was tested in Python 3.9. You should be fine with Python 3.8, and probably even as low as 3.5, but if you do want to test the new functions that were added in Python 3.8, you may want to have at least that version of Python installed.

02:30 All right, enough with the particulars. Let’s get going with this course.

Become a Member to join the conversation.