2

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.

SLNM
  • 69
  • 1
  • 10
  • JS: `false == false` -> `true` -> `true != true` -> `false` – Konrad Feb 23 '23 at 14:40
  • 1
    `False` and `True` are going to be `undefined` in JavaScript, the constants are `false` and `true` – Pointy Feb 23 '23 at 14:41
  • 1
    What is the *associativity* and *precedence* of these operators in the languages? Are you sure they are the same? In short, you need to know if `False == False != True` is the same as `(False == False) != True` or `False == (False != True)`. – Some programmer dude Feb 23 '23 at 14:41
  • 4
    @Someprogrammerdude in python it's actually `False == False and False != True` because python chains boolean operations – Konrad Feb 23 '23 at 14:42
  • 2
    Lesson for today: Different languages have different rules, and does things in different ways. The comparison behaves one way in Python (see @Konrad) and a different way in JS. You can't really do a direct comparison between the languages here. – Some programmer dude Feb 23 '23 at 14:46

1 Answers1

9

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

Florent Monin
  • 1,278
  • 1
  • 3
  • 16
  • What is `False == False != True` actually doing? `(False==False) != True` or `False == (False!=True)` – Tom McLean Feb 23 '23 at 14:44
  • Is there actually a use-case why Python uses chained comparisons? – SLNM Feb 23 '23 at 14:46
  • 1
    What do you mean? It is parsed by Python as `(False == False) and (False != True)` if that's your question @TomMcLean? – Florent Monin Feb 23 '23 at 14:46
  • @SLNM so you can say something like `if x < y < z` instead of `if ((x < y) && (y < z))` – Jared Smith Feb 23 '23 at 14:47
  • @SLNM Yes, usually it's to make sure that a variable is between 2 numbers, for instance: `if 10 < x < 20:` is better than `if (10 – Florent Monin Feb 23 '23 at 14:47
  • 1
    @SLNM Remember math where you can have something like `0 < a < 10`? Few languages implement this semantics, but Python does by this kind of chaining and it's well-defined in Python. – Some programmer dude Feb 23 '23 at 14:47
  • Oh okay, now the world makes sense again. Thank you very much @Someprogrammerdude and of course also Florent_Monin – SLNM Feb 23 '23 at 14:49
  • @FlorentMonin Ahh I get what its doing now with the comment by jared smith. Good answer – Tom McLean Feb 23 '23 at 14:50
  • @TomMcLean In the code like [this](https://onecompiler.com/python/3yyse4c36) you can see that comparison is made between x and y and then between y and z – Konrad Feb 23 '23 at 14:55