Python abs() function

Updated on Nov 08, 2019


The abs() function returns the absolute value (just magnitude without sign) of the number.

Its syntax is as follows:

abs(x) -> absolute value
Parameter Description
x Any numeric value

Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
>>> 
>>> abs(-45)
45
>>>
>>> 
>>> abs(-3.14)
3.14
>>> 
>>> 
>>> abs(10)
10
>>> 
>>> 
>>> abs(2+4j)
4.47213595499958
>>>
print(abs(-45))
print(abs(-3.14))
print(abs(10))
print(abs(2+4j))

The result is quite obvious for integers and floats. In case of a complex number z = x + yi the abs() function calculates the absolute value as follows:

Absolute value = |z| = √x² + y²

1
2
3
4
5
6
7
=> 2+4j
=> √2² + 4²
=> √4 + 16
=> √20
=> 2√5
=> 2*2.236
=> 4.472

Other Tutorials (Sponsors)