The or Operator
00:00
The or
operator is the other binary Boolean operator in Python. In Python, to apply the or
operator on the inputs x
and y
, you type x or y
.
00:13
or
returns a value of False
only when both of the operands evaluate to False
. In other words, if one of the inputs is True
, then or
is going to return True
. Here is the truth table to the or
operator. Notice that the last row in the truth table is the only time when or
returns a value of False
and it happens when both of the inputs have a value of False
.
00:39
Just like the and
operator, there’s another way to think about the or
operator with regards to the order in which the inputs come in.
00:48
Let’s divide those into two main cases. The first main case corresponds to when the first input to the or
operator is True
. In these two sub-cases, the two first rows of the truth table—when the first input is True
or returns True
.
01:07
So these two cases correspond to or
’s short-circuiting feature. When the first input is True
, or
immediately returns True
and the second input is not evaluated.
01:19
The next main case corresponds to when the first input to the or
operator is False
. In this case, the second input, or the value of the second input, completely determines what the value is to the or
operator.
01:36
Just like the and
operator, or
will return the value of the operand consistent with or
’s truth table.
01:44
Let’s see this in more detail. When Python sees the expression x or y
, Python will first evaluate x
, and if the Boolean value of x
is True
or if x
has a truthy value, then or
will return the value of x
.
02:03
So this case corresponds to or
’s short-circuiting feature. If the first input is x
, then x
or its value is returned and the second input plays no role. Otherwise, if the first input x
is False
, then the second input y
is evaluated and the resulting value of y
is returned. So just like and
, the or
operator is going to return a value—not necessarily a Boolean data type.
02:34 Let’s take a look at some examples.
02:38
Let’s use the product()
function in the itertools
to generate the truth table of the or
operator. So go ahead and import from the itertools
module, import the product()
function.
02:52
And then go ahead and write out a for
loop, so for x, y in product()
of the list that contains the Boolean values True
and False
.
03:04
And you want to create the Cartesian product of that list with itself, so go ahead and write repeat=2
for the keyword argument.
03:13
Then, like before, go ahead and print the value of the or
operator on each of the operands x
and y
. So f"{x} or {y} ="
, and then here you’ll actually want to evaluate the or
operator on x
and y
.
03:32
And let’s add a little bit of space in between the x
so that things get indented a little bit. Go ahead and run that. And so as we saw in the truth table, the only time when or
returns a False
Boolean output is when both of the inputs are False
, and then in all other cases it’s going to return True
.
03:55
All right, now let’s see some examples of what the or
operator will return depending on what the inputs are. Let’s define that print_and_return()
function like we did before with the and
operator. And if you remember, this will just simply return… Well, first it will print that it is returning, so f"I am returning {x}"
, and then it will actually return the value of x
. And let’s add again that tab (\t
) in there, just so that when things get printed it’s not so much to the left. Go ahead and run that.
04:33
And let’s first just try this with, say, True or print_and_return()
, the value of False
. And so in this case, the first operand is True
, or evaluates to True
, and so the or
operator will short-circuit.
04:51
So, if you tried something like True or print_and_return()
, and here you’re going to have a 1/0
input to the print_and_return()
function—well, that never actually gets executed, right?
05:05
The first operand is True
, we know then that the or
will short-circuit, and we won’t generate a black hole with that division by zero. On the other hand, if the first input to or
is False
, then we know that the second expression is going to be evaluated. In this case, we’re going to be evaluating the print_and_return()
function. And in this case, we get a I am returning 5
and then the actual return value.
05:36
So, in this case, if you did divide by 0
, you would be generating a black hole.
05:44
Now let’s see how we can use the or
operator to set a default value. I don’t know if you remember, but we used the not
operator to set a default value for the name of a user if the user didn’t pass a name. So, for example, you read in the value that’s passed in for the first_name
and it’s an empty string, and you know that if you type not first_name
, then this returns a value of True
. And again, you interpret this as saying it must mean that as a Boolean, an empty string must have a False
value, because not False
is True
.
06:22
So, how can we use this, say, to set a default value for the first_name
if no first name is given? So go ahead and set first_name
. Let’s make sure it’s an empty string.
06:35
And then we want to set first_name
to "Not given"
if first_name
was an empty string or we just want to leave it just like its current value, right?
06:46
If the user did pass the name "Luigi"
, we want to make sure that the first_name
remains "Luigi"
. So go ahead and type first_name or "Not given"
.
06:57
All right, let’s analyze what’s going to happen. You know that the empty string, as a Boolean, evaluates to False
. And so False
in the first operand in an or
operator expression, means that the second input is going to be evaluated, and in this case, it’s going to be "Not given"
.
07:16
So when you run that, first_name
now has a value of 'Not given'
.
07:23
On the other hand, if the user did pass in a first name, say again the name was "Luigi"
, and then in this case if you run the exact same line, or the exact same code that we had where we’re checking first_name or "Not given"
, and go ahead and run that, then we know that a non-empty string has a truthy value.
07:48
So in other words, or
will short-circuit to the value of first_name
, and in this case, then, the first_name
remains with the value of 'Luigi'
.
08:01
All right! Well, this ends the lesson on the or
operator. In the next lesson, we’ll take a look at the different comparison operators in Python.
Become a Member to join the conversation.