🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

abs()

AI & DATA SCIENCE // abs

abs() returns the absolute (non-negative) value of a number — an int, float, or complex.

Syntax

abs(x)

Deep Dive Course

For an int or float, abs(x) simply strips the sign, so the absolute value of -7 and 7 are both 7. For a complex number, it instead returns the magnitude — the distance from the origin in the complex plane, computed as the square root of the sum of the squares of the real and imaginary parts — rather than a sign-stripped complex number, since 'negative' isn't meaningful for complex values.

1Understanding abs()

For an int or float, abs(x) simply strips the sign, so the absolute value of -7 and 7 are both 7. For a complex number, it instead returns the magnitude — the distance from the origin in the complex plane, computed as the square root of the sum of the squares of the real and imaginary parts — rather than a sign-stripped complex number, since 'negative' isn't meaningful for complex values.

💡

Subtracting two numbers and taking abs() of the result is the idiomatic way to get the distance between them regardless of which one is larger.

editor.html
print(abs(-15))
print(abs(15))
print(abs(-3.7))
localhost:3000

2Practical Example

Here is a real-world application of abs() showing how it is used in production Python code.

editor.html
target = 100
actual = 87
if abs(target - actual) > 5:
    print("Reading is out of tolerance")
else:
    print("Reading is within tolerance")
localhost:3000

3Best Practices

Follow these guidelines when working with abs():

1. Use abs(a - b) to measure distance/difference between two values instead of an if/else that picks the larger minus the smaller

2. Define __abs__() on a custom numeric class if you want it to support abs()

3. Combine abs() with a tolerance and comparison, or math.isclose, rather than == when checking if a float is 'close enough' to a target

⚠️

Tip: Subtracting two numbers and taking abs() of the result is the idiomatic way to get the distance between them regardless of which one is larger.

editor.html
print(abs(-15))
print(abs(15))
print(abs(-3.7))
localhost:3000

Examples

Example 01Basic Usage
print(abs(-15))
print(abs(15))
print(abs(-3.7))
Example 02Advanced Example
target = 100
actual = 87
if abs(target - actual) > 5:
    print("Reading is out of tolerance")
else:
    print("Reading is within tolerance")

Best Practices

  • Use abs(a - b) to measure distance/difference between two values instead of an if/else that picks the larger minus the smaller
  • Define __abs__() on a custom numeric class if you want it to support abs()
  • Combine abs() with a tolerance and comparison, or math.isclose, rather than == when checking if a float is 'close enough' to a target

Interview Question

What does abs() return for a complex number, and why isn't it just 'the number with a positive sign'?

Hint: Complex numbers don't have a sign in the way real numbers do.

For a complex number, abs() returns its magnitude — the distance from the origin on the complex plane — as a float. Complex numbers aren't ordered, since there's no built-in notion of one being greater or less than another, so 'stripping a sign' doesn't make sense; magnitude is the meaningful generalization of absolute value to two dimensions.

Exercises

MediumPractice using abs() in a real scenario.
View Solution
print(abs(-15))
print(abs(15))
print(abs(-3.7))

Frequently Asked Questions

What does abs() return for a complex number, and why isn't it just 'the number with a positive sign'?

For a complex number, abs() returns its magnitude — the distance from the origin on the complex plane — as a float. Complex numbers aren't ordered, since there's no built-in notion of one being greater or less than another, so 'stripping a sign' doesn't make sense; magnitude is the meaningful generalization of absolute value to two dimensions.

Related Functions

round()min()max()