Mixing Boolean Expressions and Objects
00:00
Two lessons ago, we saw how the word or
worked on connecting two Boolean expressions. In your last lesson, we saw how it worked with objects.
00:11
And in both cases, the behavior was about the same. If the first object or the first expression evaluated to True
, you got the value of the first expression or the first object.
00:22
If the first expression evaluated to be False
, you got the second expression or object, or its value. If you mix the two operations, the results will be what you would expect.
00:36
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.
00:54
If the first operand evaluates to be False
, then you get the second operand, whatever it is. So, it’s basically the same rule. If x
is True
, then you get x
.
01:07
If x
is 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 7
, that’s True
and that’s a Boolean expression, we’re going to get the Boolean value True
back.
01:34 If I switch these around,
01:39
since "hi"
is True
—it’s not an empty string (""
)—we’re going to get 'hi'
back.
01:47
If I provide it with a False
Boolean expression, 4 > 18 or 7
, the first expression is False
, so the result of the or
operation is the second expression, which is 7
.
02:07 And if I switch those around,
02:13
7
evaluates to True
, and so we get the first operand back, which is 7
. If I give it two False
operands, 5 > 18
, or
, say, an empty string (""
), since 5 > 18
is False
, we’re going to get the second operand back.
02:37
And if I switch those around, "" or 5 > 18
, now the first operand is False
, so we’re going to get the result of the second operand, which is also False
.
02:51
So again, when you use or
between any two expressions, the behavior is always the same. If the first expression evaluates to be True
, you get that first expression, its value or its object.
03:05
If the first expression evaluates to be False
, you get whatever the second expression’s result or object is. That behavior is consistent throughout the use of the Python or
operator.
03:19
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.