abs() function returns the absolute value of the given number.
value = -10
absolute_value = abs(value)
print(absolute_value) # output is 10
Syntax
abs(num)
Parameters
abs() takes a single argument; num, i.e. a number. The number can be integer number, floating-point number or complex number.
Return Value
- For Integers – Absolute value of given integer is returned.
- For Floating-point Numbers – Absolute value of given floating-point number is returned.
- For Complex Numbers – Magnitude of the given number is returned.
Example
Print the absolute values of given numbers.
n = -15
f = -3.15
c = (5-4j)
print('Absolute value of -15 is',abs(n))
print('Absolute value of -3.15 is',abs(f))
print('Magnitude of 5-4j is',abs(c))
Output
Absolute value of -15 is 15
Absolute value of -3.15 is 3.15
Magnitude of 5-4j is 6.4031