In python, the math module is useful to perform mathematical related operations. The math module has different functions (trigonometric, logarithmic, conversion, etc.) and constants to perform various mathematical tasks.
The following table lists the constants available in the python math module.
Constant | Description |
---|---|
math.pi | It will return the pi value. |
math.tau | It will return the tau value. |
math.e | It will return the Euler's number. |
math.inf | It will return the floating-point positive infinity. |
math.nan | It will return the floating-point NaN value. |
Following is the example of using the math module constants in python.
The above python math module constants example will return the result as shown below.
Same as constants, the math module has different methods to perform mathematical operations.
Following is the example of using math module functions to perform different operations in python.
The above python math module example will return the result as shown below.
The following table lists the different methods available in the math module to perform logarithmic, trigonometric, etc., operations based on our requirements.
Method | Description |
---|---|
math.acos() | It will return the arc cosine of a number. |
math.acosh() | It returns the inverse hyperbolic cosine of a number. |
math.asin() | This method will return the arc sine of number |
math.asinh() | It returns the inverse hyperbolic sine of number |
math.atan() | It returns the arc tangent of a number in radians |
math.atan2() | It returns the arc tangent of y/x in radians |
math.atanh() | It returns the inverse hyperbolic tangent of number |
math.ceil() | It will round a number up to the nearest integer |
math.comb() | It returns the number of ways to choose k items from n items without repetition and order |
math.copysign() | It returns a float with the magnitude (absolute value) of x but the sign of y. |
math.cos() | It will return the cosine of a number. |
math.cosh() | It will return the hyperbolic cosine of a number. |
math.degrees() | It converts angle x from radians to degrees. |
math.dist() | Returns the Euclidean distance between two points (p and q). |
math.erf() | It will return the error function of a number. |
math.erfc() | It will return the complementary error function of a number. |
math.exp() | It will return E raised to the power of x. |
math.expm1() | It returns exp(x) - 1 |
math.fabs() | It will return the absolute value of the number. |
math.factorial() | It returns the factorial of a number. |
math.floor() | It will round the number down to the nearest integer. |
math.fmod() | It returns the remainder of x/y |
math.frexp() | It returns the mantissa and the exponent of a specified number. |
math.fsum() | It will return the sum of values in the iterable seq. |
math.gamma() | It will return the gamma function at x |
math.gcd() | It will return the greatest common divisor of two integers |
math.hypot() | It will return the hypot value. |
math.isclose() | Determines whether two numbers are close in value or not |
math.isfinite() | Returns True, if the number is neither an infinite nor a NAN, otherwise returns False. |
math.isinf() | Returns True, if the number is positive or negative infinity, otherwise returns False. |
math.isnan() | Returns True, if the number is NaN (not a number), otherwise False. |
math.isqrt() | It rounds the square root number downwards to the nearest integer |
math.ldexp() | It will return the x * (2**i) of the given numbers x and i |
math.lgamma() | It will return the log gamma value of x |
math.log() | Returns the logarithm of a number or the logarithm of a number to base |
math.log10() | Returns the base 10 logarithm of x |
math.log1p() | Returns the natural logarithm of 1+x |
math.log2() | Returns the base 2 logarithm of x |
math.perm() | Returns the number of ways to choose k items from n items without repetition and with an order. |
math.pow() | Returns the value of x to the power of y |
math.prod() | Returns the product of all the elements in the input iterable. |
math.radians() | It converts a degree value into radians |
math.remainder() | It returns the closest value that can make the numerator completely divisible by the denominator |
math.sin() | It returns the sine of number |
math.sinh() | It returns the hyperbolic sine of number |
math.sqrt() | It will return the square root of number |
math.tan() | It returns the tangent of number |
math.tanh() | It returns the hyperbolic tangent of number |
math.trunc() | It returns the truncated integer parts of a number |