abs()

The abs() function in Python returns a number’s absolute value, which is its distance from zero on the number line, regardless of its sign.

Python
>>> abs(-1)
1
>>> abs(1)
1

abs() Signature

Python Syntax
abs(number)

Arguments

Argument Description
number An integer, floating-point, or complex number for which to calculate the absolute value. Also accepts fractions and decimals.

Return Value

  • For real numbers, returns a non-negative number of the same type (int or float).
  • For complex numbers, it returns the magnitude as a float (that is, the square root of the sum of squares of real and imaginary parts).

abs() Examples

With integer numbers:

Python
>>> abs(-5)
5
>>> abs(5)
5

With floating-point numbers:

Python
>>> abs(-3.14)
3.14
>>> abs(3.14)
3.14

With complex numbers:

Python
>>> abs(3 + 4j)
5.0

With Fraction values:

Python
>>> from fractions import Fraction
>>> abs(Fraction("1/3"))
Fraction(1, 3)
>>> abs(Fraction("-3/4"))
Fraction(3, 4)

With Decimal values:

Python
>>> from decimal import Decimal
>>> abs(Decimal("0.3333333333333333"))
Decimal('0.3333333333333333')
>>> abs(Decimal("-0.75"))
Decimal('0.75')

abs() Common Use Cases

The most common use cases for the abs() function include the following:

  • Determine the distance between two points on a number line.
  • Process data in mathematical computations where only positive values are meaningful or allowed.
  • Find the magnitude of complex numbers.

abs() Real-World Example

Suppose you’re tracking temperature changes in a city and want to find out how much the temperature fluctuates from the average, regardless of whether it’s a drop or an increase:

Python
>>> temperatures = [22, 25, 18, 30, 15, 28, 21]
>>> average_temp = sum(temperatures) / len(temperatures)
>>> deviations = [abs(temp - average_temp) for temp in temperatures]
>>> print("Absolute deviations:", *deviations, sep="\n")
Absolute deviations:
0.7142857142857153
2.2857142857142847
4.714285714285715
7.285714285714285
7.714285714285715
5.285714285714285
1.7142857142857153

In this example, you compute the temperature deviations from the average temperature using abs().

abs() in Custom Classes

You can support the abs() function in your custom classes. To illustrate, consider the following Vector class:

Python
>>> import math
>>> class Vector:
...     def __init__(self, *coordinates):
...         self.coordinates = coordinates
...     def __abs__(self):
...         return math.hypot(*self.coordinates)
...

>>> abs(Vector(0.42, 1.5, 0.87))
1.7841804841439108

The .__abs__() special method allows you to support abs() in your custom classes. In this example, calling abs() on your Vector class instance returns the correct absolute value, equal to about 1.78.

Tutorial

How to Find an Absolute Value in Python

In this tutorial, you'll learn how to calculate the absolute value in Python using the built-in abs() function. You'll also implement the corresponding mathematical formulas from scratch. Finally, you'll change the behavior of abs() in your own classes by hooking into Python's interface.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Nov. 7, 2024 • Reviewed by Brenda Weleschuk, and Dan Bader