Python abs()

The abs() function returns the absolute value of a number. In case of of a complex number, abs()  returns its magnitude.

Syntax:

abs(num)

Parameters

  • num - The number can be:
    • integer
    • float
    • complex number

Return value

 The abs() function  returns an absolute value of the given number. In case of  complex number  it returns  a magnitude of the number.

Example : Get an absolute value

intval = -2

print('The absolute value of -2 is:', abs(intval))

floatval = -1.43

print('The absolute value of -1.43 is:', abs(floatval))

Output

Absolute value of -20 is: 20
Absolute value of -1.43 is: 1.43

​​​​​​​Example : Get the magnitude of a complex number

complexval = ( 2 + 3j)

print("The mangitute of the 2 + 3j is :" abs(complexval))

Output

The magnitude of 2 + 3j  is: 3.60555