Python Mathematical Functions
The math module is a standard module in Python and is always available. To use mathematical functions under this module, you have to import the module using import math .
It gives access to the underlying C library functions. For example,
# Square root calculation import math math.sqrt(4)
This module does not support complex datatypes. The cmath module is the complex counterpart.
Functions in Python Math Module
Here is the list of all the functions and attributes defined in math module with a brief explanation of what they do.
Function | Description |
---|---|
ceil(x) | Returns the smallest integer greater than or equal to x. |
copysign(x, y) | Returns x with the sign of y |
fabs(x) | Returns the absolute value of x |
factorial(x) | Returns the factorial of x |
floor(x) | Returns the largest integer less than or equal to x |
fmod(x, y) | Returns the remainder when x is divided by y |
frexp(x) | Returns the mantissa and exponent of x as the pair (m, e) |
fsum(iterable) | Returns an accurate floating point sum of values in the iterable |
isfinite(x) | Returns True if x is neither an infinity nor a NaN (Not a Number) |
isinf(x) | Returns True if x is a positive or negative infinity |
isnan(x) | Returns True if x is a NaN |
ldexp(x, i) | Returns x * (2**i) |
modf(x) | Returns the fractional and integer parts of x |
trunc(x) | Returns the truncated integer value of x |
exp(x) | Returns e**x |
expm1(x) | Returns e**x — 1 |
log(x[, b]) | Returns the logarithm of x to the base b (defaults to e) |
log1p(x) | Returns the natural logarithm of 1+x |
log2(x) | Returns the base-2 logarithm of x |
log10(x) | Returns the base-10 logarithm of x |
pow(x, y) | Returns x raised to the power y |
sqrt(x) | Returns the square root of x |
acos(x) | Returns the arc cosine of x |
asin(x) | Returns the arc sine of x |
atan(x) | Returns the arc tangent of x |
atan2(y, x) | Returns atan(y / x) |
cos(x) | Returns the cosine of x |
hypot(x, y) | Returns the Euclidean norm, sqrt(x*x + y*y) |
sin(x) | Returns the sine of x |
tan(x) | Returns the tangent of x |
degrees(x) | Converts angle x from radians to degrees |
radians(x) | Converts angle x from degrees to radians |
acosh(x) | Returns the inverse hyperbolic cosine of x |
asinh(x) | Returns the inverse hyperbolic sine of x |
atanh(x) | Returns the inverse hyperbolic tangent of x |
cosh(x) | Returns the hyperbolic cosine of x |
sinh(x) | Returns the hyperbolic cosine of x |
tanh(x) | Returns the hyperbolic tangent of x |
erf(x) | Returns the error function at x |
erfc(x) | Returns the complementary error function at x |
gamma(x) | Returns the Gamma function at x |
lgamma(x) | Returns the natural logarithm of the absolute value of the Gamma function at x |
pi | Mathematical constant, the ratio of circumference of a circle to it’s diameter (3.14159. ) |
e | mathematical constant e (2.71828. ) |
Visit this page to learn about all the mathematical functions defined in Python 3.
Table of Contents
Python Math
Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers.
Built-in Math Functions
The min() and max() functions can be used to find the lowest or highest value in an iterable:
Example
x = min(5, 10, 25)
y = max(5, 10, 25)
The abs() function returns the absolute (positive) value of the specified number:
Example
The pow(x, y) function returns the value of x to the power of y (x y ).
Example
Return the value of 4 to the power of 3 (same as 4 * 4 * 4):
The Math Module
Python has also a built-in module called math , which extends the list of mathematical functions.
To use it, you must import the math module:
When you have imported the math module, you can start using methods and constants of the module.
The math.sqrt() method for example, returns the square root of a number:
Example
The math.ceil() method rounds a number upwards to its nearest integer, and the math.floor() method rounds a number downwards to its nearest integer, and returns the result:
Example
x = math.ceil(1.4)
y = math.floor(1.4)
print(x) # returns 2
print(y) # returns 1
The math.pi constant, returns the value of PI (3.14. ):
Example
Complete Math Module Reference
In our Math Module Reference you will find a complete reference of all methods and constants that belongs to the Math module.
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.