Python Tutorial (41) - math module

Time: Column:Python views:217

Python Math Module (2024)

The Python math module provides numerous mathematical functions for floating-point operations.

All functions under the math module return floating-point values unless otherwise specified.

If you need to calculate with complex numbers, use the same-named functions in the cmath module.

To use the math functions, you must first import the module:

import math

You can check the contents of the math module using the following command:

>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

Constants in the math Module

ConstantDescription
math.eReturns Euler's number (2.7182...)
math.infReturns positive infinity
math.nanReturns NaN (not a number)
math.piπ, representing the ratio of a circle's circumference to its diameter (3.1415...)
math.tauMathematical constant τ = 6.283185..., which is equal to 2π, representing the ratio of a circle's circumference to its radius.

Methods in the math Module

MethodDescription
math.acos(x)Returns the arc cosine of x, result between 0 and π.
math.acosh(x)Returns the inverse hyperbolic cosine of x.
math.asin(x)Returns the arc sine of x, result between -π/2 and π/2.
math.asinh(x)Returns the inverse hyperbolic sine of x.
math.atan(x)Returns the arc tangent of x, result between -π/2 and π/2.
math.atan2(y, x)Returns the arc tangent of y/x, result between -π and π.
math.atanh(x)Returns the inverse hyperbolic tangent of x.
math.ceil(x)Rounds x upwards to the nearest integer.
math.comb(n, k)Returns the number of ways to choose k items from n without repetition and without order.
math.copysign(x, y)Returns a float with the magnitude of x but the sign of y.
math.cos(x)Returns the cosine of x radians.
math.cosh(x)Returns the hyperbolic cosine of x.
math.degrees(x)Converts angle x from radians to degrees.
math.dist(p, q)Returns the Euclidean distance between points p and q. Points must have the same dimensions.
math.erf(x)Returns the error function of x.
math.erfc(x)Returns the complementary error function at x.
math.exp(x)Returns e raised to the power of x.
math.expm1(x)Returns e^x - 1, where e is Euler's number.
math.fabs(x)Returns the absolute value of x.
math.factorial(x)Returns the factorial of x. Raises a ValueError if x is not an integer or is negative.
math.floor(x)Rounds x down to the nearest integer.
math.fmod(x, y)Returns the remainder when x is divided by y.
math.frexp(x)Returns the mantissa and exponent of x as a pair (m, e).
math.fsum(iterable)Returns the sum of all elements in an iterable as a floating-point number.
math.gamma(x)Returns the gamma function value at x.
math.gcd(a, b)Returns the greatest common divisor of integers a and b.
math.hypot(*coordinates)Returns the Euclidean norm, sqrt(sum(x**2 for x in coordinates)).
math.isclose(a, b)Returns True if a and b are close in value, otherwise False.
math.isfinite(x)Returns True if x is finite, otherwise False.
math.isinf(x)Returns True if x is infinite, otherwise False.
math.isnan(x)Returns True if x is NaN (not a number), otherwise False.
math.isqrt(x)Returns the integer square root of x.
math.ldexp(x, i)Returns x * (2**i). Essentially the inverse of math.frexp().
math.lgamma(x)Returns the natural logarithm of the absolute value of the gamma function at x.
math.log(x[, base])Returns the logarithm of x to the given base (natural log if not specified).
math.log10(x)Returns the base-10 logarithm of x.
math.log1p(x)Returns the natural logarithm of 1+x.
math.log2(x)Returns the base-2 logarithm of x.
math.perm(n, k=None)Returns the number of ways to choose k items from n with repetition and with order.
math.pow(x, y)Returns x raised to the power of y.
math.prod(iterable)Returns the product of all elements in an iterable.
math.radians(x)Converts angle x from degrees to radians.
math.remainder(x, y)Returns the remainder of x / y, following IEEE 754 standards.
math.sin(x)Returns the sine of x radians.
math.sinh(x)Returns the hyperbolic sine of x.
math.sqrt(x)Returns the square root of x.
math.tan(x)Returns the tangent of x radians.
math.tanh(x)Returns the hyperbolic tangent of x.
math.trunc(x)Returns the truncated integer value of x. Removes the decimal part.

The math module in Python provides a robust set of functions and constants for performing mathematical operations. 


Let me know if you need further adjustments!