In this guide, you'll explore Python's math module, which performs mathematical operations. Learn its key functions and examples for practical applications.
The math
module in Python provides access to mathematical functions defined by the C standard. It includes functions for basic arithmetic, trigonometry, logarithms, and more, as well as constants like pi and e.
Table of Contents
- Introduction
- Constants
- Number-theoretic and Representation Functions
ceil
copysign
fabs
factorial
floor
fmod
frexp
fsum
gcd
isfinite
isinf
isnan
ldexp
modf
trunc
- Power and Logarithmic Functions
exp
expm1
log
log1p
log2
log10
pow
sqrt
- Trigonometric Functions
acos
asin
atan
atan2
cos
sin
tan
- Angular Conversion
degrees
radians
- Hyperbolic Functions
acosh
asinh
atanh
cosh
sinh
tanh
- Special Functions
erf
erfc
gamma
lgamma
- Examples
- Real-World Use Case
- Conclusion
- References
Introduction
The math
module provides a comprehensive set of mathematical functions and constants. These functions are optimized for performance and provide a reliable foundation for numerical calculations.
Constants
pi
The mathematical constant π.
import math
print(math.pi) # 3.141592653589793
e
The mathematical constant e.
print(math.e) # 2.718281828459045
tau
The mathematical constant Ï„, which is 2Ï€.
print(math.tau) # 6.283185307179586
inf
A floating-point positive infinity.
print(math.inf) # inf
nan
A floating-point "not a number" value.
print(math.nan) # nan
Number-theoretic and Representation Functions
ceil
Returns the ceiling of x, the smallest integer greater than or equal to x.
print(math.ceil(4.2)) # 5
copysign
Returns a float with the magnitude of x but the sign of y.
print(math.copysign(3, -1)) # -3.0
fabs
Returns the absolute value of x.
print(math.fabs(-5.3)) # 5.3
factorial
Returns the factorial of x, an exact integer.
print(math.factorial(5)) # 120
floor
Returns the floor of x, the largest integer less than or equal to x.
print(math.floor(4.8)) # 4
fmod
Returns the remainder of x / y.
print(math.fmod(7, 3)) # 1.0
frexp
Returns the mantissa and exponent of x as the pair (m, e).
print(math.frexp(8)) # (0.5, 4)
fsum
Returns an accurate floating-point sum of values in the iterable.
print(math.fsum([0.1, 0.2, 0.3])) # 0.6
gcd
Returns the greatest common divisor of the integers a and b.
print(math.gcd(8, 12)) # 4
isfinite
Returns True if x is neither an infinity nor a NaN, and False otherwise.
print(math.isfinite(5)) # True
print(math.isfinite(math.inf)) # False
isinf
Returns True if x is a positive or negative infinity, and False otherwise.
print(math.isinf(math.inf)) # True
print(math.isinf(5)) # False
isnan
Returns True if x is a NaN (not a number), and False otherwise.
print(math.isnan(math.nan)) # True
print(math.isnan(5)) # False
ldexp
Returns x * (2**i). This is essentially the inverse of frexp
.
print(math.ldexp(0.5, 4)) # 8.0
modf
Returns the fractional and integer parts of x.
print(math.modf(4.5)) # (0.5, 4.0)
trunc
Returns the truncated integer value of x.
print(math.trunc(4.7)) # 4
Power and Logarithmic Functions
exp
Returns e raised to the power of x.
print(math.exp(2)) # 7.38905609893065
expm1
Returns e raised to the power of x, minus 1.
print(math.expm1(1)) # 1.718281828459045
log
Returns the natural logarithm of x.
print(math.log(2.718281828459045)) # 1.0
log1p
Returns the natural logarithm of 1 + x.
print(math.log1p(1)) # 0.6931471805599453
log2
Returns the base-2 logarithm of x.
print(math.log2(8)) # 3.0
log10
Returns the base-10 logarithm of x.
print(math.log10(100)) # 2.0
pow
Returns x raised to the power of y.
print(math.pow(2, 3)) # 8.0
sqrt
Returns the square root of x.
print(math.sqrt(16)) # 4.0
Trigonometric Functions
acos
Returns the arc cosine of x, in radians.
print(math.acos(1)) # 0.0
asin
Returns the arc sine of x, in radians.
print(math.asin(0)) # 0.0
atan
Returns the arc tangent of x, in radians.
print(math.atan(1)) # 0.7853981633974483
atan2
Returns atan(y / x), in radians.
print(math.atan2(1, 1)) # 0.7853981633974483
cos
Returns the cosine of x, in radians.
print(math.cos(math.pi / 4)) # 0.7071067811865476
sin
Returns the sine of x, in radians.
print(math.sin(math.pi / 2)) # 1.0
tan
Returns the tangent of x, in radians.
print(math.tan(math.pi / 4)) # 1.0
Angular Conversion
degrees
Converts angle x from radians to degrees.
print(math.degrees(math.pi)) # 180.0
radians
Converts angle x from degrees to radians.
print(math.radians(180)) # 3.141592653589793
Hyperbolic Functions
acosh
Returns the inverse hyperbolic cosine of x.
print(math.acosh(1)) # 0.0
asinh
Returns the inverse hyperbolic sine of x.
print(math.asinh(0)) # 0.0
atanh
Returns the inverse hyperbolic tangent of x.
print(math.atanh(0)) # 0.0
cosh
Returns the hyperbolic cosine of x.
print(math.cosh(0)) # 1.0
sinh
Returns the hyperbolic sine of x.
print(math.sinh(0)) # 0.0
tanh
Returns the hyperbolic tangent of x.
print(math.tanh(0)) # 0.0
Special Functions
erf
Returns the error function of x.
print(math.erf(0)) # 0.0
erfc
Returns the complementary error function of
x.
print(math.erfc(0)) # 1.0
gamma
Returns the Gamma function of x.
print(math.gamma(5)) # 24.0
lgamma
Returns the natural logarithm of the absolute value of the Gamma function of x.
print(math.lgamma(5)) # 3.1780538303479458
Examples
Calculating the Hypotenuse
Calculate the length of the hypotenuse of a right-angled triangle.
import math
def hypotenuse(a, b):
return math.sqrt(a**2 + b**2)
print(hypotenuse(3, 4)) # 5.0
Area of a Circle
Calculate the area of a circle given its radius.
import math
def area_of_circle(radius):
return math.pi * radius ** 2
print(area_of_circle(5)) # 78.53981633974483
Real-World Use Case
Compound Interest Calculation
Calculate the compound interest on an investment.
import math
def compound_interest(principal, rate, times_compounded, years):
return principal * (1 + rate / times_compounded) ** (times_compounded * years)
principal = 1000
rate = 0.05
times_compounded = 4
years = 10
print(compound_interest(principal, rate, times_compounded, years)) # 1647.00949769028
Conclusion
The math
module in Python provides a wide range of mathematical functions and constants that are essential for various calculations and scientific computing. By utilizing these functions, you can perform complex mathematical operations with ease and precision.
Comments
Post a Comment
Leave Comment