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在windows下实现备份程序实例
Jul 04 Python
详解Django框架中用户的登录和退出的实现
Jul 23 Python
Python检测生僻字的实现方法
Oct 23 Python
名片管理系统python版
Jan 11 Python
Python从零开始创建区块链
Mar 06 Python
实例讲解python中的序列化知识点
Oct 08 Python
Python3.7 dataclass使用指南小结
Feb 22 Python
Python 函数返回值的示例代码
Mar 11 Python
Python 实现毫秒级淘宝抢购脚本的示例代码
Sep 16 Python
解决python多线程报错:AttributeError: Can't pickle local object问题
Apr 08 Python
pyinstaller打包找不到文件的问题解决
Apr 15 Python
宝塔更新Python及Flask项目的部署
Apr 11 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
火车头采集器3.0采集图文教程
2007/03/17 PHP
destoon实现VIP排名一直在前面排序的方法
2014/08/21 PHP
Linux系统下PHP-FPM的安装和配置教程
2015/08/17 PHP
PHP实现的mysql主从数据库状态检测功能示例
2017/07/20 PHP
PHP发送邮件确认验证注册功能示例【修改别人邮件类】
2019/11/09 PHP
js 动态添加标签(新增一行,其实很简单,就是几个函数的应用)
2009/03/26 Javascript
javascript 解析url的search方法
2010/02/09 Javascript
Javascript中apply、call、bind的巧妙使用
2016/08/18 Javascript
vue2.0+webpack环境的构造过程
2016/11/08 Javascript
Vue.js 60分钟快速入门教程
2017/03/28 Javascript
使用vue实现简单键盘的示例(支持移动端和pc端)
2017/12/25 Javascript
点击按钮弹出模态框的一系列操作代码实例
2019/03/29 Javascript
详解如何在vue项目中使用layui框架及采坑
2019/05/05 Javascript
Vue中实现权限控制的方法示例
2019/06/07 Javascript
基于Nuxt.js项目的服务端性能优化与错误检测(容错处理)
2019/10/23 Javascript
基于VSCode调试网页JavaScript代码过程详解
2020/07/20 Javascript
三剑客:offset、client和scroll还傻傻分不清?
2020/12/04 Javascript
[03:30]DOTA2完美“圣”典精彩集锦
2016/12/27 DOTA
python爬虫_微信公众号推送信息爬取的实例
2017/10/23 Python
python PyTorch预训练示例
2018/02/11 Python
使用python实现抓取腾讯视频所有电影的爬虫
2019/04/15 Python
Python字符串内置函数功能与用法总结
2019/04/16 Python
Pyqt5 基本界面组件之inputDialog的使用
2019/06/25 Python
使用Pandas将inf, nan转化成特定的值
2019/12/19 Python
用HTML5实现手机摇一摇的功能的教程
2012/10/30 HTML / CSS
美国的Eastbay旗下的运动款子品牌:Final-Score
2018/01/01 全球购物
Pop In A Box英国:Funko POP搪胶公仔
2019/05/27 全球购物
高中生自我评语大全
2014/01/19 职场文书
幼儿园庆六一活动方案
2014/03/06 职场文书
法律进学校实施方案
2014/03/15 职场文书
法学求职信
2014/06/22 职场文书
2014年组织部工作总结
2014/11/14 职场文书
幼儿园班级工作总结2015
2015/05/25 职场文书
先进工作者主要事迹材料
2015/11/03 职场文书
2017春节晚会开幕词
2016/03/03 职场文书
python实现自定义日志的具体方法
2021/05/28 Python