What is the math module in Python?

The math module is a built-in Python library. It helps in numerous mathematical functions following the C standard.

This works for real numbers only.

svg viewer

Basic functions

Functions Description
math.ceil( value ) It returns the smallest integer greater than or equal to value.
math.copysign( value1,value2 ) It returns the float with magnitude of value1 and sign of value2.
math.fabs( value ) It returns the absolute of value.
math.factorial( value ) It returns the factorial of value.
math.floor( value ) It returns the largest integer greater than or equal to value.
math.fsum( iterable ) It returns the float sum of the values of the iterable.

Examples

# Importing Math library.
import math
#ceiling function
print(math.ceil(10.555))
#factorial function
print(math.factorial(3))

Other functions

LogarithmicTrigonometricPi constantExponentialPowerSquare Root

There are numerous other functions as well:

  • Logarithmic function: Returns the natural logarithm of a specified number.
  • Exponential function: Returns a float number after raising e to a specified number.
  • Square root function: Returns the square root of a specified number.
# Importing Math library.
import math
# tangent function
print(math.atan(90))
# power function
print(math.pow(3,2))
# pi constant
print(math.pi)
Copyright ©2024 Educative, Inc. All rights reserved