Functions in Python math Module

The math module in Python provides a wide range of mathematical functions and constants. It is commonly used for performing various mathematical operations beyond basic arithmetic. 

To use mathematical functions under this module, we have to import the module using import statement. Once imported, we can access its functions and constants using dot notation.

Some common categories of functions available in the math module:

Number-theoretic and Representation Functions:

math.ceil(x):

The function is used to return the ceiling value of a number, which means it is used to round a number up to the nearest integer that is greater than the number itself.

Example:

math.floor(x):

Returns the largest integer less than or equal to x.

Example:

math.fabs(x):

This function is used to calculate the absolute value of a given integer.

Example:

import math

number = -2.45

# calculating absolute value of number

absolute_value = math.fabs(number)

print(“Absolute value of”,number,”is”,absolute_value)

Output:

Absolute value of -2.45 is 2.45

math.factorial(x):

This function is used to find the factorial of a given integer.

Example:

import math

print(math.factorial(4))

print(f”Factorial of 5: {math.factorial(5)}”)

Output:

24

Factorial of 5: 120

math.gcd():

The math.gcd() function in Python’s math module is used to calculate the Greatest Common Divisor (GCD) of two or more integers.

Example:

import math

print(math.gcd(12, 3))

print(math.lcm(12, 3,8,20))

Output:

3

1

math.lcm():

The math.lcm() function in Python’s math module is used to calculate the least common multiple (LCM) of two or more integers.

Example:

import math

print(math.lcm(12, 3))

print(math.lcm(12, 3,8,20))

Output:

12

120

math.trunc(x):

Returns the integer part of x.

Example:

Power and Logarithmic Functions:

math.sqrt(x):

Returns the square root of x.

Example -1:

import math

print(math.sqrt(9))    

Output:

3.0

Example -2:

import math

x = 16
square_root = math.sqrt(x)
print(square_root)

Output:

4.0

math.exp(x):

Example:

import math

print(math.exp(1))

Output:

2.718281828459045

math.log(x, base):

Returns the logarithm of x to the given base

Example:

import math

print(math.log(100, 10))

Output:

2.0

math.log10(x):

Returns the base-10 logarithm of x. 

math.log2(x):

Returns the base-2 logarithm of x. 

Trigonometric Functions:

math.sin(x):

Returns the sine of x (where x is in radians).

Example:

import math

angle_radians = math.radians(90)
sine_value = math.sin(angle_radians)
print(sine_value)

OR

import math

#Sine of 90 degrees (converted to radians)
print(f”Sine of 90 degrees: {math.sin(math.radians(90))}”)

math.cos(x):

Returns the cosine of x (where x is in radians).

math.tan(x):

Returns the tangent of x (where x is in radians).

math.degrees(x):

Converts angle x from radians to degrees.

math.radians(x):

Converts angle x from degrees to radians.

Inverse trigonometric functions: math.asin(), math.acos(), math.atan(), math.atan2().

Hyperbolic Functions:

math.sinh(x): Returns the hyperbolic sine of x.

math.cosh(x): Returns the hyperbolic cosine of x.

math.tanh(x): Returns the hyperbolic tangent of x.

Inverse hyperbolic functions: math.asinh(), math.acosh(), math.atanh().

Constants:

math.pi:

The number 𝜋 is a mathematical constant, approximately equal to 3.14159. After importing the math module, you just need to write math.pi to access the 𝜋 number.

Example:

import math

# Value of pi

print(math.pi)

print(f”Value of pi: {math.pi}”)

Output:

3.141592653589793

Value of pi: 3.141592653589793

math.e: The mathematical constant e (Euler’s number).

math.tau: The mathematical constant tau (τ).

math.inf: Positive infinity.

math.nan: Not a number.

Leave a Comment