abs()

Anonymous contributor's avatar
Anonymous contributor
Published May 10, 2021Updated Oct 13, 2023
Contribute to Docs

The abs() function returns the absolute value of a numeric argument.

Syntax

abs(n)

Return value is the absolute value of the n parameter, which is of type int or float.

The absolute value of n will be its distance from zero regardless of its direction (i.e., whether it is positive or negative). Since the absolute value is never negative, a positive value will remain unchanged and a negative value will have its negative sign removed.

Example

In the example below, the absolute values of two variables, positive and negative, are returned with the abs() function:

positive = 10
negative = -3.5
print(abs(positive))
print(abs(negative))

This will produce the following output:

10
3.5

Codebyte Example

The following example returns the absolute value from each element in the numbers list:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy