Python number method abs() returns absolute value of x – the (positive) distance between x and zero.
If the number is a complex number, abs() returns its magnitude.
The syntax of abs() method is:
1 2 3 |
abs( x ) |
Parameters
The abs() method takes a single argument:
x – number whose absolute value is to be returned. The number can be:
-
- integer
- floating number
- complex number
Return Value
This method returns absolute value of x.
Example 1
The following example shows the usage of abs() method.
1 2 3 4 5 6 7 8 9 |
# random integer integer = -15 print('Absolute value of -15 is:', abs(integer)) #random floating number floating = -3.14 print('Absolute value of -3.14 is:', abs(floating)) |
Output:
Example 2
The following example shows the usage of abs() method that get magnitude of a complex number
1 2 3 4 5 |
# random complex number complex = (3 - 4j) print('Magnitude of 3 - 4j is:', abs(complex)) |
Output: