How to get absolute value in Python

#StandWithUkraine
Today, 15th August 2022, Ukraine is still bravely fighting for democratic values, human rights and peace in whole world. Russians ruthlessly kill all civilians in Ukraine including childs and destroy their cities. We are uniting against Putinโ€™s invasion and violence, in support of the people in Ukraine. You can help by donating to Ukrainian's army.

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
0
Bruce Hardy profile picture
Nov 01, 2020
by Bruce Hardy
Did it help you?
Yes !
No

Best related