What Is Python’s abs() Function? (Coding Specs)
Python’s abs() function takes one value and returns its absolute value or magnitude. For integers and floats, that means removing a negative sign; for complex numbers, it means returning the distance from zero as a float. abs() is built into Python, so you do not need to install a package.
I once worked through a small debugging example where two measurements were subtracted in the wrong order. The result was negative, even though the code only needed the size of the gap. That is a common reason to reach for abs(). But it is important to know what the function removes: the sign, and sometimes other information you may need.

abs() Overview: Purpose, Syntax, and Supported Values
abs(x) is a built-in Python function that takes one argument, x, and returns its absolute value or magnitude. It works with common numeric types such as integers, floats, and complex numbers. The result is not always an integer, and it does not preserve the original sign.
For a real number, absolute value means its nonnegative distance from zero on the number line. So abs(-7) returns 7, while abs(7) also returns 7. A negative sign is removed, but a positive value stays the same.
print(abs(-7)) # 7
print(abs(7)) # 7
print(abs(0)) # 0
The general call form is simple:
abs(value)
Pass exactly one argument. That argument must be a supported numeric value or an object whose class defines the method Python uses for absolute value. If the argument is unsupported, Python raises a TypeError.
Complex numbers need a slightly different explanation. A complex number has a real part and an imaginary part. Its absolute value is its magnitude: the distance from zero in the complex plane. For example, 3 + 4j has a magnitude of 5.0.
abs(3 + 4j) # 5.0
Unlike subtraction on a number line, this result does not tell you the direction or angle of the complex number. It tells you only how far it is from zero.
Return Types, Numeric Behavior, and __abs__() Requirements
The type of the result depends on the input. Integers return integers, floats return floats, and complex numbers return floats. This matters when later code checks types, formats results, or passes them to another calculation.
| Input | Result | Result type | What it represents |
|---|---|---|---|
abs(-7) |
7 |
int |
Distance from zero |
abs(-2.5) |
2.5 |
float |
Distance from zero |
abs(-0.0) |
0.0 |
float |
Nonnegative floating-point zero |
abs(3 + 4j) |
5.0 |
float |
Complex magnitude |
For integers, Python supports arbitrary precision. In practice, this means there is no fixed integer-width limit built into the language; the available memory limits how large an integer can become. Taking the absolute value of an integer does not convert it to a float.
For floats, the result remains a float. In particular, abs(-0.0) returns 0.0. Although floating-point negative zero can appear in calculations, abs() returns a nonnegative zero value.
For a complex number a + b*j, the mathematical magnitude is the square root of a² + b². For example:
z = 3 + 4j
print(abs(z)) # 5.0
Python returns a float for this result. Do not use it when you need the complex number’s angle or direction. Those are different properties from magnitude.
A custom class can also define what abs() means for its objects. Python does this by calling the class’s __abs__() method. This is part of Python’s data model: a set of conventions that let built-in operations work with user-defined types.
class Measurement:
def __init__(self, value):
self.value = value
def __abs__(self):
return abs(self.value)
m = Measurement(-2.5)
print(abs(m)) # 2.5
Here, __abs__() returns a float because the stored value is a float. When you write a custom method, choose a result that makes sense for the class and document that behavior. If a class does not support absolute value, calling abs() on its instance raises TypeError.
Practice Cases, Testing, and Common Errors
A useful first test checks both the returned value and its type. This helps catch an assumption such as “abs() always returns an integer,” which is false for floats and complex values.
cases = [-7, -2.5, 3 + 4j, -0.0]
for value in cases:
result = abs(value)
print(repr(value), "->", result, type(result).__name__)
Expected results:
| Expression | Expected value | Expected type |
|---|---|---|
abs(-7) |
7 |
int |
abs(-2.5) |
2.5 |
float |
abs(3 + 4j) |
5.0 |
float |
abs(-0.0) |
0.0 |
float |
For repeatable checks, use assertions. An assertion stops the program if a result does not match what you expected.
assert abs(-7) == 7
assert abs(-2.5) == 2.5
assert abs(3 + 4j) == 5.0
assert abs(-0.0) == 0.0
A common application is finding the numeric difference between two values without caring which is larger:
a = 12
b = 17
gap = abs(a - b) # 5
The order of subtraction changes the sign, but not the size of the gap after abs() is applied. This works for numeric distances on a line. It does not mean the function validates the inputs or confirms that the values are suitable for a particular formula.
Another useful case is measuring the size of an error:
expected = 10.0
measured = 9.6
error_size = abs(expected - measured) # 0.4
This reports the error’s magnitude, not whether the measured value was too high or too low. If that direction matters, keep the signed difference too:
error = measured - expected
error_size = abs(error)
The signed value is -0.4; the size is 0.4. Keeping both avoids losing useful information.
Other frequent mistakes include:
- Passing more than one argument:
abs(-3, 2)raisesTypeError. The function accepts one argument. - Expecting an integer for every input:
abs(-2.5)is2.5, a float. - Using it on text:
abs("-7")raisesTypeError. Convert valid numeric text first, such as withint()orfloat(). - Dropping a sign too early:
abs()cannot tell you afterward whether the original number was negative. - Treating complex magnitude as direction:
abs(3 + 4j)returns5.0, not an angle.
If you see a TypeError, check the argument’s type and whether its class implements __abs__(). If a result seems wrong, check whether your calculation needs a signed value, a magnitude, or a different measure entirely.
Applications in Algorithms, Scientific Computing, and Technical Work
abs() is useful whenever a calculation needs the size of a real-valued difference or the magnitude of a numeric value. Typical examples include comparing measurements, finding distance on a number line, and reporting the size of an error. The right use depends on whether direction and sign are relevant.
For example, a simple loop can track the largest difference between paired values:
left = [10, 20, 30]
right = [12, 18, 33]
largest_gap = 0
for x, y in zip(left, right):
largest_gap = max(largest_gap, abs(x - y))
print(largest_gap) # 3
This compares each pair and keeps the largest gap. The code treats a result above or below zero the same way. If the direction of each difference matters, store x - y as well as its absolute value.
In scientific or technical code, an absolute error can help answer “how large is the difference?” It does not answer “is the result accurate enough?” That requires a chosen tolerance and, often, a separate decision about relative error. The acceptable threshold depends on the task, units, and data; abs() does not supply one.
Complex-number work has a similar distinction. Use abs(z) when you need a complex value’s magnitude. Use a method designed for phase or angle when you need direction. A magnitude-only comparison can be correct for one task and misleading for another.
A practical workflow is:
- Identify whether the input is an integer, float, complex number, or custom object.
- Decide whether you need magnitude or signed direction.
- Apply
abs()only when discarding the sign is intended. - Test both the result and its type with representative values.
- For custom classes, define and test
__abs__().
Conclusion
abs() is a small built-in with clear behavior: it returns a number’s absolute value or magnitude. Its output type depends on the input, and it removes sign information. Test the value and type, and retain a signed result whenever direction matters.
What does Python’s abs() function do?
abs(x) returns the absolute value or magnitude of x. For real numbers, this is the nonnegative distance from zero. For complex numbers, it is the magnitude in the complex plane. The function takes one argument and is available without installing a package.
Does abs() always return an integer?
No. An integer input returns an integer, a float input returns a float, and a complex input returns a float magnitude. For instance, abs(-7) returns 7, while abs(-2.5) returns 2.5.
What does abs(-0.0) return?
abs(-0.0) returns 0.0, which is a float. The output is nonnegative zero. You can check this directly in Python with print(abs(-0.0)) or inspect the result’s type with type().
How does abs() work with complex numbers?
For a complex number, abs() returns its magnitude, or distance from zero in the complex plane. For example, abs(3 + 4j) returns 5.0. It does not return the number’s direction or angle.
Can I use abs() to find the difference between two numbers?
Yes. abs(a - b) gives the nonnegative size of the difference between two numeric values. It does not show which value is larger. Keep a - b separately if the sign or direction of the difference matters.
What happens if I pass a string to abs()?
Passing a string such as "-7" raises TypeError, because a string is not a supported numeric value for this operation. If the text contains a valid number, convert it first with int() or float(), then call abs().
What is __abs__() in Python?
__abs__() is a special method a custom class can define to support Python’s abs() function. When you call abs(object), Python uses that method. Its implementation should return a meaningful absolute value or magnitude for that class.
Why does abs() remove information I need?
For real numbers, abs() removes the sign. For complex numbers, it returns magnitude without direction or angle. Keep the original value or signed calculation when those details matter, and use abs() only for a magnitude-based task.
(This article was written by one of our staff writers, Nathan Sterling. Visit our Meet the Team page.)
