Math Functions
Python has a math module that provides most of the familiar mathematical functions. A module is a file that contains a collection of related functions.
Before we can use the module, we have to import it:
This statement creates a module object named. 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 :
The expression math.pi
gets the variable pi
from the math module. The value of this variable is an approximation of , accurate to about 15 digits.
Last updated