python内置函数 divmod()

先来看一下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版本不允许处理复数。

 
posted @ 2018-01-20 15:47  椿与湫  阅读(2083)  评论(0编辑  收藏  举报