The Python abs() function returns the absolute value of a number, which is its distance from zero without the sign, and for a complex number it returns the magnitude. We use it to measure differences, such as how far a reading is from a target, regardless of which side it falls on.
loss = abs(-50) # 50
change = abs(-99.99) # 99.99
The sections cover the return types, complex numbers, -0.0, Decimal and Fraction, custom classes with __abs__(), and abs() compared with math.fabs(). Results are from Python 3.14.
1. Syntax and Return Value
The function takes one number and keeps its type, except for complex numbers and bool.
abs(x) -> int | float | Decimal | Fraction
| Argument | Returns | Example |
|---|---|---|
| int | int | abs(-7) is 7 |
| float | float | abs(-2.5) is 2.5 |
| complex | float (the magnitude) | abs(3 – 4j) is 5.0 |
| Decimal, Fraction | the same type | abs(Decimal(‘-1.10’)) is Decimal(‘1.10’) |
| bool | int | abs(True) is 1 |
Python integers have no fixed size, so abs(-2**63) returns 9223372036854775808 instead of overflowing as it would in Java or C.
2. Integers and Floats
For real numbers, abs() drops the minus sign. A typical use is the distance between two values, where the order of the subtraction does not matter.
target = 21.0
reading = 19.5
off_by = abs(reading - target) # 1.5
same = abs(target - reading) # 1.5
To compare floats, we use math.isclose() rather than abs(a – b) < tiny_number, because a fixed threshold is too strict for large values and too loose for small ones.
import math
naive = abs(0.1 + 0.2 - 0.3) < 1e-9 # True, but the threshold is a guess
close = math.isclose(0.1 + 0.2, 0.3) # True, relative tolerance
3. Complex Numbers
For a complex number a + bj, abs() returns the magnitude, the distance from the origin, which is the square root of a**2 + b**2. The result is always a float.
magnitude = abs(3 - 4j) # 5.0
other = abs(5 - 9j) # 10.295630140987
check = math.hypot(5, -9) # 10.295630140987, the same value
4. Negative Zero, Decimal and Fraction
Floats have a negative zero, and abs(-0.0) returns the positive 0.0. Since -0.0 == 0.0 is True, the math.copysign() function is the way to see the sign.
from decimal import Decimal
from fractions import Fraction
zero = abs(-0.0) # 0.0
sign = math.copysign(1, -0.0) # -1.0, the sign was negative
money = abs(Decimal("-10.25")) # Decimal('10.25')
part = abs(Fraction(-3, 4)) # Fraction(3, 4)
5. abs() vs math.fabs() and Custom Classes
The math.fabs() function always returns a float and does not accept complex numbers, while abs() keeps the type. A class supports abs() by defining __abs__().
fabs_int = math.fabs(-5) # 5.0, a float
abs_int = abs(-5) # 5, still an int
class Vector:
def __init__(self, x, y):
self.x, self.y = x, y
def __abs__(self):
return math.hypot(self.x, self.y)
length = abs(Vector(6, 8)) # 10.0
bad = abs("5") # TypeError: bad operand type for abs(): 'str'
The last line shows that abs() does not convert strings, so input from a file or a form goes through int() or float() first.
6. Finding the Closest Value
A practical use of abs() is finding the element nearest to a target, by passing it as the key of min().
sizes = [36, 38, 40, 42, 44]
foot = 41.2
best = min(sizes, key=lambda s: abs(s - foot)) # 42
order = sorted(sizes, key=lambda s: abs(s - foot)) # [42, 40, 44, 38, 36]
The same pattern picks the nearest store or the time slot closest to a request.
7. abs() Questions
The most frequent questions cover lists, rounding and whether abs() is a method or a function.
7.1. How do we get the absolute values of a whole list?
We apply abs() to each element, [abs(n) for n in values], or use list(map(abs, values)). For NumPy arrays, numpy.abs(arr) works on the whole array at once.
7.2. Does abs() round the number?
No. It only removes the sign, so abs(-2.75) stays 2.75. Rounding needs round().
7.3. Is abs() a method of int?
No, it is a built-in function. Numbers implement it through the special method __abs__(), which abs() calls.
8. Conclusion
The abs() function removes the sign of a real number, returns the magnitude of a complex number and keeps the type of int, Decimal and Fraction. Combined with min() it finds the nearest value, and for comparing floats we use math.isclose() instead.
9. References
Happy Learning !!