Site Tools


import-math

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

  • math.sqrt(x): square root
  • math.pow(x, y): x to the power y
  • math.exp(x): e to the power x
  • math.log(x): natural logarithm
  • math.log10(x): base-10 logarithm
  • math.sin(x), math.cos(x), math.tan(x): trigonometry
  • math.radians(x), math.degrees(x): angle conversion
  • math.floor(x), math.ceil(x): rounding
  • math.factorial(x): x!
  • math.gcd(a, b): greatest common divisor
import-math.md · Last modified: by 127.0.0.1