英文文檔:
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. 如果參數是一個複數,此方法傳回此複數的絕對值(此複數與它的共轭複數的乘積的平方根)
示例:
>>> abs(34)
34
>>> abs(-2)
2
>>> abs(0)
>>> abs(454L) #python3已經沒有了 long 類型,定義的時候自動識别
File "", line 1
abs(454L)
^
SyntaxError: invalid syntax
>>> c1=comples(1,1)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'comples' is not defined
>>> c1=complex(1,1)
>>> c1
(1+1j)
>>> abs(c1)
1.4142135623730951
>>>
>>> c2 = complex(-1,-1)
>>> c2 #複數
(-1-1j)
>>> abs(c2) #複數絕對值
1.4142135623730951
>>>