import math
= 3
r = 2 * r * math.pi circumference
Math module
For other mathematical operations, you can import Python’s math module. Two ways to do that:
- Import the whole module and use as needed: need to use the module name followed by a dot (.) before using a function or variable from the module.
- Import only the functions and variables you will need. Then, no need to use the qualifier “math.”
from math import pi
= 3
r = 2 * r * pi circumference
Some functions in the math module (documentation here)
Method | Explanation |
---|---|
math.cos(x) | Returns cosine of x (rads) |
math.sin(x) | Returns sine of x (rads) |
math.tan(x) | Returns tangent of x (rads) |
math.degrees(x) | Converts x radians to degrees |
math.radians(x) | Converts x degrees to rads |
math.sqrt(x) | Returns square root of x |
- You can also try to import all of the modules resources by using
from math import *
- However, this is not recommended due to “namespace pollution” (having lots of names referenced that are actually not used)