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 异常处理实例详解
Mar 12 Python
python简单程序读取串口信息的方法
Mar 13 Python
python获取本机mac地址和ip地址的方法
Apr 29 Python
用Python删除本地目录下某一时间点之前创建的所有文件的实例
Dec 14 Python
Python序列循环移位的3种方法推荐
Apr 09 Python
python开发游戏的前期准备
May 05 Python
Python异常处理例题整理
Jul 07 Python
Python谱减法语音降噪实例
Dec 18 Python
Python enumerate() 函数如何实现索引功能
Jun 29 Python
简单的Python人脸识别系统
Jul 14 Python
pycharm使用技巧之自动调整代码格式总结
Nov 04 Python
pycharm 复制代码出现空格的解决方式
Jan 15 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+MySQL5.0中文乱码解决方法
2006/11/20 PHP
PHP+MYSQL会员系统的开发实例教程
2014/08/23 PHP
PHP读取CURL模拟登录时生成Cookie文件的方法
2014/11/04 PHP
php+ajax实现无刷新分页
2015/11/18 PHP
[原创]php简单防盗链验证实现方法
2016/07/09 PHP
宝塔面板在NGINX环境中TP5.1如何运行?
2021/03/09 PHP
JavaScrip单线程引擎工作原理分析
2010/09/04 Javascript
js变量、作用域及内存详解
2014/09/23 Javascript
js实现头像图片切割缩放及无刷新上传图片的方法
2015/07/17 Javascript
JS实现的简单鼠标跟随DiV层效果完整实例
2015/10/31 Javascript
js密码强度校验
2015/11/10 Javascript
常见的javascript跨域通信方法
2015/12/31 Javascript
JavaScript继承模式粗探
2016/01/12 Javascript
jquery代码规范让代码越来越好看
2017/02/03 Javascript
python 解析html之BeautifulSoup
2009/07/07 Python
用PyQt进行Python图形界面的程序的开发的入门指引
2015/04/14 Python
Python unittest单元测试框架总结
2018/09/08 Python
Django框架组成结构、基本概念与文件功能分析
2019/07/30 Python
Pytorch中Tensor与各种图像格式的相互转化详解
2019/12/26 Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
2020/03/03 Python
Pandas之read_csv()读取文件跳过报错行的解决
2020/04/21 Python
python3.7+selenium模拟淘宝登录功能的实现
2020/05/26 Python
利用python爬取有道词典的方法
2020/12/08 Python
Harrods英国:世界领先的奢侈品百货商店
2020/09/23 全球购物
测绘工程专业个人自我评价
2013/12/01 职场文书
水利公司纪检监察自我鉴定
2014/02/25 职场文书
村长贪污检举信
2014/04/04 职场文书
《生命 生命》教学反思
2014/04/19 职场文书
学生无故旷课检讨书
2014/09/20 职场文书
领导干部作风建设自查报告
2014/10/23 职场文书
简单的离婚协议书范本
2014/11/16 职场文书
幸福来敲门观后感
2015/06/04 职场文书
雷锋的观后感
2015/06/10 职场文书
拿破仑传读书笔记
2015/07/01 职场文书
从贫穷到富有,是知识技能和学习力的差别
2019/08/20 职场文书
解析redis hash应用场景和常用命令
2021/08/04 Redis