Python abs() Method
The abs()
method returns the absolute value of the given number and returns a magnitude of a complex number.
Syntax:
abs(num)
Parameters:
num: An int, float, or complex number.
Return type:
Returns the absolute value of a number and the magnitude of a complex number.
The following example gets the absolute values of integer, float, and complex numbers.
print("Absolute value of -10 is: ",abs(-10))
print("Absolute value of +10 is: ", abs(+10))
print("Absolute value of -10.33 is: ", abs(-10.33))
print("Absolute value of 4 - 3j is: ", abs(4 - 3j))
Absolute value of -10 is: 10
Absolute value of +10 is: 10
Absolute value of -10.33 is: 10.33
Absolute value of 4 - 3j is: 5.0
The following gets an absolute value of binary, octal and hexadecimal numbers.
print("Absolute value of -0b10011 is: ", abs(-0b10011))
print("Absolute value of -0o23 is: ", abs(-0o13))
print("Absolute value of -0x42 is: ", abs(-0x42))
Absolute value of -0b10011 is: 19
Absolute value of -0o23 is: 19
Absolute value of -0x42 is: 19