0
a=8
b=3
if a>b!=True:
    print("ex1")
else:
    print("ex2")

Output: ex1

Output expected: ex2

Why is the else condition not executed whether a>b gives True value?

mkrieger1
  • 14,486
  • 4
  • 43
  • 54

2 Answers2

1

Adding to what @khelwood has mentioned in comments.

You need to know the Operator Precedence before using operators together.

Please go through this: Operator Precedence

a=8
b=3
if (a>b) != True:
    print("ex1")
else:
    print("ex2")

Now the above code will give you ex2 as output because () has a higher precedence.

Ram
  • 4,640
  • 2
  • 12
  • 19
0

You observed effect of chaining feature of comparisons which

a>b!=True

make meaning a>b and b!=True, to avoid summoning said feature use brackets, i.e.

(a>b)!=True

As side note that above condition might be expressed as

not (a>b)
Daweo
  • 21,690
  • 3
  • 9
  • 19