abs
Calculate the absolute value of a number, its distance from zero.
Python 3.13
abs(x)- x
- The number whose absolute value to find.int | float | complex
Return value
A non-negative number of the same type: int for int, float for float; a float for complex.
Examples
Absolute value of a negative number
Python 3.13
print(abs(-5))5
Absolute value of a float and a complex number
Python 3.13
print(abs(-2.5), abs(3 + 4j))2.5 5.0
Details
For a complex number a + bj, the length of the vector √(a² + b²) is calculated, so abs(3 + 4j) is 5.0. bool is a subclass of int, so abs(True) gives 1. For your own classes, abs calls the __abs__ method.
