Easy Tutorial
❮ Python Func Number Pow Python File Readline ❯

Python math Module

The Python math module provides many functions for mathematical operations on floating-point numbers.

Functions in the math module return values as floating-point numbers unless explicitly stated otherwise.

If you need to compute with complex numbers, use the corresponding functions in the cmath module.

To use math functions, you must import the module first:

import math

To view the contents of the math module:

>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']

math Module Constants

Constant Description
math.e Returns Euler's number (2.7182...)
math.inf Returns a floating-point positive infinity
math.nan Returns a floating-point NaN (not a number)
math.pi π generally refers to the mathematical constant Pi (3.1415...)
math.tau Mathematical constant τ = 6.283185..., accurate to available precision. Tau is a circle constant equal to 2π, the ratio of a circle's circumference to its radius.

math Module Methods

Method Description
math.acos(x) Returns the arc cosine of x, in radians.
math.acosh(x) Returns the inverse hyperbolic cosine of x.
math.asin(x) Returns the arc sine of x, in radians.
math.asinh(x) Returns the inverse hyperbolic sine of x.
math.atan(x) Returns the arc tangent of x, in radians.
math.atan2(y, x) Returns the arc tangent of y/x, in radians.
math.atanh(x) Returns the inverse hyperbolic tangent of x.
math.ceil(x) Rounds x up to the nearest integer.
math.comb(n, k) Returns the number of ways to choose k items from n items without repetition and without order.
math.copysign(x, y) Returns a float with the magnitude (absolute value) of x but the sign of y.
math.cos() Returns the cosine of x radians.
math.cosh(x) Returns the hyperbolic cosine of x.
math.degrees(x) Converts angle x from radians to degrees.
math.dist(p, q) Returns the Euclidean distance between points p and q, given as a sequence (or iterable) of coordinates. Both points must have the same dimensions.
math.erf(x) Returns the error function of a number.
math.erfc(x) Returns the complementary error function at x.
math.exp(x) Returns e raised to the power of x, where e = 2.718281... is the base of natural logarithms.
math.expm1() Returns e raised to the power of x minus 1, where e = 2.718281... is the base of natural logarithms. This is usually more accurate than math.e ** x or pow(math.e, x).
math.fabs(x) Returns the absolute value of x.
math.factorial(x) Returns the factorial of x. Raises a ValueError if x is not an integer or is negative.
math.floor() Rounds a number down to the nearest integer.
math.fmod(x, y) Returns the remainder of x/y.
math.frexp(x) Returns 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. If x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1.
math.fsum(iterable) Returns the sum of the elements in an iterable (tuple, array, list, etc.), with floating-point values.
math.gamma(x) Returns the Gamma function at x.
math.gcd() Returns the greatest common divisor of the given integer arguments.
math.hypot() Returns the Euclidean norm, sqrt(sum(x**2 for x in coordinates)). This is the length of the vector from the origin to the given coordinates.
math.isclose(a,b) Checks whether two values are close to each other; returns True if a and b are close, otherwise returns False.
math.isfinite(x) Determines if x is finite; returns True if x is neither infinite nor NaN, otherwise returns False.
math.isinf(x) Determines if x is infinite; returns True if x is positive or negative infinity, otherwise returns False.
math.isnan() Determines if a number is NaN; returns True if x is NaN (not a number), otherwise returns False.
math.isqrt() Rounds a square root number down to the nearest integer.
math.ldexp(x, i) Returns x * (2**i). This is essentially the inverse of the function math.frexp().
math.lgamma() Returns the natural logarithm of the absolute value of the Gamma function at x.
math.log(x[, base]) With one argument, returns the natural logarithm of x (base e).
math.log10(x) Returns the base-10 logarithm of x.
math.log1p(x) Returns the natural logarithm of 1+x (base e).
math.log2(x) Returns the base-2 logarithm of x.
math.perm(n, k=None) Returns the total number of ways to choose k items from n items without repetition and with order.
math.pow(x, y) Returns x raised to the power of y.
math.prod(iterable) Calculates the product of all elements in the iterable.
math.radians(x) Converts angle x from degrees to radians.
math.remainder(x, y) Returns the IEEE 754-style remainder of x divided by y.
math.sin(x) Returns the sine of x radians.
math.sinh(x) Returns the hyperbolic sine of x.
math.sqrt(x) Returns the square root of x.
math.tan(x) Returns the tangent of x radians.
math.tanh(x) Returns the hyperbolic tangent of x.
math.trunc(x) Returns the integer part of x, removing the fractional part.
❮ Python Func Number Pow Python File Readline ❯