5.2. The math module

The math module contains the kinds of mathematical functions you would typically find on your calculator. As we noted above, when we import math, we create a reference to a module object that contains these elements.

Here are some items from the math module in action.

 
1
import math
2
3
print(str(math.sqrt(2.0)))
4
print(str(math.sin(math.radians(90.0))))   # sin of 90 degrees
5

(chmodule_02)

Mathematical functions do not need to be constructed. They simply perform a task. They are all housed together in a module called math. Once we have imported the math module, anything defined there can be used in our program. Notice that we always use the name of the module followed by a dot followed by the specific item from the module (math.sqrt). You can think of this as lastname.firstname where the lastname is the module family and the firstname is the individual entry in the module.

Check your understanding

modules-3-1: Which statement allows you to use the math module in your program?






You have attempted 1 of 3 activities on this page
Next Section - 5.3. The random module