Math Module in Python
The math Module in Python
The math module in Python provides a set of mathematical functions and constants that allow you to perform various advanced mathematical operations. The module includes functions for trigonometry, logarithms, factorials, constants like pi, and much more.
You need to import the math module to use its functions.
import mathKey Functions and Constants in the math Module
1. Constants
math.pi: The constant ? (pi), approximately3.141592653589793.math.e: The constant e (Euler's number), approximately2.718281828459045.math.tau: The constant ? (tau, 2 * ?), approximately6.283185307179586.math.inf: Positive infinity.math.nan: Not a Number (NaN), used to represent undefined or unrepresentable values.
import mathprint(math.pi) # Output: 3.141592653589793print(math.e) # Output: 2.718281828459045print(math.inf) # Output: inf2. Trigonometric Functions
The trigonometric functions in the math module expect angles to be in radians, not degrees. You can convert degrees to radians using the math.radians() function.
math.sin(x): Sine ofx(x in radians).math.cos(x): Cosine ofx(x in radians).math.tan(x): Tangent ofx(x in radians).math.asin(x): Inverse sine (arcsin) ofx, result in radians.math.acos(x): Inverse cosine (arccos) ofx, result in radians.math.atan(x): Inverse tangent (arctan) ofx, result in radians.
import mathangle_in_radians = math.radians(90) # Convert 90 degrees to radiansprint(math.sin(angle_in_radians)) # Output: 1.0print(math.cos(angle_in_radians)) # Output: 6.123233995736766e-17 (approximately 0)print(math.tan(angle_in_radians)) # Output: 1.633123935319537e+16 (very large)3. Logarithmic Functions
math.log(x, base): Returns the logarithm ofxto the givenbase. If no base is provided, it calculates the natural logarithm (basee).math.log10(x): Returns the base-10 logarithm ofx.math.log2(x): Returns the base-2 logarithm ofx.
import mathprint(math.log(100)) # Natural logarithm of 100 (base e), Output: 4.605170186print(math.log(100, 10)) # Logarithm base 10, Output: 2.0print(math.log2(8)) # Logarithm base 2, Output: 3.04. Power and Exponentiation
math.pow(x, y): Returnsxraised to the power ofy(equivalent tox ** y).math.sqrt(x): Returns the square root ofx.math.exp(x): Returns e raised to the power ofx(e^x).
import mathprint(math.pow(2, 3)) # Output: 8.0print(math.sqrt(16)) # Output: 4.0print(math.exp(2)) # Output: 7.3890560989 (approximately e^2)5. Factorials and Combinations
math.factorial(x): Returns the factorial ofx(x!).math.comb(n, k): Returns the number of ways to choosekitems fromnitems without repetition (combinations).math.perm(n, k): Returns the number of ways to arrangekitems fromnitems (permutations).
import mathprint(math.factorial(5)) # Output: 120 (5! = 5 * 4 * 3 * 2 * 1)print(math.comb(5, 2)) # Output: 10 (5 choose 2)print(math.perm(5, 2)) # Output: 20 (5 permute 2)6. Rounding and Floating Point
math.ceil(x): Returns the smallest integer greater than or equal tox(round up).math.floor(x): Returns the largest integer less than or equal tox(round down).math.trunc(x): Truncatesxto the nearest integer, removing the fractional part.
import mathprint(math.ceil(4.3)) # Output: 5print(math.floor(4.7)) # Output: 4print(math.trunc(4.7)) # Output: 47. Hyperbolic Functions
The math module also provides functions for hyperbolic operations.
math.sinh(x): Hyperbolic sine ofx.math.cosh(x): Hyperbolic cosine ofx.math.tanh(x): Hyperbolic tangent ofx.
import mathx = 1.0print(math.sinh(x)) # Output: 1.1752011936438014print(math.cosh(x)) # Output: 1.5430806348152437print(math.tanh(x)) # Output: 0.76159415595576498. Angle Conversion
math.degrees(x): Converts anglexfrom radians to degrees.math.radians(x): Converts anglexfrom degrees to radians.
import mathangle_in_degrees = 90angle_in_radians = math.radians(angle_in_degrees) # Converts degrees to radiansprint(angle_in_radians) # Output: 1.5707963267948966 (?/2)angle_in_degrees_back = math.degrees(angle_in_radians) # Converts radians back to degreesprint(angle_in_degrees_back) # Output: 90.09. Other Useful Functions
math.isqrt(x): Returns the integer square root ofx.math.gcd(a, b): Returns the greatest common divisor ofaandb.
import mathprint(math.isqrt(16)) # Output: 4print(math.gcd(56, 98)) # Output: 14Conclusion
The math module in Python is a powerful library that provides essential mathematical operations for performing calculations related to trigonometry, logarithms, exponentiation, combinatorics, and more. Whether you're working with simple arithmetic or more complex scientific computations, this module provides a wide range of functions to handle mathematical tasks efficiently.
Let me know if you'd like more examples or need help with a specific mathematical operation!