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 math
r = 3
circumference = 2 * r * math.pi
  • Import only the functions and variables you will need. Then, no need to use the qualifier “math.”
from math import pi
r = 3
circumference = 2 * r * pi

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
from math import *