+ 1
What should be the value of "0 == 0 == 0" ?
What about 1 == 0 == 0, 0 == 1 == 0 and 0 == 0 == 1 ? Then what about 2 == 2 == 2 ?
15 Respuestas
+ 9
1 and 5 will be True and others will be False in Python because comparisons can be chained in Python
https://docs.python.org/3/reference/expressions.html#comparisons
+ 6
1) "0 == 0 == 0"
This will evaluate to "false" as "0 == 0" will be "true" and then true as a number will be converted to "1" so it will be "1 == 0" which is false.
2) "1 == 0 == 0"
This will evaluate to "true" since "1 == 0" will be "false" and then "false" as a number will be converted to "0" so it will be "0 == 0" which is true.
3) "0 == 1 == 0"
This will evaluate to "true" since "0 == 1" will be "false" and then "false" as a number will be converted to "0" so it will be "0 == 0" which is true.
4) "0 == 0 == 1"
This will evaluate to "true" since "0 == 0" will be "true" and then "true" as a number will be converted to "1" so it will be "1 == 1" which is true.
5) "2 == 2 == 2"
This will evaluate to "false" since "2 == 2" will be "true" and then "true" as a number will be converted to "1" so it will be "1 == 2" which is false.
Edit:
As Diego pointed out this answer is specific to JavaScript and results may vary for other languages (e.g. Python)
+ 6
I prefer it if code explans itself.
In Python it seems like a == b == c is not evaluated ltr or rtl but the same as all(a,b,c)
Edit: as Diego pointed out that is not true for (0,0,0).
Edit2: it seems to be interpreted the same as all(i==p for p in (i,j,k))
https://code.sololearn.com/c3Ipf3qh78vG/?ref=app
https://code.sololearn.com/ceaMrz6WjA34/?ref=app
+ 5
Diego
Ok so, I didn't realize that using different languages would leave different results for this question. I typically use JavaScript as my primary programming language and since it was tagged I immediately went with that and with it my answer would be correct.
https://code.sololearn.com/WV3ailR0yn9r/?ref=app
But I see what you mean, thanks for pointing that out
+ 4
Tibor Santa
I have a solution that caters for all chained cases. See
https://code.sololearn.com/ceaMrz6WjA34/?ref=app
+ 2
good question i saw this in one of the quizes and was silently wondering how they got their answer, so thanks for your answers
+ 2
== is an operator, program reads operators first by following the operator precedence and then following the order of the operators.
Comparison operators always return a boolean, and booleans can be compared with other numeric datatypes. (Boolean can be thought as a numeric datatype with 2 possible values.)
+ 2
[Python]
LynTon
From Mert's answer:
"Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once".
Therefore, 1 == 0 == 0 is equal to (1 == 0 and 0 == 0), which is equal to (False and True), which evaluates to False.
Same goes for all of the other cases.
+ 2
LynTon Interesting to see how diferrente languages handle the same problem/logic.
Kristian Benoit This just goes to show you how important it is to specify the desired programming language in the question/tags as it can lead to some confusion.
+ 2
Louis
0 == 0 == 0 returns True.
all(0,0,0) returns False.
+ 2
Louis awesome. :)
+ 1
Diego aren't comparison operators quite same in most programming languages?