Python Math Module
The math
module in Python gives a wide range of mathematical functions and constants. You do not need to install anything because it is a part of Python’s standard library. Below is the detailed explanation of the module along with examples:
1. Importing the math Module
To use the math
module, you need to import it:
import math
2. Constants in the math
Module
The math
module contains the following constants:
math.pi
: The mathematical constant π (pi), approximately 3.14159.math.e
: Euler’s number (e), approximately 2.71828.math.tau
: The mathematical constant τ (tau), which is equal to 2π.math.inf
: Infinity.math.nan
: “Not a Number” (Nan).
Example:
import math
print(math.pi) # 3.141592653589793
print(math.e) # 2.718281828459045
print(math.tau) # 6.283185307179586
print(math.inf) # inf
print(math.nan) # nan
3. Basic Operations
The math
module offers functions for basic mathematical operations:
math.sqrt(x)
: Square root ofx
.math.pow(x, y)
: x to the powery
.math.ceil(x)
: Smallest integer greater than or equal tox
.math.floor(x)
: Largest integer less than or equal tox
.math.fabs(x)
: Absolute value ofx
.math.factorial(x)
: Factorial ofx
.
Example:
print(math.sqrt(16)) # 4.0
print(math.pow(2, 3)) # 8.0
print(math.ceil(3.7)) # 4
print(math.floor(3.7)) # 3
print(math.fabs(-5.5)) # 5.5
print(math.factorial(5)) # 120
4. Trigonometric Functions
The math module offers different trigonometric functions. These functions deal with angles in radians.
math.sin(x)
: sine of x (x in radians).math.cos(x)
: cosine of x (x in radians).math.tan(x)
: tangent of x (x in radians).math.asin(x)
: arc sine of x.math.acos(x)
: arc cosine of x.math.atan(x)
: arc tangent of x.math.radians(x)
: converts degrees to radians.math.degrees(x)
: converts radians to degrees.
Example:
angle_deg = 45
angle_rad = math.radians(angle_deg)
print(math.sin(angle_rad)) # 0.7071067811865475
print(math.cos(angle_rad)) # 0.7071067811865476
print(math.tan(angle_rad)) # 1.0
print(math.degrees(math.pi / 4)) # 45.0
5. Logarithmic and Exponential Functions
math.log(x, base)
: Logarithm ofx
withbase
stated (by defaulte
).math.log2(x)
: Logarithm ofx
with base 2.math.log10(x)
: Logarithm ofx
with base 10.math.exp(x)
: Exponential function ofx
(e^x
).
Example:
print(math.log(8, 2)) # 3.0
print(math.log10(1000)) # 3.0
print(math.log2(16)) # 4.0
print(math.exp(2)) # 7.3890560989306495
6. Special Functions
math.gcd(a, b)
: Greatest common divisor ofa
andb
.math.lcm(a,b)
. Least common multiple ofa
andb
.math.isfinite(x)
: returnsTrue
ifx
is a finite number.math.isinf(x)
: ReturnsTrue
ifx
is infinite.math.isnan(x)
: ReturnsTrue
ifx
is NaN.
Example:
print(math.gcd(20, 8)) # 4
print(math.lcm(12, 15)) # 60
print(math.isfinite(5)) # True
print(math.isinf(math.inf)) # True
print(math.isnan(math.nan)) # True
7. Hyperbolic Functions
math.sinh(x)
: Hyperbolic sine ofx
.math.cosh(x)
: Hyperbolic cosine ofx
.math.tanh(x)
: Hyperbolic tangent ofx
.
Example:
print(math.sinh(1)) # 1.1752011936438014
print(math.cosh(1)) # 1.5430806348152437
print(math.tanh(1)) # 0.7615941559557649
8. Combinatorics
math.comb(n, k)
: Number of ways to choose k items from n items without repetition (nCk).math.perm(n, k)
: Number of ways to arrange k items out of n items (nPk).
Example:
print(math.comb(5, 2)) # 10
print(math.perm(5, 2)) # 20
9. Floating-Point Operations
math.fsum(iterable)
: Returns an accurate floating-point sum of values in the iterable.math.isclose(a, b, rel_tol=1e-9)
: Checks ifa
andb
are approximately equal.
Example:
numbers = [0.1, 0.2, 0.3]
print(math.fsum(numbers)) # 0.6
print(math.isclose(0.1 + 0.2, 0.3)) # True
Summary:
The math
module is crucial to perform advanced mathematical calculations within Python. It includes a set of functions to perform operations on numbers, trigonometry, logarithms, combinatorics, and more.