If you are here for Python Mathematical functions, you are at the right place right now. In our previous chapters, we looked at the concepts of Python Operators and Python Operators precedence. You can make use of the operators to code your logic in any program. But for complex mathematical requirements, you might have to write complex code if you rely only on basic operators. However, you don’t need to rely on operators alone to build logic to find the factorial or square root of a number, we have pre-defined functions under the math module for the same.
Find the link here to all chapters of the Python tutorial: Learn Python
For those of you who don’t know, a module is more like a library of useful classes, functions, etc. to perform several operations easily. For example, using the math module, you can perform several mathematical operations in a python program. You must import the math module first to perform these mathematical operations easily in your python programs. Let us look at an example below.
import math #imports math module
a=math.sqrt(81) #stores square root of 81 in a variable
print(int(a)) #converts variable a to type int and prints the output
b=math.factorial(7) #stores factorial of 7 in b
print(b) #displays value stored in b
Output
9
5040
Similarly, we have several other mathematical functions to perform a wide range of mathematical operations easily in Python programs. Please find the list below
Python Mathematical Functions List
Math function name | Description |
pow(a,b) | Raises “a” value to the power of “b” Eg: pow(2,3) gives 8 |
sqrt(b) | Calculates the square root value of “b” Eg: sqrt(81) gives 9 |
log(b, [, base]) | Gives a natural logarithm of “b” of particularised base Eg: log(3,2) gives 1.5849625007211563 |
log10(b) | Gives base-10 logarithm of “b” Eg: log10(3) gives 0.47712125471966244 |
modf(b) | Gives float and integral parts of “b” Eg: modf(3.75) gives (0.75, 3.0) |
fsum(values) | Provides accurate sum of floating point values Eg: fsum([3.4,-7.3,2.3]) gives -1.6 |
fmod(a,b) | Gives remainder of division of a and b. While ‘%’ operator does the same function for integer values, fmod() obtains remainder for float numbers and is the preferred choice for this operation on float values. Eg: fmod(17.5,3) gives 2.5 |
factorial(b) | Provides the factorial value of “b”. It results in an error if the value is either negative or if it is not an integer number. Eg: factorial(7) gives 5040 |
fabs(b) | Provides the absolute value of “b” Eg: fabs(-5.75) gives 5.75 |
exp(n) | Gives exponentiation of “n”. It is equal to e **n Eg: exp(0.7) gives 2.0137527074704766 |
tan(n) | Gives the tangent value of “n” Eg: tan(0.7) gives 0.8422883804630794 |
cos(n) | Gives cosine value of “n” Eg: cos(0.7) gives 0.7648421872844885 |
sin(n) | Gives sine value of “n” Eg: sin(0.7) gives 0.644217687237691 |
radians(n) | Converts the value of “n” from degree to radians Eg: radians(270) gives 4.71238898038469 |
degrees(n) | Converts the angle value “n” from radians to degrees Eg: degrees(4.712388) gives 269.99994382809496 |
floor(n) | Decreases “n” value to the previous positive integer value. If “n” is an integer, the same value is returned. Eg: floor(3.7) gives 3 |
ceil(n) | Increases “n) value to the next higher integer value. If “n” is an integer, the same value is returned. Eg: ceil(5.7) gives 6 |
isnan(n) | Gives True if “n” is a NaN (not a number), and False in other cases. Eg:n=float(‘NaN’) #converts ‘NaN’ to float c=math.isnan(n) print(c) Output: True |
isinf(n) | Gives True if “n” is positive or negative infinity, and False in other cases. Eg: n=float(‘Inf’) c=math.isinf(n) print(c) Output: True |
trunc(n) | The real value of “n” gets truncated to an integer value and returned Eg: trunc(17.6576) gives 17 |
gcd(a,b) | Returns greatest common divisor of a and b Eg: gcd(20,25) gives 5 |
Python Math Constants
Using the python math module, you can also assign the values of constants in Python programs
Constant Name | Description |
pi | Mathematical Constant π = 3.141592653589793 with the best precision |
e | Mathematical Constant e= 2.718281828459045 with the best precision |
inf | floating-point positive infinity |
nan | A floating-point “not a number” (NaN) value. |
If you are someone who works with a lot of Mathematical expressions in your Python programs, you might need to bookmark this post as the information on this post will be useful for you. Also, we hope that you have understood about the Mathematical expressions. Please do not hesitate to share your views or ask your queries in the comments section below.