英文文档:
abs(x)
Return the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned
说明:
1. 返回数字的绝对值,参数可以是整数、浮点数或者复数
2. 如果参数是一个复数,此方法返回此复数的绝对值(此复数与它的共轭复数的乘积的平方根)
示例:
>>> help(abs)
Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
>>> abs(2)#正整数
2
>>> abs(-2)#负整数
2
>>> abs(0)#0
0
>>> abs(2.3)#正浮点数
2.3
>>> abs(-2.3)#负浮点数
2.3
>>> c1 = complex(1,1)
>>> c1 #复数
(1+1j)
>>> abs(c1) #复数绝对值
1.4142135623730951
>>> c2 = complex(-1,-1)
>>> c2 #复数
(-1-1j)
>>> abs(c2) #复数绝对值
1.4142135623730951
>>>