Python Math

Python Math

Python has a set of built-in math functions, including an extensive math module, that allows you to perform mathematical tasks on numbers.


Built-in Math Functions

The min() and max() functions can be used to find the lowest or highest value in an iterable:

Min, Max, Abs, Pow

x = min(5, 10, 25)
y = max(5, 10, 25)
z = abs(-7.25)
p = pow(4, 3) # 4 to the power of 3

print("Min:", x) print("Max:", y) print("Absolute:", z) print("Power:", p)


The math Module

Python has also a built-in module called math, which extends the list of mathematical functions. To use it, you must import math.

Math Module Examples

import math

print(math.sqrt(64)) # 8.0

print(math.ceil(1.4)) # 2 print(math.floor(1.4)) # 1

print(math.pi) # 3.141592653589793