Python Math Module

Python have 5 built-in data types one of which is Numbers which can be integer(10, 29, -10), Floats(19.29, -29.12) or even complex( 20 + 28i ). Although there are some common mathematical functions built into language itself. But there does exist a specific Module containing many functions to do Mathematical Operations on Numbers data types in Python – Math Python Module.

First question would be How to import Math module in Python? You can import python’s math module into code by using import math statement and to access functions inside it do math.function_name(parameter). Like math.ceil(19.29).

Below are some tables explaining what are functions inside Math module with some examples for each.

Examples of using Math Module in Python Code

# import math module
import math

# Print out Mathematical Exponent, Pie constant
print(math.e)
print(math.pi)

# Above code prints out
2.718281828459045
3.141592653589793

Numaric Functions Inside Python Math Module

Table containing numerical functions inside Math Python Module with description and examples for each one.

FunctionDescription
math.ceil(x)Returns smallest integer grater than or equal to x.
For example – math.ceil(19.29) will return 20
math.comb(x, y)Returns number of ways to choose y items from x items without repetition and order.
For example – math.comb(29, 2) will return 406 as there exist 406 ways to choose 2 items from a total of 29 items.
math.copysign(x, y)Returns a float having value of x but sign of y.
For example – math.copysign(29, -9) will return -29.0
math.fabs(x)Just returns absolute value of x.
For example – math.fabs(-29) will return 29
math.factorial(x)Returns factorial value of x.
For example – math.factorial(3) will return 6
math.floor(x)Returns largest integer less than or equal to x
math.fmod(x, y)Returns remainder left after division of x by y.
For example – math.fmod(39, 2) will be 1 as 18*2+1 = 36
math.frexp(x)Returns mantissa and exponent of x as a pair(a,b) where a is float and b is integer.
For example – math.frexp(9) will return (0.5625, 4)
math.gcd(x, y, …)Returns gratest common division of all numbers passed to it.
For example – math.gcd(10, 2) will return 2
math.isclose(x, y)Checks whether passed numbers x, y are close to each other or not and returns true/false accordingly.
For example – math.isclose(10, 2) will return false
math.isfinite(x)If x is not inifinity or NaN then returns true, false otherwise.
For example
math.isfinite(10) will return true
math.isfinite(NaN) will return false
math.isinf(x)If x is postivie or negative infinity then it will return true, false otherwise.
For example – math.isinf(∞) will return true
math.isnan(x)If x is NaN then it will return true, false otherwise.
For example – math.isnan(NaN) will return true
math.isqrt(x)Return integer square root of nonnegative integer x.
For example – math.isqrt(9) will return 3
math.lcm(x, y, ……)Return least common mutiple of x, y, …….
For example – math.lcm(10, 2, 8) will return 40
math.ldexp(x,y)Returns x * (2**y)
For example – math.ldexp(100, 29) will return 53687091200.0
math.modf(x)Returns fractional and integer parts of x, both results will be having sign of x and will be floats as well.
For example – math.modf(19.02022) will return
(0.02020999999999873, 19.0)
math.nextafter(x, y)Returns the next floating-point value after x towards y.
For example – math.nextafter(10, 12) will return 10.0000000000000002
math.perm(x, y)Returns number of ways to select y items from n items without repetition and with order.
For example – math.perm(19, 2) will return 342
math.prod()Returns remainder of two numbers.
For example – math.prod(100, 3) will return 1.0
math.trunc(x)Returns integer by truncationg x number.
For example – math.trunc(100.272) will return 100
math.ulp(x)Return value of least significant bit of float x.
For example – math.ulp(1) will return 2.220446049250313e – 16

Logarithmic Functions inside Python Math Module

Table containing logarithmic functions inside Math Python Module with description and examples for each one.

FunctionDescription
math.exp(x)Return e raised to power x that is ex
(here e is mathematical exponent and its value is 2.718281….. )
For example – math.exp(3) will return 20.085536923187668
math.expm1(x)Return e raised to power x and then minus 1
that is ex – 1
(here e is mathematical exponent and its value is 2.718281….. )
For example – math.exp(3) will return 19.085536923187668
math.log(x, base)If base is provide then will return log(x)/log(base) value, otherwise will return only log(x) value.
For example – math.log(10) will return 2.302585092994046 while math.log(10, 3) will return 2.095903274289385
math.log1p(x)Returns natural logarithm of 1+x with base e.
For examples – math.log1p(10) will return 2.3978952727983707
math.log2(x)Return logarithm of x with base 2.
For example – math.log2(100) will return 6.643856189774724
math.log10(x)Return logarithm of x with base 10.
For example – math.log10(100) will return
2
math.pow(x,y)Return x raised to power y.
For example – math.pow(4, 8) will return 65536.0
math.sqrt(x)Return square root of x.
For example – math.sqrt(10) will return 3.0

Trigonometric Functions in Python Math Module

Table containing logarithmic functions inside Math Python Module with description and examples for each one.

Stop 🖐🏻
If your unaware of What are trigonometric functions? then see – Mathematical Trigonometric Functions first.

Python deals with Trigonometric functions like Sine, Cosine and others in Radians not in degrees. If in case your unaware of What is Radian? then see Radian first before moving on further in this article.

FunctionDescription
math.cos(x)Returns cosine of x
For example – math.cos(100) will return 0.862318872287684
math.sin(x)Returns sine of x
For example – math.sin(4) will return -0.7568024953079282
math.tan(x)Returns tangent of x
For example – math.tan(5) will return -3.380515006246585
math.dist(x,y)Returns Euclidean distance between x and y points.
For example – math.dist((10,2), (20,1)) will return 10.04987562112089

Angular Conversion functions in Python Math Module

Table containing Angular Conversion functions(For converting degrees to radians and vice-versa) with description and examples for each one.

FunctionDescription
math.degress(x)Converts angle x from degress to radians.
For example – math.degrees(10) will return 572.9577951308232
math.radians(x)Converts angle x from radians to degrees.
For example – math.radians(29) will return 0.5061454830783556

Mathematical Constants in Python Math Module

Tables containing Mathematical Constants with description and examples for each one.

FunctionDescription
math.piReturns mathematical constant π = 3.141592
math.eReturns mathematical exponent value e = 2.718281
math.tauReturns mathematical constant value τ = 6.283185
math.nanReturns non a number NaN

Conclusion

Python’s Data Type Number can be integer, float or complex. But all of functions in Python’s Math Module can be applied to only integers/floats but can’t be applied to complex numbers as these have an imaginary part called iota.
That’s why Complex Numbers are totally different from others, so in order to do Mathematics with these numbers Python’s core developers have designed a separate module containing all functions which can take in complex numbers as parameter.
See this – cmath Python Module for Complex Numbers

Moreover if your interested in learning more about Python Programming Language, then you can see other articles written by me about Python here – Computer Science Hub Python.

Josh

Hi, I'm Josh a Computer Science graduate from California State University, Sacramento since coming out with my Master's from university. I've worked with multiple startups across US and in UK as well primarily as a Python Developer. Here on this website, I'm sharing my knowledge of Python. If you want to ask me anything about Python feel free to reach out, I would be happy to help you out.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts