There are lots of built-in math functions in Python. If you don’t find the one you need, chances are that it’s defined in the math module, which you can simply import, but let’s start with the built-in ones.
If you want to watch a video first, here it is:
And now let’s have a look at the math functions one by one.
Table of Contents
Built-in Math Functions
Here are the built-in math functions. You can use them out of the box, without importing anything.
abs
The first function is abs. It returns the absolute value of a number:
point1 = (3, 0) # x, y - coordinates of point 1
point2 = (-5, 0) # x, y - coordinates of point 2
distance_from_zero1 = abs(point1[0]) # distance of point 1 from (0,0)
distance_from_zero2 = abs(point2[0]) # distance of point 2 from (0,0)
print(f"Point 1 is {distance_from_zero1} units away from the center.")
print(f"Point 2 is {distance_from_zero2} units away from the center.")
The output is:
Point 1 is 3 units away from the center.
Point 2 is 5 units away from the center.
max and min
Then we have the max and min functions, which return the greatest and the smallest of their arguments respectively:
workers = {"Daniel Smith" : 57,
"Andy Lee" : 51,
"Brian Denver" : 48,
"Will Hopkins" : 50}
oldest = max(workers.values())
youngest = min(workers.values())
print(f"The oldest worker is {oldest}, the youngest only {youngest} years old.")
The output is:
The oldest worker is 57, the youngest only 48 years old.
round
The next function, round, is used to round numbers:
distance = 8.4632
print(f"The distance is about {round(distance, 1)} km.")
Now the output is:
The distance is about 8.5 km.
Math Functions in the math Module
Now, the following functions are available in the math module, so we have to import them.
ceil and floor
The two functions, ceil and floor, return integers.
The ceil(x) function returns the smallest integer not less than x, whereas the floor(x) function returns the largest integer not greater than x:
from math import *
value1 = 3.02
value2 = 3.64
value3 = 3.91
value4 = 3.18
max_value = max(value1, value2, value3, value4)
min_value = min(value1, value2, value3, value4)
print(f"The value oscillates between about {floor(min_value)} and {ceil(max_value)}.")
The output is:
The value oscillates between about 3 and 4.
pow
The pow function works pretty much like the ** operator, but it returns a float:
from math import *
>>> 2 ** 12
4096
>>> math.pow(2, 12)
4096.0
There is no difference with floats:
>>> 25 ** 0.5
5.0
>>> math.pow(25, 0.5)
5.0
exp
The exponential function returns ex, where e is the Euler’s number equal to 2.71828…:
from math import *
exponents = range(5)
for exponent in exponents:
result = exp(exponent)
print(f"e^{exponent} = {result}")
The output is:
e^0 = 1.0
e^1 = 2.718281828459045
e^2 = 7.38905609893065
e^3 = 20.085536923187668
e^4 = 54.598150033144236
sqrt
The sqrt function returns the square root of x. x cannot be a negative number or we’ll get an error:
from math import *
for num in range(0, 101, 10):
result = sqrt(num)
print(f"the square root of {num} is {result}")
The output is:
the square root of 0 is 0.0
the square root of 10 is 3.1622776601683795
the square root of 20 is 4.47213595499958
the square root of 30 is 5.477225575051661
the square root of 40 is 6.324555320336759
the square root of 50 is 7.0710678118654755
the square root of 60 is 7.745966692414834
the square root of 70 is 8.366600265340756
the square root of 80 is 8.94427190999916
the square root of 90 is 9.486832980505138
the square root of 100 is 10.0
This function works pretty much the same as the ** operator with a fractional operand on the right:
>>> from math import *
>>> 49 ** 0.5
7.0
>>> sqrt(49)
7.0
log and log10
The log and log10 functions return the natural logarithm and the logarithm in base 10 of a number respectively. A natural logarithm is a logarithm in base e (Euler’s number). The Euler’s number is available in the math module (math.e).
from math import *
nums = [0.001, e, 1000]
print("Natural logarithms:\n")
for num in nums:
print(f"the natural logarithm of {num} is {log(num)}")
print("\nLogarithms in base 10:\n")
for num in nums:
print(f"the logarithm in base 10 of {num} is {log10(num)}")
The output is:
Natural logarithms:
the natural logarithm of 0.001 is -6.907755278982137
the natural logarithm of 2.718281828459045 is 1.0
the natural logarithm of 1000 is 6.907755278982137
Logarithms in base 10:
the logarithm in base 10 of 0.001 is -3.0
the logarithm in base 10 of 2.718281828459045 is 0.4342944819032518
the logarithm in base 10 of 1000 is 3.0
I’m going to wrap it up for now. There are lots of other mathematical functions, so if you use a lot of math in your code, feel free to explore all the available resources. There are also special modules specifically for mathematical computing, like numpy for example, with their own sets of mathematical functions. Give them a try too.