Python has many built-in functions for common operations, but Python cannot anticipate everything that people might want to do and build it in. Instead, Python uses modules to extend its capabilities. You can think of a module as a small library of functions that serve some purpose.
One important module is the math module. It is a library providing many familiar mathematical functions such as square root. Because modules are not built-in, you have to import a module before you can use it. The following statement creates a module objecct named math:
import math
The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.
Probably the most-used function from the math module is sqrt, which stands for “square root”.
Here is a program that uses the math module’s log10 function (logarithm base 10) to calculate the signal-to-noise ratio in decibels. This is a useful calculation for audio engineers; the bigger this number is, the less unwanted noise you have in your audio.
Subsection6.4.1Trigonometric Functions
The math module also provides functions to calculate the trigonometric functions such as sine, cosine, and tangent. If you have never studied these functions in your math classes, feel free to skip this section. If these functions come up in an assignment, ask your instructor for resources to learn about them.
If you’re still here, then you need to know something very important about how the trigonometry functions work in the math module. You may know that the sine of 45° is one-half the square root of 2, which is about 0.707. If you try doing the following in Python, you are in for a surprise:
The Python trigonometric functions do not want their arguments in degrees—they want their arguments in radians, where \(\pi\) (pi) radians equals 180° To convert from degrees to radians, divide by 180 and multiply by \(\pi\text{.}\) Luckily, the math module also provides you with a variable named pi, which is an approximation to \(\pi\) to about 15 digits. This program works a lot better:
By the way: you can also use the math.radians function to convert degrees to radians; then you don’t have to remember the conversion formula.
You can check the previous result by comparing it to the square root of two divided by two. The results are close enough for anything you’ll need to do in most beginning programming courses.
Subsection6.4.2Exercises
Checkpoint6.4.1.
Q-5: Which statement allows you to use the math module in Python?
import math
Correct! import math allows you to use the math module by creating the "math" module object.
include math
Incorrect! Some languages have an include statement that works similarly to import, but it’s not what we’re using. Try again.
add math
Incorrect! There is no add statement in Python; that will not import the math module. Try again.
None. You can always use the math module.
Incorrect! Something needs to be done to allow the math module to be used. Try again.
Checkpoint6.4.2.
Q-6: To access a function in a module, we must use…
log
Incorrect! log is a function within the math module. Try again.
quotation marks or single quotes
Incorrect! You need these to create a string. Try again.
dot notation
Correct! Dot notation allows us to access a function in a module.
function notation
Incorrect! Function notation is the way a function is written. Try again.
Checkpoint6.4.3.
Q-7: When we use the import math statement, a __________ called math is created.