Python Math Module With Examples

Python Math Module With Examples

In this blog post, we are going to discuss about python  math module  that plays vital role in computing mathematical problems. math() module is the python built-in module provides access to the mathematical functions defined by the C standard. We will be discussing some important mathematical functions provided by math module one by one.

For using math module in program, simply import the module the header section of the python program using

import math

All the examples shown below have been done in  python version 3.9.4 . For checking your python version simply enter this command in your working terminal

C:/>python --version

Note: All the return values of mathematical functions in python math module are of float types except when explicitly noted

 

Mathematical Functions

Various mathematical functions provided by math module which makes our work a way lot better and easier are:

math.floor(x)

This function returns the largest integer value less than or equal to x that is floor function will always give the lowest integer value of given number x. If input is integer, then the same integer number is returned.

>>> import math
>>> math.floor(2.7)
2
>>> math.floor(2.5)
2
>>> math.floor(2.2)
2

 

math.ceil(x)

This function returns the smallest integer value greater than or equal to x i.e ceil function will always give the highest integer value of given number x. If input is integer, then the same integer number is returned.

>>> import math
>>> math.floor(2.7)
3
>>> math.floor(2.5)
3
>>> math.floor(2.2)
3

math.sqrt(x)

This function returns the square root in float of the given number x whether it is integer or float. The output is always in float whether input is integer of float.

>>> import math
>>> math.sqrt(25)
5.0
>>> math.sqrt(225.0)
15.0
>>> math.sqrt(1457)
38.17066936798463
>>> math.sqrt(0.36)
0.6

 

math.pow(x, y)

This function returns the x raised to the power y. Alternative to this function is built-in  ** operator . We can use any of them and they both return value in float type.

>>> import math
>>> math.pow(2, 3)
8.0
>>> math.pow(5.5, 2)
30.25
>>> math.pow(2.5, 4)
39.0625
>>> math.pow(2.6, 2.2)
8.183542939727342
>>> 2.6 ** 2.2
8.183542939727342

math.factorial(x)

This function returns the factorial of x as integer value. It throws ValueError error, if the input number is not integer or is negative.

>>> import math
>>> math.factorial(0)
1
>>> math.factorial(5)
120
>>> math.factorial(12)
479001600

 

math.fabs(x)

This function returns the absolute value of x.

>>> import math
>>> math.fabs(-4.5)
4.5
>>> math.fabs(-77)
77.0
>>> math.fabs(7654)
7654.0

 

math.gcd(*integers)

This function returns the greatest common divisor for any number of integers. Previously below python version 3.9, this function takes only two arguments.

Note: if do not place any arguments, then gcd() returns 0

>>> import math
>>> math.gcd(4, 8)
4
>>> math.gcd(12,24,6)
6
>>> math.gcd(45,56,78)
1
>>> math.gcd()
0

 

math.lcm(*integers)

This function returns the least common multiple for any number of integers. lcm() is introduced from python version 3.9

Note: if do not place any arguments, then lcm() returns 1

>>> import math
>>> math.lcm(7, 49)
49
>>> math.lcm(5,55,255)
2805
>>> math.lcm()
1

 

math.copysign(x, y)

This function returns the absolute float value of x but with the sign of y.

>>> import math
>>> math.copysign(-4.5, 7)
4.5
>>> math.copysign(-4, -7)
-4.0
>>> math.copysign(-9.8, -0.0)
-9.8

 

math.perm(n, k=None)

This function returns the number of ways to choose k number of items from n items such that k <= n with order and without repetition.

Note: perm( ) returns TypeError if any of the arguments are not integers and if any argument value is negative it raises ValueError. If we do not mention the second argument value, it simply determines the  factorial of n(n!) .

