python divmod_Python divmod()
python divmod
Python divmod() function is used to perform division on two input numbers. The numbers should be non-complex and can be written in any format such as decimal, binary, hexadecimal etc.
Python divmod()函数用于对两个输入数字进行除法。 数字应该是非复杂的,并且可以以任何格式编写,例如十进制,二进制,十六进制等。
Python divmod() (Python divmod())
Python divmod()
syntax is:
Python divmod()
语法为:
divmod(a, b)
The output is a tuple consisting of their quotient and remainder when using integer division.
使用整数除法时,输出是一个由它们的商和余数组成的元组 。
For integer arguments, the result is the same as (a // b, a % b).
对于整数参数,结果与(a // b,a%b)相同。
For floating point numbers the result is (q, a % b), where q is usually (math.floor(a / b), a % b). In any case q * b + a % b is very close to a.
对于浮点数,结果为(q,a%b),其中q通常为(math.floor(a / b),a%b)。 无论如何,q * b + a%b非常接近a。
If a % b is non-zero it has the same sign as b, and 0 <= abs(a % b) < abs(b).
如果%b不为零,则它的符号与b相同,并且0 <= abs(a%b)<abs(b)。
带有整数的Python divmod()示例 (Python divmod() example with integers)
# simple example, returns (a // b, a % b) for integers
dm = divmod(10, 3)
print(dm)x, y = divmod(10, 3)
print(x)
print(y)dm = divmod(0xF, 0xF) # hexadecimal
print(dm)
Output:
输出:
(3, 1)
3
1
(1, 0)
带有浮点数的Python divmod()示例 (Python divmod() example with floats)
# floats, returns usually (math.floor(a / b), a % b)
dm = divmod(10.3, 3)
print(dm)dm = divmod(11.51, 3)
print(dm)dm = divmod(-11.51, 3)
print(dm)
Output:
输出:
(3.0, 1.3000000000000007)
(3.0, 2.51)
(-4.0, 0.4900000000000002)
Notice that in case of floating points, the q * b + a % b
is very close to a
in some cases.
注意,在浮点情况下, q * b + a % b
在某些情况下非常接近a
。
带有复数的divmod() (divmod() with complex numbers)
If we pass complex numbers as argument, we will get TypeError
.
如果我们将复数作为参数传递,则会得到TypeError
。
dm = divmod(3 + 2J, 3)
Output: TypeError: can't take floor or mod of complex number.
输出: TypeError: can't take floor or mod of complex number.
Reference: Official Documentation
参考: 官方文档
翻译自: https://www.journaldev.com/22813/python-divmod
python divmod
python divmod_Python divmod()相关推荐
- Python中的“ @”(@)符号有什么作用?
我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...
- 如何使用Python numpy.where()方法
In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...
- Python字符串title()
Python String title() Python字符串title() Python String title() function returns a title cased version ...
- Python字符串isdecimal()
Python String isdecimal() function returns True if all the characters in the string are decimal char ...
- Python字符串isalnum()
Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...
- Python字符串count()
Python String count() function returns the number of occurrences of a substring in the given string. ...
- Python字符串splitlines()
Python String splitlines() Python字符串splitlines() Python String splitlines() function returns the lis ...
- Python字符串join()方法
Python string join() method creates a string from an iterable. It joins all the iterable elements wi ...
- Python字符串index()
Python String index() function returns the lowest index where the specified substring is found. If t ...
最新文章
- c语言 静态链表插入排序,数据结构C语言版 表插入排序
- 嵌入式系统笔记之自动登录
- Lucene 4.X 倒排索引原理与实现: (3) Term Dictionary和Index文件 (FST详细解析)——直接看例子就明白了!!!...
- java 报500该怎么解决_关于servlet500错误 应该怎么解决
- 解决svn错误:post-commit hook failed (exit code 1) with output
- Multidex实现简要分析
- Authentication和Authorization的区别
- 什么是JAX-RS注释?
- 深入理解DOM事件机制系列第四篇——事件模拟
- python协同过滤调用包_简单的python协同过滤程序实例代码
- Java 能抵挡住 JavaScript 的进攻吗?
- launchpad乐器_PreSonus 发布 ATOM 打击垫控制器(视频)
- JavaScript TypeScript 学习总结
- Simulink仿真计算中保留特定位数的小数
- 物联网模块ESP8266-01S实现PC端和Android APP同时连接通信
- Ubuntu断点续存之wget
- 微信小程序网络请求异常怎么办_微信小程序网络超时的处理
- mipi传输距离3米_蓝牙网关有效传输距离是多少?
- Python到底是干啥的?
- python爬取谷歌学术参考文献的BibTex格式——基于selenium