一.divmod()函數介紹
divmod()函數也是python的內置函數,它是把除數和余數運算結果結合起來,返回一個包含商和余數的元組(a // b, a % b)
divmod(x, y)
參數: x,y都是數字,可以是整形也可以是浮點數,x 是分子,y 是分母;
返回值:返回一個元組(x//y,x%y);
二.divmod()函數使用
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:何以解憂 @Blog(個人博客地址): shuopython.com @WeChat Official Account(微信公眾號):猿說python @Github:www.github.com @File:python_divmod.py @Time:2019/12/07 21:25 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累! """ print("{}".format(type(divmod(9,6)))) print("{}".format(divmod(9,6))) print("{}".format(type(divmod(9.2,6.6)))) print("{}".format(divmod(9.2,6.6)))
輸出結果:
<class 'tuple'> (1, 3) <class 'tuple'> (1.0, 2.5999999999999996)
注意:divmod()函數只能接受整數或浮點數類型的參數不能使用字符串,否則會報錯!
猜你喜歡:
轉載請注明:猿說Python » python divmod函數
技術交流、商務合作請直接聯系博主
掃碼或搜索:猿說python

猿說python
微信公眾號 掃一掃關注