先來看一下builtins.py中的代碼:
def divmod(x, y): # known case of builtins.divmod """ Return the tuple (x//y, x%y). Invariant: div*y + mod == x. """ return (0, 0)
python divmod() 函數把除數和余數運算結果結合起來,返回一個包含商和余數的元組(x//y, x%y)。
>>> divmod(27,5) (5, 2) >>> divmod(6,6) (1, 0) >>> divmod(6,3.0) (2.0, 0.0) >>> divmod(1+2j,1+2j) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't take floor or mod of complex number. >>>
有些python版本不允許處理復數。