Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does this return False?
#1
The following returns False.

1 == 2 < 3
Could someone explain why?

My understanding is

1 == ( 2 < 3 )
so
1 == True
which is the same as
1 == 1
So True?
I think it is because it is chained.

For example
1 > 3 < 4
is false.

So back to the original expression
(1 == 2) and (2 < 3)
which is
false and true
which is
false
Reply
#2
You are correct. From Python documentation Documetation >>> The Python Language Reference >>> 6.10. Comparisons

Quote:Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

So there are two comparisons in your example: 1 == 2 and 2 < 3. As first comparison is false second is not evaluated.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to return False in ternary condition KEYS 7 2,954 Dec-10-2020, 05:32 PM
Last Post: perfringo
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,166 Apr-20-2020, 05:39 PM
Last Post: fbaldit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020