python 中的divmod数字处理函数浅析


Posted in Python onOctober 17, 2017

divmod(a,b)函数

中文说明:

divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数

返回结果类型为tuple

参数:

a,b可以为数字(包括复数)

版本:

在python2.3版本之前不允许处理复数,这个大家要注意一下

英文说明:

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代码实例:

>>> divmod(9,2)
(4, 1)
>>> divmod(11,3)
(3, 2)
>>> divmod(1+2j,1+0.5j)
((1+0j), 1.5j)

PS:Python标准库:内置函数divmod(a, b)

本函数是实现a除以b,然后返回商与余数的元组。如果两个参数a,b都是整数,那么会采用整数除法,结果相当于(a//b, a % b)。如果a或b是浮点数,相当于(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))

输出结果如下:

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)

总结

以上所述是小编给大家介绍python divmod数字处理函数浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
Python自动登录126邮箱的方法
Jul 10 Python
pip安装Python库时遇到的问题及解决方法
Nov 23 Python
python3.6编写的单元测试示例
Aug 17 Python
django中的图片验证码功能
Sep 18 Python
wxPython实现画图板
Aug 27 Python
python3 实现函数写文件路径的正确方法
Nov 27 Python
CentOS7下安装python3.6.8的教程详解
Jan 03 Python
pytorch:实现简单的GAN示例(MNIST数据集)
Jan 10 Python
python函数定义和调用过程详解
Feb 09 Python
Python利用matplotlib绘制折线图的新手教程
Nov 05 Python
解决python 执行shell命令无法获取返回值的问题
Dec 05 Python
python绘图pyecharts+pandas的使用详解
Dec 13 Python
Python中的id()函数指的什么
Oct 17 #Python
Python中int()函数的用法浅析
Oct 17 #Python
一文总结学习Python的14张思维导图
Oct 17 #Python
python 中的int()函数怎么用
Oct 17 #Python
python遍历序列enumerate函数浅析
Oct 17 #Python
浅谈python中的正则表达式(re模块)
Oct 17 #Python
深入理解Django的自定义过滤器
Oct 17 #Python
You might like
给海燕B411配件机起死回生配上件
2021/03/02 无线电
PHP+DBM的同学录程序(5)
2006/10/09 PHP
PHP函数学习之PHP函数点评
2012/07/05 PHP
php使用ICQ网关发送手机短信
2013/10/30 PHP
PHP解码unicode编码的中文字符代码分享
2014/08/13 PHP
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
2014/11/29 PHP
PHP闭包函数详解
2016/02/13 PHP
jQuery使用手册之二 DOM操作
2007/03/24 Javascript
Jquery+CSS3实现一款简洁大气带滑动效果的弹出层
2013/05/15 Javascript
javascript得到当前页的来路即前一页地址的方法
2014/02/18 Javascript
jQuery中focus事件用法实例
2014/12/26 Javascript
JavaScript数组的一些奇葩行为
2016/01/25 Javascript
js防阻塞加载的实现方法
2016/09/09 Javascript
详解ECharts使用心得总结
2016/12/06 Javascript
基于Vue如何封装分页组件
2016/12/16 Javascript
Bootstrap table 定制提示语的加载过程
2017/02/20 Javascript
angular中的post请求处理示例详解
2020/06/30 Javascript
打印出python 当前全局变量和入口参数的所有属性
2009/07/01 Python
Python基于Matplotlib库简单绘制折线图的方法示例
2017/08/14 Python
Python创建二维数组实例(关于list的一个小坑)
2017/11/07 Python
python 批量修改/替换数据的实例
2018/07/25 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
2018/10/28 Python
Python函数装饰器常见使用方法实例详解
2019/03/30 Python
使用Python的OpenCV模块识别滑动验证码的缺口(推荐)
2019/05/10 Python
Django基于客户端下载文件实现方法
2020/04/21 Python
Python如何自动获取目标网站最新通知
2020/06/18 Python
HTML5 Canvas实现360度全景图的示例代码
2018/01/29 HTML / CSS
英国健身仓库:Bodybuilding Warehouse
2019/03/06 全球购物
会计应聘求职信范文
2013/12/17 职场文书
经贸韩语专业大学生职业规划
2014/02/14 职场文书
2014年新生军训方案
2014/05/01 职场文书
物业品质提升方案
2014/06/08 职场文书
2014年公务员退休工资改革方案
2014/10/01 职场文书
2015年幼儿园大班工作总结
2015/04/25 职场文书
情侣之间的道歉短信
2015/05/12 职场文书
2015年大学迎新晚会总结
2015/07/16 职场文书