python 中的楼层划分操作数的除法,结果是除去小数点后的数字所得的商。但如果其中一个操作数为负数,则结果将被取整,即从零开始舍入(向负无穷大舍入)。
在python中,//是双斜杠运算符,即地板除法。//运算符用于执行将结果向下舍入到最近整数的除法。//运算符的使用非常简单。我们还将与单斜杠除法的结果进行比较。让我们首先看一下语法−
a 和 b 是第一个st 和第二个nd 数字:
a // b
example of // (双斜杠) 运算符让我们现在看一个在python中实现双斜杠运算符的例子 -
a = 37b = 11# 1st numberprint(the 1st number = ,a)# 2nd numberprint(the end number = ,b)# dividing using floor divisionres = a // bprint(result of floor division = , res)
输出('the 1st number = ', 37)('the end number = ', 11)('result of floor division = ', 3)
使用负数实现 //(双斜杠)运算符example 的中文翻译为:示例我们将尝试使用双斜杠运算符和负数作为输入。让我们看一下示例
# a negative number with a positive numbera = -37b = 11# 1st numberprint(the 1st number = ,a)# 2nd numberprint(the end number = ,b)# dividing using floor divisionres = a // bprint(result of floor division = , res)
输出('the 1st number = ', -37)('the end number = ', 11)('result of floor division = ', -4)
example 的中文翻译为:示例正如您在上面的输出中看到的,使用负数不会影响舍入。结果向下取整。现在,我们可以使用双斜杠运算符检查 -22 // 10 -
# a negative number with a positive numbera = -22b = 10# 1st numberprint(the 1st number = ,a)# 2nd numberprint(the end number = ,b)# dividing using floor divisionres = a // bprint(result of floor division = , res)
输出('the 1st number = ', -22)('the end number = ', 10)('result of floor division = ', -3)
以上就是为什么在python中,-22 // 10 返回 -3?的详细内容。
