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

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

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

Python 中的楼层划分

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

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

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

立即学习“Python免费学习笔记(深入)”;

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?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1342770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 05:53:18
下一篇 2025年12月13日 05:53:30

相关推荐

发表回复

登录后才能评论
关注微信