Differences Between any() and or
In this lesson, you’ll be looking at a very important difference between
or operator returns the first truthy value. That is, if you’re comparing two values that aren’t explicitly
False, for example—say an empty string and a string that had a value—it would return the actual string value.
any(), on the other hand, returns
In the previous lesson, you were looking at using
any() with a list that contained an empty string (
"") and a string that had some content, in this case
"Hello". Now, although
any() converts these values,
any() will always return a
any() has been billed so far as a possible replacement for
or still has a special behavior that
any() doesn’t share.
If instead, you were going to compare the empty string (
"Hello" or the empty string, you know, that the empty strings evaluate to
False, so they won’t satisfy the condition here in this case.
"Hello", however, will evaluate to
True, but instead of returning
True, it actually returns the value itself.
Maybe this is a behavior that’s desirable in your program, maybe not. It’s just something to bear in mind about the difference between what
or returns and what
any() returns. In the next lesson, you’re going to cover short circuiting, which is a behavior of these functions when they’re going through various conditions.
Become a Member to join the conversation.