Python abs() Function
abs(x) returns the absolute value of a number: its distance from zero, so abs(-7) is 7.The absolute value drops the sign of a number. It answers questions like "how far apart are these two values?" without caring which one is larger: the gap between a temperature of 18 and 25 is 7 either way.
Python's abs() works on integers and floats and also on complex numbers, where it gives the magnitude. For your own classes you can support it by defining __abs__(), one of the special methods.
| Syntax | abs(x) |
|---|---|
| Parameters | x: an int, float, complex number, or an object that defines __abs__() |
| Returns | A non-negative number of the same kind (a float for a complex input) |
| Raises | TypeError for strings, lists and other non-numeric values |
| Time complexity | O(1) |
How abs() treats ints, floats and complex numbers
For a negative int or float, abs() flips the sign. For a positive number or zero it returns the same value. The type is preserved: an int stays an int and a float stays a float.
For a complex number a + bj, abs() returns the length of the vector from the origin, sqrt(a*a + b*b), as a float. That is why abs(3 + 4j) equals 5.0.
print(abs(-12))
print(abs(-0.75))
print(abs(3 + 4j))Using abs() to measure differences and tolerances
Two everyday jobs make abs() worth knowing. The first is the distance between two numbers: abs(a - b). The second is comparing floats safely. Floats store tiny rounding errors, so 0.1 + 0.2 == 0.3 is False; checking that abs(x - y) is below a small tolerance is more reliable. For serious numeric work, math.isclose() from the math module does this for you.
total = 0.1 + 0.2
print(total == 0.3)
print(abs(total - 0.3) < 1e-9)abs() vs math.fabs() vs negation
| Tool | Input types | Return type for an int input |
|---|---|---|
abs(x) | int, float, complex, custom __abs__ | int |
math.fabs(x) | Real numbers only | float |
-x | Any number | Flips the sign; positive numbers become negative |
max(x, -x) | Real numbers | Same as abs(x) but slower and less clear |
Examples you can run
Press Run, then edit the code and run it again.
Absolute value of different numbers
Sign is dropped; zero stays zero.
print(abs(-7), abs(-3.5), abs(0), abs(12))Expected output7 3.5 0 12
Distance between two readings
abs(a - b) gives the gap regardless of order.
morning = 18
evening = 25
print(abs(morning - evening))
print(abs(evening - morning))Expected output7
7
Find the closest value
Use abs() as the key of min() to pick the item nearest a target.
prices = [80, 95, 130, 210]
target = 100
print(min(prices, key=lambda p: abs(p - target)))Expected output95
Magnitude of a complex number
The result is a float.
z = 3 + 4j
print(abs(z))Expected output5.0
Common mistakes
Passing a string
✗ Wrong
balance = "-50"
print(abs(balance))✓ Right
balance = "-50"
print(abs(int(balance)))Convert the text to a number first.
Calling abs() on a list
✗ Wrong
changes = [-3, 5, -1]
print(abs(changes))✓ Right
changes = [-3, 5, -1]
print([abs(c) for c in changes])abs() takes one number. Apply it to each item with a comprehension or map().
Comparing floats with ==
✗ Wrong
x = 0.1 + 0.2
print(x == 0.3)✓ Right
x = 0.1 + 0.2
print(abs(x - 0.3) < 1e-9)Compare with a tolerance using abs().
Python abs() Function: questions people ask
How do I get the absolute value in Python?
Use the built-in abs() function, for example abs(-5) returns 5. It needs no import.
Does abs() work with floats and complex numbers?
Yes. For floats it drops the sign, and for complex numbers it returns the magnitude as a float, so abs(3 + 4j) is 5.0.
What is the difference between abs() and math.fabs()?
abs() keeps the input type and also accepts complex numbers, while math.fabs() always returns a float and accepts only real numbers.
Keep learning
Step-by-step lessons:
Python NumbersPython math and statisticsPython Operators
Related:
Python round() FunctionPython pow() FunctionPython min() FunctionPython max() Function
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →