Using and in Non-Boolean Contexts
flag will be set to the truth value of length of the list is greater than
0 and element
0 has the string
"expected value". If both conditions are true, then
flag will have the value
True. If either of those conditions are false, then
flag will be
False. Exactly what you wanted.
However, you test for that in the first expression. If the list is empty, then that part of the
and expression is false, and Python knows the whole expression evaluates to
False without needing to check the second expression.
Become a Member to join the conversation.