Python builtins abs()
Example
Get the absolute value of numbers, including complex magnitude:
print(abs(-5)) # 5
print(abs(-3.14)) # 3.14
print(abs(3+4j)) # 5.0 (magnitude of the complex number)
Try it Yourself »
Definition and Usage
The abs()
built-in returns the absolute value of a number.
For complex numbers, it returns the magnitude (Euclidean norm). Custom classes can support abs(x)
by defining __abs__(self)
.