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通过递归遍历出集合中所有元素的方法
Feb 25 Python
python通过yield实现数组全排列的方法
Mar 18 Python
Windows下用py2exe将Python程序打包成exe程序的教程
Apr 08 Python
使用Django的模版来配合字符串翻译工作
Jul 27 Python
如何在Python函数执行前后增加额外的行为
Oct 20 Python
Python AES加密实例解析
Jan 18 Python
用uWSGI和Nginx部署Flask项目的方法示例
May 05 Python
利用python读取YUV文件 转RGB 8bit/10bit通用
Dec 09 Python
Python爬虫谷歌Chrome F12抓包过程原理解析
Jun 04 Python
Python调用shell cmd方法代码示例解析
Jun 18 Python
详解BeautifulSoup获取特定标签下内容的方法
Dec 07 Python
python使用pycharm安装pyqt5以及相关配置
Apr 22 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
PHP+ACCESS 文章管理程序代码
2010/06/21 PHP
PHP定时任务延缓执行的实现
2014/10/08 PHP
PHP命名空间(namespace)原理与用法详解
2019/12/11 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
PHP执行普通shell命令流程解析
2020/08/24 PHP
js 鼠标拖动对象 可让任何div实现拖动效果
2009/11/09 Javascript
jQuery 学习第五课 Ajax 使用说明
2010/05/17 Javascript
基于JavaScript实现定时跳转到指定页面
2016/01/01 Javascript
jQuery实现简单的点赞效果
2020/05/29 Javascript
jQuery+php实时获取及响应文本框输入内容的方法
2016/05/24 Javascript
只需五句话搞定JavaScript作用域(经典)
2016/07/26 Javascript
几种响应式文字详解
2017/05/19 Javascript
解决Vue 浏览器后退无法触发beforeRouteLeave的问题
2017/12/24 Javascript
React Native自定义控件底部抽屉菜单的示例
2018/02/08 Javascript
vue使用map代替Aarry数组循环遍历的方法
2020/04/30 Javascript
解决vue组件销毁之后计时器继续执行的问题
2020/07/21 Javascript
[07:49]2014DOTA2国际邀请赛 Newbee夺冠后采访xiao8坦言奖金会上交
2014/07/23 DOTA
在win和Linux系统中python命令行运行的不同
2016/07/03 Python
Python中不同进制的语法及转换方法分析
2016/07/27 Python
Python字符编码与函数的基本使用方法
2017/09/30 Python
python 把列表转化为字符串的方法
2018/10/23 Python
Python弹出输入框并获取输入值的实例
2019/06/18 Python
django fernet fields字段加密实践详解
2019/08/12 Python
PyQt5 如何让界面和逻辑分离的方法
2020/03/24 Python
基于python实现上传文件到OSS代码实例
2020/05/09 Python
Scrapy项目实战之爬取某社区用户详情
2020/09/17 Python
Opencv+Python识别PCB板图片的步骤
2021/01/07 Python
新西兰廉价汽车租赁:Snap Rentals
2018/09/14 全球购物
对标管理实施方案
2014/03/12 职场文书
诚信考试承诺书
2014/03/27 职场文书
开学寄语大全
2014/04/08 职场文书
2014年团总支工作总结
2014/11/21 职场文书
礼仪培训心得体会
2016/01/22 职场文书
CSS3新特性详解(五):多列columns column-count和flex布局
2021/04/30 HTML / CSS
Mysql Online DDL的使用详解
2021/05/20 MySQL
python 安全地删除列表元素的方法
2022/03/16 Python