Python Math Module

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.

Math Constants

The following table lists the constants available in the python math module.

 

ConstantDescription
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.

 

import math

# Print the value of E
print ("E val:", math.e)
# Print the positive infinity print ("inf val:", math.inf)
# Print the negative infinity print ("Neg inf val:", -math.inf)
# Print the value of nan
print ("nan val:", math.nan)
# Print the value of pi
print ("pi val:", math.pi)
# Print the value of tau
print ("tau val:", math.tau)

The above python math module constants example will return the result like as shown below.

 

E val: 2.718281828459045
inf val: inf
Neg inf val: -inf
nan val: nan
pi val: 3.141592653589793
tau val: 6.283185307179586

Math Functions

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.

 

import math

# Print the square root of number
print (math.sqrt(64))
# Rounds number up to nearest integer
print (math.ceil(5.647))
# Rounds number down to nearest integer
print (math.floor(5.647))
# Print the power of number
print (math.pow(5,3))
# Print the sine of a number
print (math.sin(60))
# Print the cosine of a number
print (math.cos(60))
# Print the tangent of a number
print (math.tan(60))

The above python math module example will return the result as shown below.

 

8.0
6
5
125.0
-0.3048106211022167
-0.9524129804151563
0.320040389379563

The following table lists the different methods available in the math module to perform logarithmic, trigonometric, etc., operations based on our requirements.

 

MethodDescription
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