在Python中,“False == False != True”为True,但在JavaScript中为false。

huangapple go评论70阅读模式
英文:

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

在Python中,“False == False != True”为True,但在JavaScript中为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

huangapple
  • 本文由 发表于 2023年2月23日 22:38:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定