Python abs() function
Python abs() Function
Description
The abs() function returns the absolute value of x, that is, the positive distance between x and zero. The argument can be an integer, floating-point number, or complex number. If the argument is a complex number, its modulus is returned. The modulus of a complex number is the square root of the sum of the squares of its real and imaginary parts.
abs(x+ij)=sqrt{x^{2}+y^{2}}
Syntax
The syntax of the abs() function is as follows:
abs(x)
Parameters
- x − This is a numeric expression.
Return Value
This method returns the absolute value of x.
Example
The following example demonstrates how to use the abs() function.
x = -45
y = abs(x)
print ("x: ",x, "abs(x): ", y)
x = 100.12
y = abs(x)
print ("x: ",x, "abs(x): ", y)
x = 2+3j
y = abs(x)
print ("x: ",x, "abs(x): ", y)
When we run the above program, it produces the following results –
x: -45 abs(x): 45
x: 100.12 abs(x): 100.12
x: (2+3j) abs(x): 3.605551275463989