How to get absolute value of a number in python

Published: May 01, 2018

DMCA.com Protection Status

To get absolute value of a number in python, there is the "built-in" function abs(), example:

>>> x = - 3
>>> abs(x)
3

with a float:

>>> x = - 6.0
>>> abs(x)
6.0

Note: with a complex number, the function abs() gives the modulus

>>> z = complex(3,4)
>>> abs(z)
5.0

since

$\sqrt{3^2+4^2} = \sqrt{9+16} = 5$

References

Links Site
abs() python doc