一条线上的多重比较操作员
我正在介绍Python课程的介绍,在测验上有一个问题,我无法做出头或尾巴。鉴于此代码,问题询问将分配给X的值什么值:
z = 2
y = 1
x = y < z or z > y and y > z or z < y
我对本语句中的分配的位置感到非常困惑。我是否应该将其读为“如果y小于z,x等于y”?在这种情况下,我什至无法开始理解如何阅读“,”,“和”。
提前致谢。
I'm taking an intro to python course, and there was one question on a quiz that I could not make heads or tails of. Given this code, the question asks what value will be assigned to x:
z = 2
y = 1
x = y < z or z > y and y > z or z < y
I'm really confused about where the assignment is in this statement. Should I read this as "x equals y if y is less than z"? I can't even begin to understand how to read "or" and "and" in this context.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须这样阅读,x将是布尔值(是或否),每个比较表达方式也将是
you have to read it like this, x will be a boolean (true or false) and so will each of the comparison expressions
在Python中,
和
具有比或
更高的优先级,因此与与
与与与
与
In Python,
and
has higher precedence thanor
, sois the same as
is the same as
is the same as
在
中,Python
,和
的优先级高于或
。因此,和
将首先评估(从左到右),然后将评估或
(从左到右),除非有()
。因此,在您的情况下,要评估的第一件事是
z&gt; y和y&gt; z
,步骤如下:In
python
,and
have higher priority thanor
. So,and
will be evaluated first (left to right) thenor
will be evaluated (left to right), unless there are()
.So, in your case, first thing to be evaluated are
z > y and y > z
, and steps follows like: