Python Math Module

Python Math Module

Now, Let’s discuss a very commonly used module, which is the math module. As the name of the module suggests, it is used to perform some mathematical operations. We are going to look at some of the different things that we can do using the math module. Let’s have a look at some of the insights into what we are going to cover.

  • Methods from the math module
    • ceil method
    • factorial method
    • floor method
    • fabs method
    • pow method
    • sqrt method
  • Constants from the math module

First of all, here is a list of some of the methods, that are available for us, with the math module.

MethodsDescription
ceil(x)This method returns the ceiling of x, which simply means the smallest integer, greater than or equal to x.
factorial(x)This method returns the factorial of x as an integer. It raises ValueError, if the x is not integral, or is negative.
floor(x)This method returns the floor of x, which simply means the largest integer less than or equal to x.
comb(k, n)This method returns the number of ways to choose the k items from n items, without repetition and without order.
fabs(x)This method returns the absolute value of x.
fsum(iterable)This method returns an accurate floating-point sum of values in the iterable.
gcd(*numbers)This method returns the greatest common divisor of the specified integer arguments.
isclose(a, b)This method returns True if the values a and b are close to each other, and False otherwise.
isfinite(x)As the name of the method says, this method returns True, if x is neither infinity nor a NaN, and False otherwise.
isinf(x)This method returns True, if x is a positive or negative infinity, and False otherwise.
isnan(x)This method returns True, if x is NaN (not a number), and False otherwise.
trunc(x)This method returns the integer with the fractional part removed and leaving the integer part.
log(x, base)This method returns the logarithm of x to the given base. If the base is not specified, it returns a log of x to the base e.
log2(x)This method returns the logarithm of x to base 2.
log10(x)This method returns the logarithm of x to the base 10.
pow(x, y)This method returns the result of x to the power of y.
sqrt(x)This method returns the square root of the value x.
sin(x)This method returns the sine of x radians.
cos(x)This method returns the cosine of x radians.
tan(x)This method returns the tangent of x radians.

There are many other methods from the math module, which you can simply explore. But here, we are going to consider some methods from the above-mentioned table, so that we can simply understand the methods from the math module.