tgoop.com/DataScienceQ/1252
Create:
Last Update:
Last Update:
In Python, the math module provides a wide range of mathematical functions and constants for precise computations. It supports operations like trigonometry, logarithms, powers, and more.
import math
# Constants
print(math.pi) # Output: 3.141592653589793
print(math.e) # Output: 2.718281828459045
# Basic arithmetic
print(math.sqrt(16)) # Output: 4.0
print(math.pow(2, 3)) # Output: 8.0
print(math.factorial(5)) # Output: 120
# Trigonometric functions (in radians)
print(math.sin(math.pi / 2)) # Output: 1.0
print(math.cos(0)) # Output: 1.0
print(math.tan(math.pi / 4)) # Output: 0.9999999999999999
# Logarithmic functions
print(math.log(10)) # Output: 2.302585092994046
print(math.log10(100)) # Output: 2.0
print(math.log2(8)) # Output: 3.0
# Rounding functions
print(math.ceil(4.2)) # Output: 5
print(math.floor(4.8)) # Output: 4
print(math.trunc(4.9)) # Output: 4
print(round(4.5)) # Output: 4 (rounding to nearest even)
# Special functions
print(math.isfinite(10)) # Output: True
print(math.isinf(float('inf'))) # Output: True
print(math.isnan(0.0 / 0.0)) # Output: True
# Hyperbolic functions
print(math.sinh(1)) # Output: 1.1752011936438014
print(math.cosh(1)) # Output: 1.5430806348152417
# Copysign and fmod
print(math.copysign(-3, 1)) # Output: -3.0
print(math.fmod(10, 3)) # Output: 1.0
# Gamma function
print(math.gamma(4)) # Output: 6.0 (same as factorial(3))
By: @DataScienceQ
