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
  • 19,194
  • 5
  • 54
  • 65

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,724
  • 2
  • 14
  • 22
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
  • 31,313
  • 3
  • 12
  • 25