CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PYTHON - Maths

Python - math Module

Python math Module

Themathmodule is a built-in module in Python that is used for performing mathematical operations. This module provides various built-in methods for performing different mathematical tasks.

Note:Themathmodule’s methods do not work with complex numbers. For that, you can use thecmathmodule.

Importing math Module

Before using the methods of themathmodule, you need to import themathmodule into your code. The following is the syntax:

PYTHON CODE
import math

Methods of Python math Module

The following is the list of math module methods that we have categorized based on their functionality and usage.

Python’s standard library provides math module. This module includes many pre-defined functions for performing different mathematical operations. These functions do not work with complex numbers. There is a cmath module contains mathematical functions for complex numbers. Following are various functions available in the Python math module −

Math Module - Theoretic and Representation Methods

Python includes following theoretic and representation Functions in themathmodule −

Sr.No.Function & Description1math.ceil(x)The ceiling of x: the smallest integer not less than x2math.comb(n,k)This function is used to find the returns the number of ways to choose “x” items from “y” items without repetition and without order.3math.copysign(x, y)This function returns a float with the magnitude (absolute value) of x but the sign of y.4math.cmp(x, y)This function is used to compare the values of to objects. This function is deprecated in Python3.5math.fabs(x)This function is used to calculate the absolute value of a given integer.6math.factorial(n)This function is used to find the factorial of a given integer.7math.floor(x)This function calculates the floor value of a given integer.8math.fmod(x, y)The fmod() function in math module returns same result as the”%“operator. However fmod() gives more accurate result of modulo division than modulo operator.9math.frexp(x)This function is used to calculate the mantissa and exponent of a given number.10math.fsum(iterable)This function returns the floating point sum of all numeric items in an iterable i.e. list, tuple, array.11math.gcd(*integers)This function is used to calculate the greatest common divisor of all the given integers.12math.isclose()This function is used to determine whether two given numeric values are close to each other.13math.isfinite(x)This function is used to determine whether the given number is a finite number.14math.isinf(x)This function is used to determine whether the given value is infinity (+ve or, -ve).15math.isnan(x)This function is used to determine whether the given number is “NaN”.16math.isqrt(n)This function calculates the integer square-root of the given non negative integer.17math.lcm(integers)This function is used to calculate the least common factor of the given integer arguments.18math.ldexp(x, i)This function returns product of first number with exponent of second number. So, ldexp(x,y) returns x2**y. This is inverse of frexp() function.19math.modf(x)This returns the fractional and integer parts of x in a two-item tuple.20math.nextafter(x, y, steps)This function returns the next floating-point value after x towards y.21math.perm(n, k)This function is used to calculate the permutation. It returns the number of ways to choose x items from y items without repetition and with order.22math.prod(iterable, , start)This function is used to calculate the product of all numeric items in the iterable (list, tuple) given as argument.23math.remainder(x,y)This function returns the remainder of x with respect to y. This is the difference x − ny, where n is the integer closest to the quotient x / y.24math.trunc(x)This function returns integral part of the number, removing the fractional part. trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x.25math.ulp(x)This function returns the value of the least significant bit of the float x. trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x.

Math Module - Power and Logarithmic Methods

Sr.No.Function & Description1math.cbrt(x)This function is used to calculate the cube root of a number.2math.exp(x)This function calculate the exponential of x: ex3math.exp2(x)This function returns 2 raised to power x. It is equivalent to 2x.4math.expm1(x)This function returns e raised to the power x, minus 1. Here e is the base of natural logarithms.5math.log(x)This function calculates the natural logarithm of x, for x> 0.6math.log1p(x)This function returns the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.7math.log2(x)This function returns the base-2 logarithm of x. This is usually more accurate than log(x, 2).8math.log10(x)The base-10 logarithm of x for x> 0.9math.pow(x, y)The value of xy.10math.sqrt(x)The square root of x for x > 0

Math Module - Trigonometric Methods

Python includes following functions that perform trigonometric calculations in themathmodule −

Sr.No.Function & Description1math.acos(x)This function returns the arc cosine of x, in radians.2math.asin(x)This function returns the arc sine of x, in radians.3math.atan(x)This function returns the arc tangent of x, in radians.4math.atan2(y, x)This function returns atan(y / x), in radians.5math.cos(x)This function returns the cosine of x radians.6math.sin(x)This function returns the sine of x radians.7math.tan(x)This function returns the tangent of x radians.8math.hypot(x, y)This function returns the Euclidean norm, sqrt(xx + yy).

Math Module - Angular conversion Methods

Following are the angular conversion function provided by Pythonmathmodule −

Sr.No.Function & Description1math.degrees(x)This function converts the given angle from radians to degrees.2math.radians(x)This function converts the given angle from degrees to radians.

Math Module - Mathematical Constants

The Pythonmathmodule defines the following mathematical constants −

Sr.No.Constants & Description1math.piThis represents the mathematical constant pi, which equals to”3.141592…“to available precision.2math.eThis represents the mathematical constant e, which is equal to”2.718281…“to available precision.3math.tauThis represents the mathematical constant Tau (denoted by ). It is equivalent to the ratio of circumference to radius, and is equal to2.4math.infThis represents positive infinity. For negative infinity use”−math.inf”.5math.nanThis constant is a floating-point “not a number” (NaN) value. Its value is equivalent to the output of float(‘nan’).

Math Module - Hyperbolic Methods

Hyperbolic functions are analogs of trigonometric functions that are based on hyperbolas instead of circles. Following are the hyperbolic functions of the Pythonmathmodule −

Sr.No.Function & Description1math.acosh(x)This function is used to calculate the inverse hyperbolic cosine of the given value.2math.asinh(x)This function is used to calculate the inverse hyperbolic sine of a given number.3math.atanh(x)This function is used to calculate the inverse hyperbolic tangent of a number.4math.cosh(x)This function is used to calculate the hyperbolic cosine of the given value.5math.sinh(x)This function is used to calculate the hyperbolic sine of a given number.6math.tanh(x)This function is used to calculate the hyperbolic tangent of a number.

Math Module - Special Methods

Following are the special functions provided by the Pythonmathmodule −

Sr.No.Function & Description1math.erf(x)This function returns the value of the Gauss error function for the given parameter.2math.erfc(x)This function is the complementary for the error function. Value of erf(x) is equivalent to1-erf(x).3math.gamma(x)This is used to calculate the factorial of the complex numbers. It is defined for all the complex numbers except the non-positive integers.4math.lgamma(x)This function is used to calculate the natural logarithm of the absolute value of the Gamma function at x.

Example Usage

The following example demonstrates the use of math module and its methods:

PYTHON CODE
# Importing math Module
import math

# Using methods of math module
print(math.sqrt(9))
print(math.pow(3, 3))
print(math.exp(1))
print(math.log(100, 10))

print(math.factorial(4))
print(math.gcd(12, 3))

Output

PYTHON CODE
3.0
27.0
2.718281828459045
2.0
24
3