When working on financial or scientific projects, it is sometimes necessary to incorporate complexÂ mathematical calculations into the project. Python has a math module that can handle similar calculations. PythonÂ Math functionsÂ are a set of pre-defined mathematical actions with general implicit values that can be directly accessed for the program’s mathematical activities. Python’s math module allows you to conduct common and useful mathematical computations and functions within your application. In this article, we’ll go over all of the Python math functions and how to use them in your code.
What is math module in Python?
Python math module is a standard module that is always accessible in Python to perform simple mathematical calculations. To utilize the mathematical functions in this module, you must first import it with import math. Importing math in Python provides access to the Python math functions provided by the C standard. Any math function in Python can be accessed by using a math class object.
Example
import math print('Square root of 25 is:', math.sqrt(25)) print('Value of pi is:', math.pi)
Output
Square root of 25 is: 5.0 Value of pi is: 3.141592653589793
Complex data types are not supported by this module. The complex counterpart is the cmath module.
Functions in Python Math Module
All key Python math functions are deeply described below,
- Constants – Python’s math module includes a number of preset constants. Having access to these constants has a number of advantages. For starters, you won’t have to manually hardcode them into your program, which will save you a significant amount of time. Furthermore, they ensure consistency throughout your code. Examples, Eulerâ€™s number, pi, infinity, tau, etc.
- Logarithmic functions – A logarithm is the inverse of exponentiation. To determine the logarithm value of any given number x, the exponent of another fixed number with base b is determined. Examples, log, exponential, power, sqrt, etc.
- Numeric functions – All mathematical inceptions can be calculated using the numeric functions.
- Trigonometric functions – Trigonometric functions are used in mathematics to describe the point of view of a right-angled triangle with two side lengths. These Python math functions include sin, cos, tan, etc.
The following is a list of all the functions and attributes specified in the math module, along with a brief description of what they perform.
Function |
Description |
ceil(x) | The lowest integer bigger than or equal to x is returned. |
copysign(x, y) | Returns a number with the value ‘x’ but the sign ‘y.’ |
fabs(x) | The absolute value of the number is returned. |
factorial(x) | The factorial of the number is returned. |
floor(x) | The greatest integer less than or equal to x is returned. |
fmod(x, y) | When x is divided by y, the residual is returned. |
frexp(x) | As a pair (m, e), this function returns the mantissa and exponent of x. |
fsum(iterable) | Returns an accurate floating-point sum of the iterable’s values. |
isfinite(x) | Check that the value is not infinity or Nan. |
isinf(x) | Check to see if the value is infinite or not. |
isnan(x) | If the number is “nan,” returns true; otherwise, returns false. |
ldexp(x, i) | x * (2**i) is returned. |
modf(x) | The fractional and integer components of x are returned. |
trunc(x) | The shortened integer value of x is returned. |
exp(x) | e**x is the result. |
expm1(x) | e**x – 1 is returned. |
log(x[, b]) | The logarithmic value of a with base b is returned. |
log1p(x) | 1+x’s natural logarithmic value is returned. |
log2(x) | Calculates the value of log a with base 2. |
log10(x) | This function computes the value of log a with a base of ten. |
pow(x, y) | Calculates the value of x raised to the power of y, i.e. (x**y). |
sqrt(x) | The square root of x is returned. |
acos(x) | The arc cosine of x is returned. |
asin(x) | The arc sine of x is returned. |
atan(x) | The arc tangent of x is returned. |
atan2(y, x) | atan(y / x) is returned. |
cos(x) | The cosine of x is returned. |
hypot(x, y) | Returns the Euclidean norm – sqrt(x*x + y*y) |
sin(x) | The sine of x is returned. |
tan(x) | The tangent of x is returned. |
degrees(x) | Angle x is converted from radians to degrees. |
radians(x) | Angle x is converted from degrees to radians. |
acosh(x) | The inverse hyperbolic cosine of the value supplied as input is returned. |
asinh(x) | The inverse hyperbolic sine of the value supplied as input is returned. |
atanh(x) | The inverse hyperbolic tangent of the value supplied as input is returned. |
cosh(x) | The hyperbolic cosine of the value supplied as an argument is returned. |
sinh(x) | The hyperbolic sine of the value supplied as an argument is returned. |
tanh(x) | The hyperbolic tangent of the value supplied as input is returned. |
erf(x) | The error function at x is returned. |
erfc(x) | The complementary error function at x is returned. |
gamma(x) | The Gamma function at x is returned. |
lgamma(x) | Returns the natural logarithm of the absolute value of the Gamma function at x |
pi | Mathematical constant, the circumference to diameter ratio of a circle (3.14159…) |
e | e is the mathematical constant (2.71828…) |
Few Python math function examples are as follows:
import math print('Power of number:', math.pow(2, 2)) print('Floor value is:', math.floor(12.5473))Â print('Ceil value is:', math.ceil(11.5473))Â print('Factorial of number:', math.factorial(5))Â print('modf of number:', math.modf(10.45))
Output
Power of number: 4.0 Floor value is: 12 Ceil value is: 12 Factorial of number: 120 modf of number: (0.4499999999999993, 10.0)
Frequently Asked Questions
Q1. What are math functions in Python?
When working on financial or scientific projects, it is sometimes necessary to incorporate complexÂ mathematical calculations into the project. Python has a math module that can handle similar calculations. PythonÂ Math functionsÂ are a set of pre-defined mathematical actions with general implicit values that can be directly accessed for the program’s mathematical activities. Python’s math module allows you to conduct common and useful mathematical computations and functions within your application. All key Python math functions are deeply described below,
- Constants – Python’s math module includes a number of preset constants. Having access to these constants has a number of advantages. For starters, you won’t have to manually hardcode them into your program, which will save you a significant amount of time. Furthermore, they ensure consistency throughout your code. For Example, Eulerâ€™s number, pi, infinity, tau, etc.
- Logarithmic functions – A logarithm is the inverse of exponentiation. To determine the logarithm value of any given number x, the exponent of another fixed number with base b is determined. For Example, log, exponential, power, sqrt, etc.
- Numeric functions – All mathematical inceptions can be calculated using the numeric functions.
- Trigonometric functions – Trigonometric functions are used in mathematics to describe the point of view of a right-angled triangle with two
Q2. How do you use math in Python?
Python math module is a standard module that is always accessible in Python to perform simple mathematical calculations. To utilize the mathematical functions in this module, you must first import it with import math. Importing math in Python provides access to the Python math functions provided by the C standard. Any math function in Python can be accessed by using a math class object.
Example
import math print('Square root of 100 is:', math.sqrt(25)) print('Value of pi is:', math.pi)
Output
Square root of 100 is: 10.0 Value of pi is: 3.141592653589793
Leave a Reply