Python 内建函数 - divmod(a, b)

本文详细介绍了Python中divmod函数的功能及用法。该函数接受两个非复数数字作为输入,返回一个包含整数除法得到的商和余数的元组。文章通过实例展示了整数和浮点数情况下divmod的不同表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Manual

Take two (non complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply. For 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).

直译

取得两个数字(非复数)做参数,并返回一对数,其中包含它们的商和余数(当使用整数除法时)。混合运算类型下,支持二进制算数运算符。对于整数,结果等同于(a // b, a % b)。对于浮点数,结果等同于(q, a % b),其中q通常是math.floor(a / b),但可能比它小1。任何情况下,q * b + a % b都非常接近a,若a % b非零,它与b符号相同,且0 <= abs(a % b) < abs(b)。

实例

>>> divmod(2,4)
(0, 2)
>>> divmod(5, 2)
(2, 1)
>>> a = 5.3
>>> b = 3.14
>>> a_div_b = divmod(a, b)
>>> q = a_div_b[0]
>>> q
1.0
>>> x = q * b + a % b
>>> x == a
True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值