+ 2

A strange result. Help me to understand.

Why in the first expression the result is False? print(1>2==False) #False print((1>2)==False) #True print(1>(2==False)) #True

14th Aug 2017, 6:50 AM
Goga
4 odpowiedzi
+ 5
In the first line the ==False is ignored, just try ==True and find out that both returns false since 1 is not greater than 2
14th Aug 2017, 7:16 AM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 2
For the first one only the first comparison is checked and the ==False is ignored. See the code below to help you understand. Note that the print statement in the function isn't displayed because it is not called. def notRan(): print('ran') return False print(1>2==notRan()) The second fixes the problem with the first by wrapping the first part in parantheses so that it will be evaluated to False and then you end up with the statement False==False which will evaluate to True. The third evaluates the 2==False first. A number other than 0 in python is truthy while the number 0 is falsey. So this results in True==False which evaluates to False. False will act as a 0 in numeric comparison so this leaves us with 1>0 which will result in True.
14th Aug 2017, 7:12 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Hi, i don't know python but i can help you because this is just a logic problem: 1) (1>2==False) -> EDIT: i was wrong! like @ChaoticDawg said, the second condition == False is ignored, so 1>2 ? #False will be the only computed 2) ((1>2)==False) -> with order it makes 1>2 ? False, False == False ? #True 3) (1>(2==False)) -> with order it makes 2==False ? (false could be seen like 0(zero)) False, 1>False ? (for the same reason) #True (1>0). I hope this can help you ;)
14th Aug 2017, 7:15 AM
Michele Virgilio
Michele Virgilio - avatar
+ 1
@Nomeh Uchenna Gabriel The first example ignores == False. Is this a rule or follows from this example?
14th Aug 2017, 8:50 AM
Goga