Differences Between any() and or
00:00
In this lesson, you’ll be looking at a very important difference between any()
and or
. The or
operator returns the first truthy value. That is, if you’re comparing two values that aren’t explicitly True
or False
, for example—say an empty string and a string that had a value—it would return the actual string value.
00:21
any()
, on the other hand, returns True
or False
always.
00:27
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 True
or False
value.
00:47
any()
has been billed so far as a possible replacement for or
, but or
still has a special behavior that any()
doesn’t share.
00:57
If instead, you were going to compare the empty string (""
) with "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.
01:18
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.