The abs() function returns the absolute value of a number. The absolute value represents the distance from zero, so a negative number becomes positive while positive numbers remain unchanged.
Syntax
abs(number)1. Basic Example
number = -10
result = abs(number)
print(result)Output:
102. Positive Number
number = 25
print(abs(number))Output:
25A positive number remains unchanged.
3. Zero
number = 0
print(abs(number))Output:
04. Negative Numbers
numbers = [-10, -20, -5, -30]
for number in numbers:
print(abs(number))Output:
10
20
5
305. Calculate Distance Between Two Numbers
abs() is very useful for calculating the distance between two values.
a = 10 b = 25
distance = abs(a - b)
print(distance)Output:
15The calculation is:
25 - 10 = 15Even if the order is reversed:
a = 25 b = 10
distance = abs(a - b)
print(distance)Output:
156. Distance from Zero
numbers = [-15, -8, 0, 12, 20]
for number in numbers:
print(
number,
abs(number)
)Output:
-15 15
-8 8
0 0
12 12
20 207. abs() with min()
You can use abs() as the key for min().
numbers = [-10, 5, -3, 8, -1]
result = min(
numbers,
key=abs
)
print(result)Output:
-1Python compares:
-10 → 10
5 → 5
-3 → 3
8 → 8
-1 → 1Therefore, -1 is closest to zero.
8. abs() with max()
numbers = [-10, 5, -3, 8, -1]
result = max(
numbers,
key=abs
)
print(result)Output:
-10The absolute values are:
10, 5, 3, 8, 1So -10 has the largest absolute value.
9. abs() with sorted()
You can sort numbers according to their distance from zero.
numbers = [-10, 5, -3, 8, -1]
result = sorted(
numbers,
key=abs
)
print(result)Output:
[-1, -3, 5, 8, -10]The sorting is based on:
1, 3, 5, 8, 1010. Sort by Absolute Value in Descending Order
numbers = [-10, 5, -3, 8, -1]
result = sorted(
numbers,
key=abs,
reverse=True
)
print(result)Output:
[-10, 8, 5, -3, -1]11. abs() with map()
Since you already learned map(), you can use abs() directly as the function.
numbers = [-10, -20, 5, -8]
result = map(abs, numbers)
print(list(result))Output:
[10, 20, 5, 8]This is simpler than using a lambda:
map(lambda x: abs(x), numbers)You can directly write:
map(abs, numbers)12. abs() with filter()
Suppose we want numbers whose absolute value is greater than 10.
numbers = [-20, -5, 8, 15, 3, -30]
result = filter(
lambda x: abs(x) > 10,
numbers
)
print(list(result))Output:
[-20, 15, -30]The absolute values are:
20, 5, 8, 15, 3, 30Only values greater than 10 are selected.
13. Find Numbers Close to Zero
numbers = [-10, 3, -2, 8, 1, -5]
result = [
x
for x in numbers
if abs(x) <= 3
]
print(result)Output:
[3, -2, 1]14. Find Difference Within a Limit
a = 100 b = 105
if abs(a - b) <= 5:
print("Values are close")
else:
print("Values are far apart")Output:
Values are closeThis pattern is useful when comparing numerical values.
15. Compare Temperature Changes
yesterday = 25 today = 30
change = abs(today - yesterday)
print("Temperature change:", change)Output:
Temperature change: 516. Find Closest Number to a Target
Suppose the target is 50.
numbers = [20, 45, 55, 70, 90]
target = 50
closest = min(
numbers,
key=lambda x: abs(x - target)
)
print(closest)Output:
45Why?
20 → |20 - 50| = 30
45 → |45 - 50| = 5
55 → |55 - 50| = 5
70 → |70 - 50| = 20
90 → |90 - 50| = 40Both 45 and 55 are equally close. Python returns the first one encountered.
17. Find Closest Course Price
prices = [10000, 15000, 20000, 25000, 30000]
target = 18000
closest = min(
prices,
key=lambda price: abs(price - target)
)
print(closest)Output:
20000The difference is:
|20000 - 18000| = 200018. Find Closest Student Mark
marks = [55, 65, 72, 80, 90]
target = 75
closest = min(
marks,
key=lambda mark: abs(mark - target)
)
print(closest)Output:
72Because:
|72 - 75| = 3
|80 - 75| = 5So 72 is closer.
19. abs() with Decimal Values
abs() also works with floating-point numbers.
number = -12.75
print(abs(number))Output:
12.7520. abs() with Complex Numbers
abs() can also calculate the magnitude of a complex number.
number = 3 + 4j
print(abs(number))Output:
5.0This is calculated using:
√(3² + 4²) = √25 = 521. Practical Example : Profit and Loss
Suppose a business has revenue and cost.
revenue = 50000 cost = 65000
difference = revenue - cost
print("Difference:", abs(difference))Output:
Difference: 15000The actual difference is:
50000 - 65000 = -15000abs() gives the size of the difference:
1500022. Practical Example : Attendance Difference
expected = 100 actual = 85
difference = abs(expected - actual)
print("Difference:", difference)Output:
Difference: 1523. Practical Example : Data Analysis
Suppose we have actual and predicted values.
actual = 100 predicted = 92
error = abs(actual - predicted)
print("Absolute Error:", error)Output:
Absolute Error: 8This concept is important in data science and machine learning, where absolute error is used to measure the difference between actual and predicted values.
24. Calculate Total Absolute Error
actual = [100, 200, 300, 400]
predicted = [90, 210, 280, 390]
errors = [
abs(a - p)
for a, p in zip(actual, predicted)
]
total_error = sum(errors)
print("Errors:", errors)
print("Total Error:", total_error)Output:
Errors: [10, 10, 20, 10]
Total Error: 50This combines:
abs()
zip()
list comprehension
sum()25. Calculate Mean Absolute Error
Mean Absolute Error (MAE) is commonly used in machine learning.
actual = [100, 200, 300, 400]
predicted = [90, 210, 280, 390]
errors = [
abs(a - p)
for a, p in zip(actual, predicted)
]
mae = sum(errors) / len(errors)
print("MAE:", mae)Output:
MAE: 12.526. Important Difference
Remember that abs() does not sort or modify a collection.
number = -25
print(abs(number))It simply returns the absolute value:
25Easy way to remember
abs(-10) → 10
abs(10) → 10
abs(0) → 0