Arun Kumar
Arun Kumar

Reputation: 9

Python compare >, <

Can some one explain the following

print("5<7<3 ",5<7<3) 
print("(5<7)<3 ",(5<7)<3) 
print("5<(7<3) ",5<(7<3)) 
print("5>(7<3) ", 5>(7<3))
print("(5<7)>3 ",(5<7)>3)
print("(5<7)<1 ",(5<7)<1)

Output is:

Line 1 - 5<7<3  False 
Line 2 - (5<7)<3  True
Line 3 - 5<(7<3)  False
Line 4 - 5>(7<3)  True
Line 5 - (5<7)>3  False
Line 6 - (5<7)<1  False

I am confused how come output in line 3, and 4 are giving True and False. As I understand x

Upvotes: 0

Views: 91

Answers (3)

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6935

Line 3 - 5<(7<3) False

(7<3) is False which is 0 in context of Python, now 5<0 is False.

Line 4 - 5>(7<3) True

Similarly, 7<3 is False which is also 0, so 5>0 is True.

For other lines, if a condition leads to True, it would be rendered as 1 by Python for subsequent condition checks.

Upvotes: 2

shreesh katti
shreesh katti

Reputation: 837

Simply evaluate the answer of the parenthesis:

And remember False = 0 and True = 1

So, line 3 and 4:

print("5<(7<3) ",5<(7<3)) 
print("5>(7<3) ", 5>(7<3))

would be

1) 5<(False) i,e 5 < 0 which is False.

2) 5 > False i,e 5 > 0 which is True

Upvotes: 0

Christian Sloper
Christian Sloper

Reputation: 7510

Line 3 - (7 < 3) resolves to 0 which is not more than 5 => False.
Line 4 - (7 < 3) resolves to 0 which is less than 5 => True.

Upvotes: 0

Related Questions