Python Math Module
The math module in Python provides access to various mathematical functions, constants, and operations. These functions typically operate on floats or integers.
Importing the math Module
import math
Commonly Used Functions in math
- Basic Mathematical Functions:
math.sqrt(x): Returns the square root ofx.print(math.sqrt(16)) # Output: 4.0math.pow(x, y): Returnsxraised to the power ofy(x^y).print(math.pow(2, 3)) # Output: 8.0math.exp(x): Returnseraised to the power ofx(e^x).print(math.exp(1)) # Output: 2.718281828459045 (value of e)math.log(x, base): Returns the logarithm ofxto the specifiedbase. If no base is given, it returns the natural logarithm (basee).print(math.log(10)) # Output: 2.302585092994046 (natural log) print(math.log(100, 10)) # Output: 2.0 (log base 10)math.log10(x): Returns the base-10 logarithm ofx.print(math.log10(1000)) # Output: 3.0math.log2(x): Returns the base-2 logarithm ofx.print(math.log2(8)) # Output: 3.0
- Trigonometric Functions:
math.sin(x): Returns the sine ofxradians.print(math.sin(math.pi / 2)) # Output: 1.0math.cos(x): Returns the cosine ofxradians.print(math.cos(math.pi)) # Output: -1.0math.tan(x): Returns the tangent ofxradians.print(math.tan(math.pi / 4)) # Output: 1.0math.asin(x): Returns the arc sine (inverse sine) ofx.print(math.asin(1)) # Output: 1.5707963267948966 (pi/2)math.acos(x): Returns the arc cosine (inverse cosine) ofx.print(math.acos(-1)) # Output: 3.141592653589793 (pi)math.atan(x): Returns the arc tangent (inverse tangent) ofx.print(math.atan(1)) # Output: 0.7853981633974483 (pi/4)
- Hyperbolic Functions:
math.sinh(x): Returns the hyperbolic sine ofx.print(math.sinh(1)) # Output: 1.1752011936438014math.cosh(x): Returns the hyperbolic cosine ofx.print(math.cosh(1)) # Output: 1.5430806348152437math.tanh(x): Returns the hyperbolic tangent ofx.print(math.tanh(1)) # Output: 0.7615941559557649
- Rounding and Remainder Functions:
math.ceil(x): Returns the smallest integer greater than or equal tox.print(math.ceil(4.3)) # Output: 5math.floor(x): Returns the largest integer less than or equal tox.print(math.floor(4.8)) # Output: 4math.trunc(x): Truncates the decimal part ofx, returning the integer part only.print(math.trunc(4.9)) # Output: 4math.fmod(x, y): Returns the remainder ofxdivided byy.print(math.fmod(17, 5)) # Output: 2.0
- Factorial and Combinatorics:
math.factorial(x): Returns the factorial ofx(x!).print(math.factorial(5)) # Output: 120math.comb(n, k): Returns the number of ways to choosekitems fromnwithout repetition and without order.print(math.comb(5, 3)) # Output: 10math.perm(n, k): Returns the number of ways to choosekitems fromnwithout repetition but with order.print(math.perm(5, 3)) # Output: 60
- Special Functions:
math.gcd(a, b): Returns the greatest common divisor ofaandb.print(math.gcd(8, 12)) # Output: 4math.isqrt(x): Returns the integer square root ofx.print(math.isqrt(16)) # Output: 4math.lcm(a, b): Returns the least common multiple ofaandb.print(math.lcm(8, 12)) # Output: 24
- Constants:
math.pi: The mathematical constant pi (π), approximately 3.14159.print(math.pi) # Output: 3.141592653589793math.e: The mathematical constant e, approximately 2.71828.print(math.e) # Output: 2.718281828459045math.tau: The constant τ, which is 2π (approximately 6.28318).print(math.tau) # Output: 6.283185307179586math.inf: Represents positive infinity.print(math.inf) # Output: infmath.nan: Represents “Not a Number” (NaN).print(math.nan) # Output: nan
Example Usage:
import math
# Square root
print(math.sqrt(25)) # Output: 5.0
# Trigonometric functions
angle = math.pi / 4
print(math.sin(angle)) # Output: 0.7071067811865475
print(math.cos(angle)) # Output: 0.7071067811865476
# Logarithm
print(math.log(100, 10)) # Output: 2.0
# Factorial
print(math.factorial(5)) # Output: 120
# Greatest Common Divisor
print(math.gcd(48, 18)) # Output: 6
The math module is highly useful for various mathematical computations, especially when working with floating-point numbers, logarithms, trigonometric functions, and special functions like factorial and GCD.