Top 10 math functions in python

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

import math

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 which is greater than or equal to the given float value. Similarly the floor() method returns an integer which is lesser or equal to the given float value. If an integer value is given the same value is returned.

ceil() and floor() functions in python

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

factorial method in python

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.

gcd in python

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.

sqrt in python

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.

pow function in python

sin, cos and tan

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

math cos, sin and tan

exp

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

exp() function

fabs

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

fabs() method in python

isclose

The isclose() method takes two values. We can specify the value of 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 relative or absolute difference.

isclose() method in python

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.

constants

Hope these methods helped you.

Happy coding!