Home
IT Knowledge
Inspiration
Languages
EN

# Python - abs() method example

0 points
Created by:
386

The `abs()` function returns the absolute value of a number.

The absolute value of the number x denoted |x|, is the non-negative value of x without regard to its sign.

Popular usage examples:

``````print(abs(2))     # 2
print(abs(-2))    # 2

print(abs(2.54))  # 2.54
print(abs(-2.54)) # 2.54

print(abs(0))     # 0

print(abs(3 + 5)) # 8
print(abs(3 - 5)) # 2``````

## 1. Documentation

 Syntax ``abs(number)`` Parameters `number` - number whose absolute value is to be returned Result The absolute value calculated from the `number`. Description `abs()` is a method that takes only one parameter and returns an absolute number parameter value. The absolute value of the number x denoted |x|, is the non-negative value of x without regard to its sign.

## 2. `abs()` custom implementation

``````def absoluteValue(x):
if x < 0:
return -x
return x

print(absoluteValue(2))     # 2
print(absoluteValue(-2))    # 2
print(absoluteValue(0))     # 0``````

## References

1. Absolute value - Wikipedia