python 中的divmod數(shù)字處理函數(shù)淺析
divmod(a,b)函數(shù)
中文說明:
divmod(a,b)方法返回的是a//b(除法取整)以及a對(duì)b的余數(shù)
返回結(jié)果類型為tuple
參數(shù):
a,b可以為數(shù)字(包括復(fù)數(shù))
版本:
在python2.3版本之前不允許處理復(fù)數(shù),這個(gè)大家要注意一下
英文說明:
Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using long division. With mixed operand types, the rules for binary arithmetic operators apply. For plain and long integers, the result is the same as (a // b, a % b). For floating point numbers the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. In any case q * b + a % b is very close to a, if a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).
Changed in version 2.3: Using divmod() with complex numbers is deprecated.
python代碼實(shí)例:
>>> divmod(9,2) (4, 1) >>> divmod(11,3) (3, 2) >>> divmod(1+2j,1+0.5j) ((1+0j), 1.5j)
PS:Python標(biāo)準(zhǔn)庫(kù):內(nèi)置函數(shù)divmod(a, b)
本函數(shù)是實(shí)現(xiàn)a除以b,然后返回商與余數(shù)的元組。如果兩個(gè)參數(shù)a,b都是整數(shù),那么會(huì)采用整數(shù)除法,結(jié)果相當(dāng)于(a//b, a % b)。如果a或b是浮點(diǎn)數(shù),相當(dāng)于(math.floor(a/b), a%b)。
例子:
#divmod() print('divmod(2, 4):', divmod(2, 4)) print('divmod(28, 4):', divmod(28, 4)) print('divmod(27, 4):', divmod(27, 4)) print('divmod(25.6, 4):', divmod(25.6, 4)) print('divmod(2, 0.3):', divmod(2, 0.3))
輸出結(jié)果如下:
divmod(2, 4): (0, 2) divmod(28, 4): (7, 0) divmod(27, 4): (6, 3) divmod(25.6, 4): (6.0, 1.6000000000000014) divmod(2, 0.3): (6.0, 0.20000000000000007)
總結(jié)
以上所述是小編給大家介紹python divmod數(shù)字處理函數(shù)淺析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Python實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫(kù)并把備份數(shù)據(jù)庫(kù)郵件發(fā)送
這篇文章主要介紹了Python實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫(kù)并把備份數(shù)據(jù)庫(kù)郵件發(fā)送的相關(guān)資料,需要的朋友可以參考下2018-03-03Python實(shí)現(xiàn)向列表或數(shù)組添加元素
Python中的列表是一種動(dòng)態(tài)數(shù)組,可以存儲(chǔ)不同數(shù)據(jù)類型的元素,并提供多種方法進(jìn)行元素的添加和刪除,列表是Python中非常靈活和強(qiáng)大的數(shù)據(jù)結(jié)構(gòu),可以通過索引訪問、修改和操作列表中的元素,列表的創(chuàng)建十分簡(jiǎn)單,只需使用方括號(hào)括起元素,并用逗號(hào)分隔2024-09-09Django 前后臺(tái)的數(shù)據(jù)傳遞的方法
本篇文章主要介紹了Django 前后臺(tái)的數(shù)據(jù)傳遞的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Python lambda函數(shù)基本用法實(shí)例分析
這篇文章主要介紹了Python lambda函數(shù)基本用法,結(jié)合實(shí)例較為詳細(xì)的分析了Python lambda函數(shù)的功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-03-03Django如何配置mysql數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Django配置mysql數(shù)據(jù)庫(kù)的詳細(xì)步驟,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05