布尔值的非和加法运算的评估

发布于 2025-01-15 20:31:47 字数 300 浏览 2 评论 0原文

有人可以解释一下这两个代码片段中的操作顺序吗?我不明白为什么我得到不同的输出: True 在以下情况下:

print(not not True + True + False)

2 在以下情况下:

print((not not True) + True + False)

Can someone please explain the order of operations in these two code snippets? I don't understand why I get different outputs: True in the case of:

print(not not True + True + False)

and 2 in the case of:

print((not not True) + True + False)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2025-01-22 20:31:47

使用此表作为运算符优先级,我们看到+ 的优先级高于 not。因此:

not not True + True + False

相当于 相当于

not not (True + True + False)

相当于 相当于

not not (2)

相当于

not False

相当于

True

我们可以对第二次打印遵循类似的过程:

(not not True) + True + False

相当于:

True + True + False

通过布尔加法,结果为:

2

Using this table for operator precedence, we see that + has a higher precedence than not. So:

not not True + True + False

is equivalent to

not not (True + True + False)

which is equivalent to

not not (2)

which is equivalent to

not False

which is equivalent to

True

We can follow a similar procedure for the second print:

(not not True) + True + False

is equivalent to:

True + True + False

which, by boolean addition, results in:

2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文