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:


will return 11, when:


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)

Will print 2.

Float absolute value in python

You can also get an absolute for a float:


Will return 11.02.

To get absolute value use abs function

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

Best related

Other by Bruce Hardy