In an earlier post, we have learnt some of the basic mathematical operators in Python. In this post we will cover some of the key mathematical functions in Python which provide access to more mathematical operations in Python.

Built-in functions

The following mathematical functions are built-in and can be accessed directly:

Function nameDescription
abs(x)Returns the absolute value of a number (i.e., x). x may be an integer or a floating point number.
divmod(x,y)Returns a pair of numbers containing quotient and remainder as a result of the division of x and y. If x and y are integers, the result is same as (x//y,x%y). If x and y are decimal numbers, the quotient is the integer part of x/y without rounding off.
min(x,y)Returns the smallest of x and y
max(x,y)Returns the largest of x and y
pow(x,y)Returns x raised to the power y
round(x, ndigits = y)Returns x rounded off to y digits. If y is not specified, returns the nearest integer. Where the decimal part is 0.5, returns the nearest even number.

math Class

Python has a math class which comes bundled in the standard library and provides more constants and functions above and over the built-in functions.

To use the math class, we need to include it in our program first. Any class in Python can be included in the program using the import keyword.

Our list below covers some of the important functions from the math class. If you are interested in knowing more about the math class, you can refer the official documentation here.

Constant nameDescription
math.piThe mathematical constant PI.
math.eThe mathematical constant e.
Function nameDescription
math.ceil(x)Returns the ceiling of x i.e., smallest integer greater than or equal to x.
math.comb(n,k)Returns number of ways to choose k items from n items without repetition and order.
math.fabs(x)Returns absolute value of x.
math.factorial(x)Returns the integer factorial of x. x must be a positive integer.
math.floor(x)Returns the floor of x i.e., largest integer smaller than or equal to x.
math.gcd(*integers)Returns the greatest common divisor among the specified integers.
math.lcm(*integers)Returns the least common multiple among the specified integers.
math.perm(n,k)Returns number of ways to choose k items from n items without repetition and with order. k may not be specified and in such case, it returns n factorial.
math.cbrt(x)Returns the cube root of x.
math.sqrt(x)Returns the square root of x.
math.exp(x)Returns e raised to the power x.
math.log(n,k)Returns log of x to the base k. If k is not specified, returns natural log of x.
math.pow(x,y)Returns x raised to the power y. The result is a floating point number and both x and y are converted to float.

Example

Some of the above functions can be demonstrated using a simple program which accepts 2 numbers as under:

import math

a = int(input("Enter a number: "))
b = float(input("Enter a decimal number: "))

print ("Built-in functions: ")
print (f"Absolute value of {a} is {abs(a)}")
print ("Minimum of",a,"and",b,"is",min(a,b))
print (b,"rounded off to",a,"digits is", round(b,ndigits=a))

print ("Math class functions: ")
print (f"Ceiling value of {b} is {math.ceil(b)}")
print (f"Floor value of {b} is {math.floor(b)}")
print (a,"factorial is",math.factorial(a))

The output of the above program can be seen as under: Program Output

Summary

In this post, we learnt about the key mathematical functions in Python. Thank you for reading through and hope you learnt something valuable from this post. Do not forget to share this post and follow us for more similar content.