Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(\newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\Q}{\mathbb Q} \newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 4.4 Math functions
Python has a
math module that provides most of the familiar mathematical functions. Before we can use the module, we have to import it:
This statement creates a
module object named math. If you print the module object, you get some information about it:
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 .
The first example computes the logarithm base 10 of the signal-to-noise ratio. The math module also provides a function called
log that computes logarithms base e.
The second example finds the sine of
radians. The name of the variable is a hint that
sin and the other trigonometric functions (
cos,
tan, etc.) take arguments in radians. To convert from degrees to radians, divide by 360 and multiply by 2pi:
The expression
math.pi gets the variable
pi from the math module. The value of this variable is an approximation of pi, accurate to about 15 digits.
If you know your trigonometry, you can check the previous result by comparing it to the square root of two divided by two:
Checkpoint 4.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! include works similarly to import, but itβs not what weβre using. Try again.
add math
Incorrect! add 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.
Checkpoint 4.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.
Checkpoint 4.4.3 .
You have attempted
of
activities on this page.