The Python function abs serves the purpose of providing the absolute value of a given number. It accepts a single parameter, which is the number for which the absolute value is to be calculated. This parameter can either be an integer or a floating-point number. In cases where the argument is a complex number, abs will return its magnitude instead.
Python abs Function Syntax
It has the following syntax:
Example
abs (num)
Parameters
num: A number whose absolute value is to be returned. The number can be:
- integer
- floating number
- complex number
Return
It provides the absolute value of the given number.
Python abs Function Example 1
Let’s examine an illustration to determine the absolute value of a numerical figure.
Example
# integer number
integer = -40
print('Absolute value of -40 is:', abs(integer))
# floating number
floating = -40.83
print('Absolute value of -40.83 is:', abs(floating))
Output:
Output
Absolute value of -40 is: 40
Absolute value of -40.83 is: 40.83
Python abs Function Example 2
This example shows magnitude of a complex number.
Example
#complex number
complex = (3 - 4j)
print('Magnitude of 3 - 4j is:', abs(complex))
Output:
Output
Magnitude of 3 - 4j is: 5.0