Integer division 整除(%)Python中是否存在错误?
win32上的Python 3.7.4(tags/v3.7.4:e09359112e,2019年7月8日,20:34:20)[MSC v.1916 64位(AMD64)] 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证” 7%5 2Integer division 整除(%)Python中是否存在错误?,integer-division,Integer Division,win32上的Python 3.7.4(tags/v3.7.4:e09359112e,2019年7月8日,20:34:20)[MSC v.1916 64位(AMD64)] 有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证” 7%5 2 8%5 3 6%5 一, 部门/ 使用整数作为输入,除法将给出整数结果(Python2+) 至少有一个非整数作为输入(Python2+),您将得到一个非整数结果 >>> 10/1.5 6.666666666666667 >>
8%5 3
6%5 一,
部门/ 使用整数作为输入,除法将给出整数结果(Python2+) 至少有一个非整数作为输入(Python2+),您将得到一个非整数结果
>>> 10/1.5
6.666666666666667
>>> 10/4.0
2.5
Python3:整数除法是/
>>> 10/4 # Python 3
2.5
>>> 10//4 # Python 3 integer division
2
模%(=余数)
模是整数除法的余数
>>> 7%5 # = What is the remainder of the division of 7 by 5
2
>>> 8%5
3
>>> 10%5
0
您使用的是模运算
7%5=2
其正确性
对于整数除法,您必须使用“/”,例如:
7//5
。如果您想得到浮点结果,可以使用7/5
这里怎么了?7%5表示python3中的“7乘5的欧几里德除法的其余部分是2”整数除法是python2中唯一的整数除法。这是正确的,Matt相应地更新了答案。
>>> 7%5 # = What is the remainder of the division of 7 by 5
2
>>> 8%5
3
>>> 10%5
0