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版本不允許處理復數。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM