abs()
abs(number) int | float · Added in v3.0 · Updated March 13, 2026 · Built-in Functions The abs() function returns the absolute value of a number, which is the number without its sign. It accepts integers, floating-point numbers, or any object that implements the __abs__() method. For complex numbers, abs() returns the magnitude (the distance from the origin in the complex plane).
Syntax
abs(number)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
number | int | float | complex | object with __abs__ | — | A numeric value or object implementing __abs__(). Position-only parameter. |
Returns: The absolute value as an integer if the input is an integer, a float if the input is a float or complex.
Examples
Basic usage with integers
# Positive integer returns unchanged
print(abs(5))
# 5
# Negative integer returns positive
print(abs(-5))
# 5
# Zero returns zero
print(abs(0))
# 0
Working with floating-point numbers
# Float works the same way
print(abs(-3.14))
# 3.14
# Scientific notation
print(abs(-1e-10))
# 1e-10
Complex numbers return magnitude
For complex numbers, abs() returns the magnitude (also called the modulus), calculated as the square root of the sum of squares of the real and imaginary parts.
# Complex number: magnitude = sqrt(real² + imaginary²)
z = 3 + 4j
print(abs(z))
# 5.0
# Another example
w = 1 - 1j
print(abs(w))
# 1.4142135623730951
Using with custom objects
Objects can implement __abs__() to work with the built-in function:
class Temperature:
def __init__(self, kelvin):
self.kelvin = kelvin
def __abs__(self):
return Temperature(abs(self.kelvin))
# Demonstrating __abs__ implementation
t = Temperature(-100)
print(abs(t)) # Returns a new Temperature object with absolute value
Common Patterns
Distance calculations
abs() is essential for calculating distances, which are always positive:
# Distance between two points on a number line
point_a = 10
point_b = 25
distance = abs(point_a - point_b)
print(distance)
# 15
# Find closest value in a list
values = [3, 7, 2, 15, 8]
target = 6
closest = min(values, key=lambda x: abs(x - target))
print(closest)
# 7
Handling user input
# Get magnitude of a number (e.g., temperature change)
temp_change = -15 # User might enter negative
magnitude = abs(temp_change)
print(f"Temperature changed by {magnitude} degrees")
# Temperature changed by 15 degrees
Validating numerical ranges
# Check if a value is within a tolerance
def within_tolerance(actual, expected, tolerance=0.01):
return abs(actual - expected) < tolerance
print(within_tolerance(1.005, 1.0))
# True
print(within_tolerance(1.1, 1.0))
# False
Sorting by distance
# Sort points by distance from origin
points = [(3, 4), (1, 1), (-2, -3), (0, 5)]
sorted_by_distance = sorted(points, key=lambda p: abs(p[0]) + abs(p[1]))
print(sorted_by_distance)
# [(1, 1), (0, 5), (3, 4), (-2, -3)]
Working with dictionaries and sorting
# Find key with value closest to target
scores = {"alice": 95, "bob": 87, "charlie": 92, "diana": 89}
target = 90
closest_key = min(scores, key=lambda k: abs(scores[k] - target))
print(closest_key)
# charlie
Errors
TypeError for incompatible types
# String raises TypeError
abs("hello")
# TypeError: bad operand type for abs(): 'str'
Custom objects without abs
Objects that don’t implement __abs__() will raise a TypeError:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(3, 4)
abs(p)
# TypeError: bad operand type for abs(): 'Point'
See Also
- math-module::fabs — returns a float absolute value (requires import)
- built-in::complex — creates complex numbers
- built-in::pow — power function for exponentiation
- built-in::round — round numbers to specified precision