The abs() method returns the absolute value of the given number. If the number is a complex number, abs() returns its magnitude.
The abs() method takes a single argument:
- num - number whose absolute value is to be returned. The number can be:
- integer
- floating number
- complex number
The abs() method returns the absolute value of the given number.
- For integers - integer absolute value is returned
- For floating numbers - floating absolute value is returned
- For complex numbers - magnitude of the number is returned
Example -
# random integer
integer = -20
print('Absolute value of -20 is:', abs(integer))
#random floating number
floating = -30.33
print('Absolute value of -30.33 is:', abs(floating))
Output-
Absolute value of -20 is: 20
Absolute value of -30.33 is: 30.33