abs(x)
Returns the absolute value of a number. The argument can be an integer, a floating-point number, or an object implementing abs(). If the argument is a complex number, its magnitude is returned.
Parameters
The abs() function takes one argument:
num: the number whose absolute value is to be returned.
The number can be:
- an integer
- a floating-point number
- a complex number
Return Value
The abs() function returns the absolute value of the given number.
- For integers, it returns the integer absolute value.
- For floating-point numbers, it returns the floating-point absolute value.
- For complex numbers, it returns the magnitude of the number.
Examples
number = -10
absolute = abs(number)
print(absolute)
# Output: 10
Get the absolute value of a number:
# a random integer
integer = -40
print('Absolute value of -40:', abs(integer))
# a random floating-point number
floating = -56.21
print('Absolute value of -56.21:', abs(floating))
# Output:
# Absolute value of -40: 40
# Absolute value of -56.21: 56.21
Get the magnitude of a complex number:
# a random complex number
complex_ = 3 - 4j
print('Absolute value of 3 - 4j:', abs(complex_))
# Output:
# Absolute value of 3 - 4j: 5.0