为什么在Python中,-22 // 10 返回 -3?

WBOY
发布: 2023-09-13 13:13:02
转载
910 人浏览过

为什么在Python中,-22 // 10 返回 -3?

在Python中,-22//10返回-3,因为底除法的概念,即双斜杠运算符。 // 是双斜杠,即算术运算符。我们先来了解一下。

Python 中的楼层划分

操作数的除法,结果是除去小数点后的数字所得的商。但如果其中一个操作数为负数,则结果将被取整,即从零开始舍入(向负无穷大舍入)。

在Python中,//是双斜杠运算符,即地板除法。//运算符用于执行将结果向下舍入到最近整数的除法。//运算符的使用非常简单。我们还将与单斜杠除法的结果进行比较。让我们首先看一下语法−

a 和 b 是第一个st 和第二个nd 数字:

a // b
登录后复制

Example of // (双斜杠) 运算符

让我们现在看一个在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)
登录后复制

使用负数实现 //(双斜杠)运算符

Example

的中文翻译为:

示例

我们将尝试使用双斜杠运算符和负数作为输入。让我们看一下示例

# 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)
登录后复制

Example

的中文翻译为:

示例

正如您在上面的输出中看到的,使用负数不会影响舍入。结果向下取整。现在,我们可以使用双斜杠运算符检查 -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中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!