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?
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?
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.
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)