Calculating absolute value in Python

K.Kane picture K.Kane · Sep 12, 2015 · Viewed 16.5k times · Source

How do I make a program that asks for one floating point number in Python and then calculates the absolute value? I've already tried the asking, but I can't make the line where it calculates the absolute value.

Answer

user3917838 picture user3917838 · Sep 12, 2015

You can do it with the built-in abs() function:

absolute_val = abs(x)

Otherwise, without the built-in function, use math:

absolute_val = (x ** 2) ** 0.5