Please enable JavaScript to use CodeHS

Math Module in Python

By Ryan Hart

Python modules are very important to understand and take advantage of as you write your own programs. Modules are essentially files that contain pre-written code with items like objects, functions, and constants. Programmers can import these modules into their programs in order to use the items without having to write the code themselves. The modules often address specific topics and functionality that are commonly used in programs.


The Math module

The Python Math module introduces many common math functions and constants that would otherwise not be available in the base Python language. Let’s start by learning how to import the module, then we’ll review a handful of the module’s items.

Adding any Python module is pretty easy, and because the Math module automatically comes with the Python language, all we have to do is import it by writing:

import math
Plain text

Once imported, we can access any function or constant by using the notation math.[function or constant].

math.pi

Let’s start out with a very simple and super helpful math constant, pi. Prior to importing the Math module, in order to store the value of pi, we would have needed to know a circle’s radius and circumference to calculate it, or manually typed in its many digits ourselves.

With the Math module imported, all we need to do now is write math.pi to access the the pi constant.

Take a look at the following example to see it in use.

math.e

Euler’s number, e = 2.718…, is also a very important math constant, and appears in many different places – growth/decay rates in finance, probability, and even nuclear physics!

Similar to the example above, to access the constant from the module, you’d write math.e.

Look at the below example of a formula that models the rate at which 100mg of caffeine (about 1 cup of coffee) leaves the body after t hours. Try changing the value of t to see how that impacts the amount of caffeine still in the body.

Arithmetic functions

In addition to constants, the Math module contains many helpful arithmetic functions. Below are a few commonly used ones:

  • sqrt(number) – returns the square root of a number
  • fabs(number) – returns the absolute value of a number
  • ceil(number) – returns the smallest integer greater than or equal to a number
  • floor(number) – returns the largest integer less than or equal to a number
  • gcd(number1, number2) – returns the greatest common factor between two numbers.

Check out the example below to see these in use. Try changing the value of number, and number1 and number2 to see how the outputs change. What happens when number is negative (be sure to delete or comment out line 7 first)?

Trigonometric functions

The Math module also includes common trig related functions. Again, without the inclusion of these pre-built functions, it would be very difficult to complete many different real-life mathematical calculations since these functions appear everywhere in our lives!

  • sin(angle) – returns the sine of the angle (in radians)
  • cos(angle) – returns the cosine of the angle (in radians)
  • tan(angle) – returns the tangent of the angle (in radians)
  • degrees(angle) – converts the angle from radians to degrees
  • radians(angle) – converts the angle from degrees to radians

Take a look at the below example that explores a couple of calculations with the following right triangle:
Right Tri
Try changing the values of angle and hypotenuse and seeing how that affects the outputs!

Full documentation

Take a look at https://docs.python.org/3/library/math.html to see the full documentation on the Math module, which includes other functions like those used in exponential and logarithmic equations!

Use the space below to try some of them out yourself. Remember to import the Math module before using any of the functions. A couple of ideas to try:

  • Calculate the area and circumference of a circle with radius 12.
  • Calculate the angle (in degrees) in a right triangle when the hypotenuse is length 10 and the adjacent side is 6. Extra challenge: also calculate the length of the unknown third side and the final unknown angle (in degrees).