Python math Module

Python math Module

Python has a built-in module that you can use for mathematical tasks.

The `math` module has a set of methods and constants.

Math Methods

Method Description
math.acos(x) Returns the arc cosine value of x
math.acosh(x) Returns the hyperbolic arc cosine of x
math.asin(x) Returns the arc sine of x
math.asinh(x) Returns the hyperbolic arc sine of x
math.atan(x) Returns the arc tangent value of x
math.atan2(y, x) Returns the arc tangent of y/x in radians
math.atanh(x) Returns the hyperbolic arctangent value of x
math.ceil(x) Rounds a number upwards to the nearest integer, and returns the result
math.comb(n, k) Returns the number of ways to choose k items from n items without repetition and order
math.copysign(x, y) Returns a float consisting of the value of the first parameter and the sign of the second parameter
math.cos(x) Returns the cosine of x
math.cosh(x) Returns the hyperbolic cosine of x
math.degrees(x) Converts an angle from radians to degrees
math.dist(p, q) Calculates the euclidean distance between two specified points (p and q), where p and q are the coordinates of that point
math.erf(x) Returns the error function of x
math.erfc(x) Returns the complementary error function of x
math.exp(x) Returns the value of Ex, where E is Euler's number (approximately 2.718281...), and x is the number passed to it
math.expm1(x) Returns the value of Ex - 1, where E is Euler's number (approximately 2.718281...), and x is the number passed to it
math.fabs(x) Returns the absolute value of a number
math.factorial() Returns the factorial of a number
math.floor(x) Rounds a number downwards to the nearest integer, and returns the result
math.fmod(x, y) Returns the remainder of specified numbers when a number is divided by another number
math.frexp() Returns the mantissa and the exponent, of a specified value
math.fsum(iterable) Returns the sum of all items in an iterable (tuples, arrays, lists, etc.)
math.gamma(x) Returns the gamma value of x
math.gcd() Returns the highest value that can divide two integers
math.hypot() Find the Euclidean distance from the origin for n inputs
math.isclose() Checks whether two values are close, or not
math.isfinite(x) Checks whether x is a finite number
math.isinf(x) Check whether x is a positive or negative infinty
math.isnan(x) Checks whether x is NaN (not a number)
math.isqrt(n) Returns the nearest integer square root of n
math.ldexp(x, i) Returns the expression x * 2i where x is mantissa and i is an exponent
math.lgamma(x) Returns the log gamma value of x
math.log(x, base) Returns the natural logarithm of a number, or the logarithm of number to base
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) Returns the number of ways to choose k items from n items with order and without repetition
math.pow(x, y) Returns the value of x to the power of y
math.prod(iterable, *, start=1) Returns the product of an iterable (lists, array, tuples, etc.)
math.radians(x) Converts a degree value (x) to radians
math.remainder(x, y) Returns the closest value that can make numerator completely divisible by the denominator
math.sin(x) Returns the sine of x
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
math.tanh(x) Returns the hyperbolic tangent of x
math.trunc(x) Returns the truncated integer parts of x

Math Constants

Constant Description
math.e Returns Euler's number (2.7182...)
math.inf Returns a floating-point positive infinity
math.nan Returns a floating-point NaN (Not a Number) value
math.pi Returns PI (3.1415...)
math.tau Returns tau (6.2831...)