Table of Contents

import math

import math is a Python import that provides mathematical functions and constants. Use it for trigonometry, logarithms, rounding, and accessing π and e.

Example

# Python
# description: math functions and constants
 
import math
 
# Constants
print(math.pi)     # 3.14159...
print(math.e)      # 2.71828...
 
# Trigonometry
angle = math.radians(45)
print(math.sin(angle))  # 0.707...
print(math.cos(angle))  # 0.707...
 
# Logarithms and exponentials
print(math.log(100))      # natural log
print(math.log10(100))    # base 10 log
print(math.exp(1))        # e^1
 
# Powers and roots
print(math.sqrt(16))      # 4.0
print(math.pow(2, 8))     # 256.0
 
# Rounding
print(math.floor(3.7))    # 3
print(math.ceil(3.2))     # 4
 
# Hyperbolic functions
print(math.sinh(1))
 
# Factorial and GCD
print(math.factorial(5))  # 120
print(math.gcd(48, 18))   # 6

Common functions