EN
Python - abs() method example
0 points
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:
xxxxxxxxxx
1
print(abs(2)) # 2
2
print(abs(-2)) # 2
3
4
print(abs(2.54)) # 2.54
5
print(abs(-2.54)) # 2.54
6
7
print(abs(0)) # 0
8
9
print(abs(3 + 5)) # 8
10
print(abs(3 - 5)) # 2
Syntax |
xxxxxxxxxx 1 abs(number) |
Parameters | number - number whose absolute value is to be returned |
Result | The absolute value calculated from the number . |
Description |
|
xxxxxxxxxx
1
def absoluteValue(x):
2
if x < 0:
3
return -x
4
return x
5
6
7
print(absoluteValue(2)) # 2
8
print(absoluteValue(-2)) # 2
9
print(absoluteValue(0)) # 0