Mixing Boolean Expressions and Objects
If the left operand evaluates to be
True, whether it’s a Boolean expression or an object or other expression, you get that value back. If it’s a Boolean, you get
True. If it’s not a Boolean, you get that object.
False, you get
y. And that behavior is consistent through all three of the cases that we have looked at. So if I have a Boolean expression,
3 < 7,
or, an object—say, a string—since
3 is less than
True and that’s a Boolean expression, we’re going to get the Boolean value
7 evaluates to
True, and so we get the first operand back, which is
7. If I give it two
5 > 18,
or, say, an empty string (
5 > 18 is
False, we’re going to get the second operand back.
In your next lesson, we’re going to take a little closer look at what Python is doing behind the scenes when it looks at your
or operation, specifically how it evaluates and when it evaluates each part of your
or expression to determine the result.
Become a Member to join the conversation.