在Python中,-22//10返回-3,因为底除法的概念,即双斜杠运算符。 // 是双斜杠,即算术运算符。我们先来了解一下。
操作数的除法,结果是除去小数点后的数字所得的商。但如果其中一个操作数为负数,则结果将被取整,即从零开始舍入(向负无穷大舍入)。
在Python中,//是双斜杠运算符,即地板除法。//运算符用于执行将结果向下舍入到最近整数的除法。//运算符的使用非常简单。我们还将与单斜杠除法的结果进行比较。让我们首先看一下语法−
a 和 b 是第一个st 和第二个nd 数字:
a // b
让我们现在看一个在Python中实现双斜杠运算符的例子 -
a = 37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)
('The 1st Number = ', 37) ('The end Number = ', 11) ('Result of floor division = ', 3)
我们将尝试使用双斜杠运算符和负数作为输入。让我们看一下示例
# A negative number with a positive number a = -37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)
('The 1st Number = ', -37) ('The end Number = ', 11) ('Result of floor division = ', -4)
正如您在上面的输出中看到的,使用负数不会影响舍入。结果向下取整。现在,我们可以使用双斜杠运算符检查 -22 // 10 -
# A negative number with a positive number a = -22 b = 10 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)
('The 1st Number = ', -22) ('The end Number = ', 10) ('Result of floor division = ', -3)
以上是为什么在Python中,-22 // 10 返回 -3?的详细内容。更多信息请关注PHP中文网其他相关文章!