I am confused on how does the != operator works in python. I am new to python programming.
I know simple !=
simply checks whether the LHS expression and RHS expression are not equal.
for eg:
True != False
returns True
.
My question is how it works in series of !=
operators.
e.g.: when I type
-5 != False != True != True
in my python interactive session it returns False
, but if I solve it step by step I get the answer True
.
Solving it step by step:
-5 != False
returns True
True != True
returns False
False != True
returns True
So it should return True
but it returns False
. I don't know why.
In Python, this comparation is equivalent to:
-5 != False and False != True and True != True
I.e.,
result = (-5 != False) and (False != True) and (True != True)
result = (True) and (True) and (False)
result = False
See more in:https://docs.python.org/3/reference/expressions.html#comparisons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With