>>> import math
>>> math.perm(4, 2)
12
>>> math.perm(4)
24
>>> math.perm(4.0, 2.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
>>> math.perm(4, -2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: k must be a non-negative integer

 

math.fmod(x, y)

This function returns the remainder of x / y. We also have x % y in python, but it does not give same result as of fmod( ). fmod( ) is preferred when working with floats where x % y is preferred when working with integers.

>>> import math
>>> math.fmod(75, 7)
5.0
>>> math.fmod(78.5, 8.5)
2.0
>>> 78.5 % 8.5
2.0
>>> 10 % 3
1

 

math.frexp(x)

This function returns the mantissa and the exponent of x in the form of (m, e) where m is float type and e is integer type. x is equal to

m * (2 ** e) .

>>> import math
>>> math.frexp(9)
(0.5625, 4)
>>> math.frexp(8.0)
(0.5, 4)
>>> math.frexp(256)
(0.5, 9)

 

math.fsum(iterable)

This function returns the sum of all items in any type of iterable such lists or tuples or sets.

>>> import math
>>> math.fsum([0.5, 0.5, 1, 2.0, 1])
5.0
>>> math.fsum((0.5, 0.5, 1, 2.0, 1))
5.0
>>> math.fsum({1, 2, 3, 4, 5})
15.0

 

math.prod(iterable, start=1)

This function returns the product of all items in any type of iterable such lists or tuples or sets. The default start value is 1. If we input empty iterable, then the output is start value.

>>> import math
>>> math.prod([1, 2, 3], start=4)
24
>>> math.prod([])
1
>>> math.prod((4, 5, 6), start=1)
120
>>> math.prod({1, 2, 3})
6

math.isfinite(x)

This function returns true if x is finite number else returns false.

>>> import math
>>> math.isfinite(0.0)
True
>>> math.isfinite(22/7)
True
>>> math.isfinite(float('nan'))
False
>>> math.isfinite(float('inf'))
False
>>> math.isfinite(float('-inf'))
False

 

math.isnan(x)

This function returns true if x in NaN(Not a Number) else false.

>>> import math
>>> math.isnan(float('nan'))
True
>>> math.isnan(78.7)
False
>>> math.isnan(float('inf'))
False

 

math.exp(x)

This function returns the value e ** x where e is the base of natural logarithms which equal to 2.7182818….

>>> import math
>>> math.exp(2)
7.38905609893065
>>> math.exp(0.69)
1.9937155332430823
>>> math.exp(6)
403.4287934927351

 

math.dist(p, q)

This function returns the euclidean distance between points p and q.

>>> import math
>>> math.dist([4, 5], [6, 7])
2.8284271247461903
>>> math.dist((2.5, 3), (3.5, 5.5))
2.6925824035672523

 

Logarithmic Functions

math.log(x)

This function with one argument returns the natural logarithm of number x. With two arguments i.e log(x, y) returns the logarithm of x to the given base y.

>>> import math
>>> math.log(4)
1.3862943611198906
>>> math.log(4, 2)
2.0

 

math.log1p(x)

This function returns the natural logarithm of 1 + x.

>>> import math
>>> math.log1p(4)
1.6094379124341003
>>> math.log1p(15)
2.772588722239781

 

math.log2(x)

This function returns the base-2 logarithm of x.

>>> import math
>>> math.log2(55)
5.78135971352466
>>> math.log2(77)
6.266786540694901

 

math.log10(x)

This function returns the base-10 logarithm of x.

>>> import math
>>> math.log10(77)
1.8864907251724818
>>> math.log10(10)
1.0
>>> math.log10(100)
2.0

 

Trigonometric Functions

math.asin(x) : Returns the arc sine of x in radians -1 and 1 .

math.atan(x) : Returns the arc tangent of x in radians between -PI/2 and PI/2.

math.acos(x) : Returns the arc cosine of x in radians between -1 and 1.

math.sin(x) : Returns the sine of x in radians.

math.cos(x) : Returns the cosine of x in radians.

math.tan(x) : Returns the tangent of x in radians.

>>> import math
>>> math.atan(60)
1.554131203080956
>>> math.acos(1)
0.0
>>> math.asin(1)
1.5707963267948966
>>> math.sin(45)
0.8509035245341184
>>> math.cos(45)
0.5253219888177297
>>> math.tan(90)
-1.995200412208242

 

Constant Functions

math.pi : Returns the value of PI.

math.e : Returns the value of mathematical constant e.

math.tau : Returns the value of tau(a circle constant eqaul to 2*PI).

math.inf : Returns the floating-point positive infinity. For negative infinity use -math.inf.

math.nan : Returns a floating-point not a number(NaN) value.

>>> import math
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045
>>> math.tau
6.283185307179586
>>> math.inf
inf
>>> -math.inf
-inf
>>> math.nan
nan

 

Conversion Functions

math.degrees(x)

This function converts the angle x from radians to degrees.

>>> import math
>>> math.degrees(45)
2578.3100780887044
>>> math.degrees(22/7)
180.07244989825872

 

math.radians(x)

This function converts the angle x from degrees to radians.

>>> import math
>>> math.radians(45)
0.7853981633974483
>>> math.radians(22/7)
0.05485320506267893
>>> math.radians(90)
1.5707963267948966

 

Conclusion

We discussed almost all python math module functions. This module helps to perform the mathematical tasks easily and we have to take the advantage of it, by using it wisely rather than making own mathematical functions. As the functions provided by this module is more accurate than created manually one, we should familiar to use those functions in python program.

If you want to learn more about other functions then visit the python official documentation of math module.

Happy Learning 🙂

 

Leave a Reply