英文:
Why is "False == False != True" True in Python but false in JavaScript?
问题
我的学员刚刚联系我,问为什么在Python中 False == False != True
的表达式的结果是 True
,而在JavaScript中是 false
。
我认为这个说法是错误的,无论如何解决,它在我看来都返回 False
。
以下是详细解释:
给定:
False == False != True
#情况1:
False == False => True
True != True => False
#情况2:
False != True => True
False == True => False
我是否漏掉了一些明显的东西?我尝试了使用JS的 !=
和 ===
但由于类型相同,它仍然产生相同的输出。
英文:
My trainee just reached out to me and asked why False == False != True
evaluates to
True
in Python, but false
in JavaScript.
I think that statement is false
/ False
, no matter how you solve it, it spits out False
in my head.
Heres the breakdown:
given:
False == False != True
#Case 1:
False == False => True
True != True => False
#Case 2:
False != True => True
False == True => False
Am I missing something obvious? I tried JS with != and ===
but since the type is the same, it keeps the same ouput.
答案1
得分: 9
在Python中,你可以使用链接比较。这通常对于像 a < b < c
这样的情况很直观,但在这种情况下,它会产生相当不直观的结果。你需要将
False == False != True
解释为
(False == False) and (False != True)
然后它的结果为 True
。
英文:
In Python, you have chained comparisons
This is usually intuitive for cases like a < b < c
, but in that case, it gives that pretty unintuitive result. You need to parse
False == False != True
as
(False == False) and (False != True)
which then evaluates to True
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论