Python abs() Function
The Python abs() function returns absolute value of a number. Absolute value of a number is defined as its distance from 0 on a number line and it is always positive.
Syntax
abs(number)
Parameters
number |
Required. A number. |
Example: Absolute value of a number
In the example below, abs() function is used to find out the absolute value of a given number.
MyNumber = 3.5 print(abs(MyNumber)) MyNumber = -5.5 print(abs(MyNumber))
The output of the above code will be:
3.5 5.5
Example: Absolute value of a complex number
Absolute value of a complex number is defined as its distance from origin on Argand plane. The abs() function can also be used to find out the absolute value of a given complex number.
MyNumber = 3+4j print(abs(MyNumber))
The output of the above code will be:
5.0
❮ Python Built-in Functions