How to get absolute value in Python

To get an absolute value in Python use abs function. It is a built-in function so no import is required.

For example:

abs(-11)

will return 11, when:

abs(11)

will also return 11.

Formally, the absolute value is a value without a minus sign (-) for all negatives and the same value for all non-negatives. Note that 0 is nonnegative, so abs(0) returns 0.

The absolute value in python variable

You can call the function passing variable also:

val = -2
val_abs = abs(val)
print(val_abs)

Will print 2.

Float absolute value in python

You can also get an absolute for a float:

abs(-11.02)

Will return 11.02.

To get absolute value use abs function

#python #math
5
Bruce Hardy profile picture
Nov 01, 2020
by Bruce Hardy
Did it help you?
Yes !
No

Best related

Other by Bruce Hardy