This question already has answers here:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 3 years ago.
Can someone explain to me why the Python interpreter evaluates this expression to be False?
1 in [1] == True
I would expect that 1 in [1] would evaluate to True, and obviously True == True would be True. However this isn't what happens - the expression is False. Why does this happen?
== and in are both comparison operators. And when you have multiple comparison operators like this, Python considers it a chained comparison. For example, 1 < x < 10 is equivalent to 1 < x and x < 10.
In your case, 1 in [1] == True is equivalent to (1 in [1]) and ([1] == True), which evaluates to False.
If you have an expression like this python split it to more statements. In fact:
1 in [1] == True equals to: (1 in [1]) and ([1] == True)
Right side is false since [1] != True and whole sentence is false
Related
This question already has answers here:
Why does 1 == True but 2 != True in Python? [duplicate]
(8 answers)
Closed 1 year ago.
It is in the REPL
>>> 1 == True
True
>>> 2 == True
False
>>> 3 == True
False
>>> -1 == False
False
but
if 3:
print('yes') #prints yes
if not -1:
print('yes') #prints nothing
Why are positive integers not evaluated as True when using ==, but they are evaluated as True in if-statements? 2) Why are negative integers not evaluated as False when using ==, but they are also not evaluated as False in if-statements?
What is the underlying rule, where is it written down?
Every integer except 0 evaluates to True. The reason why your REPL was giving you different results than your if statements is because you aren't evaluating the same expression.
In your REPL you are asking is A equal to B. It's only true for False == 0 or True == 1. In your code you aren't doing a comparison. You are just checking that the variable is not something that could be considered "empty/nonexistent" (so to speak).
in short:
if a == True: and if a: are not the same thing.
If it helps, you can look at it like this:
if changeForADollar == myChange:
vs
if anyChangeAtAll:
This question already has an answer here:
How do chained comparisons in Python actually work?
(1 answer)
Closed 2 years ago.
Running this in the REPL gives me
>>> 1 == 2 == 2
False
Which surprised me, in C this would evaluate to 1. I expected the right-hand side to evaluate to True and 1 == True is True in Python. For example, this evaluates as I would expect:
>>> 1 == (2 == 2)
True
How is Python parsing and evaluating the first expression? This, evaluates the same way but this is what I would expect because == is right-associative and would evaluate to 0 in C
>>> 2 == 2 == 1
False
This is due to the operators chaining phenomenon
An example :
>>> 1==2
=> False
>>> 2!=3
=> True
>>> (1==2) and (2!=3)
# False and True
=> False
This question already has answers here:
python operator precedence of in and comparison [duplicate]
(3 answers)
Closed 3 years ago.
I started learning python but I've noticed something unsual, something that I do not understand, why the expression provided below it's evaluated to false even it is true??
l = [1,2,3,4,5,"Hi"]
"Hi" in l # returns True
"Hi" in l == True # returns False
"Hi" in l == True is evaluated as ("Hi" in l) and (l == True) which is False.
Explanation from documentation:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z
This question already has an answer here:
Why does (1 in [1,0] == True) evaluate to False?
(1 answer)
Closed 5 years ago.
I came across this expression, which I thought should evaluate to True but it doesn't.
>> s = 1 in range(2)
>> s == True
>> True
Above statement works as expected but when this:
1 in range(2) == True
is executed, it evaluates to False.
I tried searching for answers but couldn't get a concrete one. Can anyone help me understand this behavior?
1 in range(2) == True is an operator chain, just like when you do 0 < 10 < 20
For it to be true you would need
1 in range(2)
and
range(2) == True
to be both true. The latter is false, hence the result. Adding parenthesis doesn't make an operator chaining anymore (some operators are in the parentheses), which explains (1 in range(2)) == True works.
Try:
>>> 1 in range(2) == range(2)
True
Once again, a good lesson learned about not equalling things with == True or != False which are redundant at best, and toxic at worst.
Try to write
(1 in range(2)) == True
It has to do with parsing and how the expression is evaluated.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 6 years ago.
Why does the following evaluates to False in Python?
6==(5 or 6)
False
'b'==('a' or 'b')
False
The first expression evaluates (5 or 6) first, which evaluates to 5 because 5 is truthy. 5 is NOT equal to 6 so it returns False.
The second expression evaluates ('a' or 'b') first, which evaluates to 'a' for the same reason as above. 'a' is NOT equal to 'b' so it returns False.
A good example to explain this would be to try to put a falsey value as the first part of the or expression like 6 == ([ ] or 6) or 6 == (None or 6). Both of these will return true because the or statement will evaluate to the truthy value (in each case it is 6).
There are a couple of ways to create the statement I think you want. The first would be to use in like 6 in (6,5). The second way would be to expand your boolean expression to read like this (6 == 5) or (6 == 6). Both of these will return True.
The condition within parentheses is evaluated first. So
6==(5 or 6)
evaluates to
6==(5) # since 5 != 0 (True), the second operand is not evaluated
which is False. Same goes for the second one. ('a' != None)
Try:
>>>6 == (6 or 5)
True
What's going on? Try:
>>5 or 6
5
I'm not totally sure why "5 or 6" evaluates to 5, but I would just try writing that expression differently.
But there is a caveat:
>> 5 == (0 or 5)
True
Because 0 is not truth-worthy, hence the bracket result in 5.