Top 10 math functions in python

Photo by Annie Spratt on Unsplash

The math module provides us various functions. Some of them are discussed in this article. Import the math function to begin.

The ten methods in this article are

  1. ceil and floor
  2. factorial
  3. gcd
  4. sqrt
  5. pow
  6. sin, cos, tan
  7. exp
  8. fabs
  9. isclose
  10. constants

Ceil and floor functions

The ceil() method returns an integer that is greater than or equal to the given float value. Similarly, the floor() method returns an integer that is lesser or equal to the given float value. If an integer value is given the same value is returned.

factorial

This method returns the factorial value of a number. In mathematics, the factorial of a positive integer n, denoted by n! is the product of all positive integers less than or equal to n.

Example 4! is 4*3*2*1 and 5! is 5*4*3*2*1

gcd

The gcd() method returns the gcd of the two numbers passed to the function. In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4.

sqrt

The sqrt() method returns the square root of a number. In mathematics, a square root of a number x is a number y such that y² = x.

pow

The pow(x, y) method returns the value of x raised to the power of y. The return type is always float and hence takes both integer or float as arguments.

sin, cos, and tan

The sin, cos, and tan methods take a degree and return their corresponding value.

exp

Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.

fabs

The fabs() method returns the absolute value of the number as a floating-point value. This is similar to the abs() method except for the result here is in float.

isclose

The isclose() method takes two values. We can specify the value of the difference. A difference value greater than this returns False and a difference value less than this returns True. By default, the difference value is 0.

The difference value can be specified as a relative or absolute difference.

The first command returns false as the difference between 2 and 5 is 3, but the difference that we have specified is 1.

Whereas the second command returns true as the difference between 2 and 3 is 1, which matches our difference.

Constants

The values of mathematical constants such as π, Τ, and e are also available in the math module.

Conclusion

Hope these methods helped you. Happy coding!

Python Developer | Django Developer | Python Enthusiast