Python math

Python math module consists of important constants like pi, tau, etc. and most of commonly used mathematical functions. We can use all these functions in our programming while solving the poblems.

python math constants

math.pi

  • It represents the mathematical constant π = 3.14159
  • Let's write a simple program to calculate the area of a circle
import math

def get_circle_area(radius):
    area = math.pi * radius ** 2
    return area

math.e

  • It represents the mathematical constant e = 2.718281
  • It's also called the Euler’s number.
import math

print(math.e)
# output: 2.718281828459045

math.tau

  • It represents the mathematical constant τ = 6.28318
import math
print(math.tau)
# output: 6.283185307179586

math.inf

  • It represents the value of floating-point infinity
import math
print(math.inf)
# output: inf

math.nan

  • It represents floating-point NaN value which means Not a Number
import math
print(math.nan)
# output: nan

python math functions

  • The numeric functions in the math module take real numbers/integers as arguments and after doing the specified mathematical calculations, it returns a number or a boolean value.

math.ceil(x)

  • Rounds a number up to the nearest integer
import math

print(math.ceil(1.4))
# output: 2
print(math.ceil(-5.3))
# output: -5

math.floor(x)

  • Rounds a number down to the nearest integer
import math

print(math.floor(0.6))
# output: 0
print(math.floor(1.4))
# output: 1

math.fabs(x)

import math

print(math.fabs(7.5))
# output: 7.5
print(math.fabs(-7.5))
# output: 7.5

math.copysign(x,y)

  • Returns a float consisting of the value of the first parameter and the sign of the second parameter
import math

print(math.copysign(7.5, -1))
# output: -7.5
print(math.copysign(-120, 100))
# output: 120.0

math.factorial(x)

  • Returns the factorial of a given number
import math

print(math.factorial(5))
# output: 120
print(math.factorial(16))
# output: 20922789888000

math.fmod(x,y)

  • Returns the remainder of x/y
import math

print(math.fmod(23, 6))
# output: 5.0

math.gcd(x,y)

  • Returns the greatest common divisor of two integers
import math

print(math.gcd(23, 6))
# output: 1

math.comb(n,r)

  • Returns the number of ways to choose k items from n items without repetition and order
import math

print(math.comb(5, 3))
# output: 10

math.frexp(x)

  • Return the mantissa and exponent of x as the pair (m, e). m is a float and e is an integer such that x == m * 2**e exactly. If x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1. This is used to “pick apart” the internal representation of a float in a portable way.
import math

print(math.frexp(5))
# output: (0.625, 3)

math.fsum(iterable)

  • Returns the sum of all items in any iterable (tuples, arrays, lists, etc.)
import math

print(math.fsum([5, 3, 7]))
# output: 15.0

math.isclose(x, y, rel_tol = 1e-09, abs_tol = 0.0)

  • Checks whether two values are close to each other, or not
import math

print(math.isclose(1.233, 1.233))
# output: True
print(math.isclose(1.233, 1.24))
# output: False

math.isfinite(x)

  • Check whether a value is finite or not
import math

print(math.isfinite(100))
# output: True
print(math.isfinite(float("-inf")))
# output: False
print(math.isfinite(math.inf))
# output: False
print(math.isfinite(0))
# output: True

math.isinf(x)

  • Checks whether a number is infinite or not
import math

print(math.isinf(math.inf))
# output: True
print(math.isinf(0))
# output: False

math.isnan(x)

  • Checks whether a value is NaN (not a number) or not
import math

print(math.isnan(math.nan))
# output: True
print(math.isnan(10))
# output: False

math.sqrt(x)

  • Return the square root of x.
import math

print(math.sqrt(100))
# output: 10.0
print(math.sqrt(10))
# output: 3.1622776601683795

math.isqrt(x)

  • Rounds a square root number downwards to the nearest integer
import math

print(math.isqrt(100))
# output: 10.0
print(math.isqrt(10))
# output: 3

math.lcm(x,y,z….)

  • Return the least common multiple of the specified integer arguments.
import math

print(math.lcm(10, 15, 21, 8))
# output: 840

Note: only available after 3.9

math.ldexp(x,i)

  • Return x * (2**i). This is essentially the inverse of function frexp().
import math

print(math.ldexp(9, 3))
# output: 72.0
print(math.ldexp(-5, 2))
# output: -20

math.modf(x)

  • Return the fractional and integer parts of x. Both results carry the sign of x and are floats.
import math

print(math.modf(9))
# output: (0.0, 9.0)
print(math.modf(12.5))
# output: (0.5, 12.0)

math.nextafter(x,y)

  • Return the next floating-point value after x towards y.
import math

print(math.nextafter(-5, math.inf))
# output: -4.999999999999999
print(math.nextafter(10, -math.inf))
# output: 9.999999999999998
print(math.nextafter(16, 0.0))
# output: 15.999999999999998

math.prod(iterable, start=1)

  • Calculate the product of all the elements in the input iterable. The default start value for the product is 1.
import math

print(math.prod([1,2,3,4,5,6]))
# output: 720
print(math.prod([]))
# output: 1

math.remainder(x,y)

  • Returns the closest value that can make numerator completely divisible by the denominator
import math

print(math.remainder(17, 2))
# output: 1
print(math.remainder(2, 17))
# output: 2

math.trunc(x)

  • Returns the truncated integer parts of a number
import math

print(math.trunc(2.77))
# output: 2
print(math.trunc(-3.77))
# output: -3

math.pow(x, y)

  • Returns the value of x to the power of y
import math

print(math.pow(2, 4))
# output: 16.0

math.exp(x)

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

print(math.exp(4))
# output: 54.598150033144236
print(math.exp(-6.89))
# output: 0.0010179138409954387

math.expm1(x)

  • Return e raised to the power x, minus 1. Here e is the base of natural logarithms.
import math

print(math.expm1(4))
# output: 53.598150033144236

math.log(x, [b])

  • return the logarithm of x to the given base, calculated as log(x)/log(base).
import math

print(math.log(10, 2))
# output: 3.3219280948873626

math.log1p(x)

  • Return the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.
import math

print(math.log1p(10))
# output: 2.3978952727983707

math.log2(x)

  • Return the base-2 logarithm of x. This is usually more accurate than log(x, 2).
import math

print(math.log2(10))
# output: 3.321928094887362
  • Math module also provides trignometric calculations also.

Note: If you want more advanced maths then look at python packages like Numpy, SciPy, Statsmodel, Scikit-learn, etc.

References