Using or With Boolean Expressions
Now let’s take a look at how we can use the
or operator with Boolean expressions in Python.
To take the
or of two Boolean expressions, you say
<first expression> or <second expression>. This will evaluate to be
True if either the first expression or second expression is
True, and if they’re both
or will evaluate to be
Remember this is the inclusive OR, where if both are
True the entire expression is viewed as
That can be summarized in this truth table. If expression 1 and expression 2 are both
True, the entire expression evaluates to
True, if the first one is
True and the second one is
False, the whole expression evaluates to
True, and so on and so forth.
Let’s take a look at some examples of this. So if I provide it two expressions,
5 == 5 or 3 < 9, since both those expressions are
or will be
True. If I provide one
True and one
4 < 9 or 7 == 4, since the first expression was
True, the whole thing is
If I provide an example where the first one is
False but the second one is
4 < 2 or 9 == 9, the whole thing evaluates to be
True. And if I provide two expressions that are
5 < 1 or 7 == 8, the whole thing evaluates to
An observation to make—and this will help you understand some of the future examples we’ll take a look at—is if expression 1 is
True, the result is the value of expression 1.
If expression 1 is
False, then the result is whatever the value of expression 2 is. And we will see how that’s useful when we start taking a look at how we can use
or with expressions that aren’t Boolean expressions.
Become a Member to join the conversation.