Python Math Module – Python for Mathematics
The majority of Python development involves mathematical calculations. It is impossible to avoid the need for math when working on a scientific project, a financial application, or any other type of programming project that you are undertaking.
Python’s built-in mathematical operators, such as addition (+), subtraction (-), division (/), and multiplication (*), can be used for simple mathematical calculations. However, more advanced operations like exponential, logarithmic, trigonometric, or power functions are not included. Does this imply that you must recreate all of these functions?
No, thankfully. Python includes a math module that is specifically designed for higher-level mathematical operations.
What is the Python math Module?
The math module in Python provides mathematical functions and constants, including trigonometric functions, logarithmic functions, and other mathematical constants. Some examples of functions provided by the math module include sqrt() for finding the square root of a number, sin() and cos() for trigonometry, and exp() for exponentiation.
Additionally, the math module also provides constants such as pi and e. To use the functions and constants provided by the math module, you need to import it using the import statement.
Example:
import math x = math.sqrt(16) print(x) # 4.0 y = math.sin(math.pi) print(y) # 1.2246467991473532e-16 z = math.exp(2) print(z) # 7.389056098930649
You can also import specific functions or constants from the math module using the from keyword.
Example:
from math import sqrt, pi x = sqrt(16) print(x) # 4.0 y = pi print(y) # 3.141592653589793
Constants of python math module
The math module in Python provides several mathematical constants that can be used in mathematical calculations. Some of these constants include:
- math.pi: The mathematical constant pi (π), which is the ratio of a circle’s circumference to its diameter.
- math.e: The mathematical constant e, which is the base of the natural logarithm.
- math.tau: The mathematical constant tau (τ), which is equal to 2*pi.
- math.inf: A special floating-point value that represents positive infinity.
- math.nan: A special floating-point value that represents “not a number.”
import math print(math.pi) # 3.141592653589793 print(math.e) # 2.718281828459045 print(math.tau) # 6.283185307179586
Note that these are read-only constants and cannot be reassigned, if you try to reassign these constants it will raise a SyntaxError.
Functions in Python math Module
Function | Description |
ceil(x) | Returns the smallest integer greater than or equal to x. |
isinf(x) | Returns True if x is a positive or negative infinity |
isnan(x) | Returns True if x is a NaN |
exp(x) | Returns e**x |
log10(x) | Returns the base-10 logarithm of x |
log(x[, b]) | Returns the logarithm of x to the base b (defaults to e) |
modf(x) | Returns the fractional and integer parts of x |
isfinite(x) | Returns True if x is neither an infinity nor a NaN (Not a Number) |
factorial(x) | Returns the factorial of x |
ldexp(x, i) | Returns x * (2**i) |
pow(x, y) | Returns x raised to the power y |
copysign(x, y) | Returns x with the sign of y |
fabs(x) | Returns the absolute value 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 |
trunc(x) | Returns the truncated integer value of x |
expm1(x) | Returns e**x – 1 |
log1p(x) | Returns the natural logarithm of 1+x |
log2(x) | Returns the base-2 logarithm of x |
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…) |
Functions available in python’s math module
1. math.sqrt(x): Returns the square root of x.
Example:
import math print(math.sqrt(16)) # Output: 4.0
2. math.floor(x): Returns the largest integer less than or equal to x.
Example:
import math print(math.floor(3.14)) # Output: 3
3. math.ceil(x): Returns the smallest integer greater than or equal to x.
Example:
import math print(math.ceil(3.14)) # Output: 4
4. math.exp(x): Returns e raised to the power of x.
Example:
import math print(math.exp(1)) # Output: 2.718281828459045
5. math.log(x[, base]): Returns the natural logarithm of x, or the logarithm of x to base if specified.
Example:
import math print(math.log(100)) # Output: 4.605170185988092 print(math.log(100, 10)) # Output: 2.0
6. math.log10(x): Returns the base-10 logarithm of x.
Example:
import math print(math.log10(100)) # Output: 2.0
7. math.sin(x): Returns the sine of x (measured in radians).
Example:
import math print(math.sin(math.pi / 2)) # Output: 1.0
8. math.cos(x): Returns the cosine of x (measured in radians).
Example:
import math print(math.cos(0)) # Output: 1.0
9. math.tan(x): Returns the tangent of x (measured in radians).
Example:
import math print(math.tan(math.pi / 4)) # Output: 1.0
10. math.asin(x): Returns the arcsine of x, in radians.
Example:
import math print(math.asin(1)) # Output: 1.5707963267948966
11. math.acos(x): Returns the arccosine of x, in radians.
Example:
import math print(math.acos(0)) # Output: 1.5707963267948966
12. math.atan(x): Returns the arctangent of x, in radians.
Example:
import math print(math.atan(1)) # Output: 0.7853981633974483
13. math.degrees(x): Converts angle x from radians to degrees.
Example:
import math print(math.degrees(math.pi)) # Output: 180.0
14. math.radians(x): Converts angle x from degrees to radians.
Example:
import math print(math.radians(180)) # Output: 3.141592653589793
15. math.pi: The mathematical constant pi (3.14159…).
Example:
import math print(math.pi) # Output: 3.141592653589793
16. math.e: The mathematical constant e (2.71828…).
Example:
import math print(math.e) # Output: 2.718281828459045
17. math.inf: Positive infinity.
Example:
import math print(math.inf) # Output: inf
18. math.isinf(x): Returns True if x is a positive or negative infinity, False otherwise.
Example:
import math print(math.isinf(math.inf)) # Output: True
19. math.isnan(x): Returns True if x is NaN (Not a Number), False otherwise.
Example:
import math print(math.isnan(math.sqrt(-1))) # Output: True
20. math.copysign(x, y): Returns a float with the magnitude (absolute value) of x but the sign of y.
Example:
import math print(math.copysign(10, -1)) # Output: -10.0
Conclusion
The math module in Python provides a wide range of mathematical functions and constants that can be used to perform various mathematical calculations and operations. The module includes basic mathematical functions such as square root, trigonometric functions, logarithmic functions, and others, as well as mathematical constants such as pi and e.
To use the functions and constants provided by the math module, you need to import it using the import statement. The math module is a built-in Python library, so it does not need to be installed separately. In conclusion, the math module in Python is a powerful tool for performing mathematical calculations and operations, and it is widely used in various scientific, engineering, and mathematical applications.