Returning Expressions With Boolean Operators
For more information on topics covered in this lesson, check out these resources:
00:00
Let’s continue our look at returning Boolean values, this time considering Boolean expressions which use the and
or or
operators.
00:10
In Python, the result of an operation using and
or or
is going to be the value of one of the operands, which might not always be True
or False
.
00:23
Here are some examples, and we’ll look through these one at a time. 0 and 1
. 0
is falsy, 1
is truthy, so this is going to evaluate to something falsy, but it doesn’t evaluate to the word False
. It evaluates to 0
.
00:43
1
and 2
are both truthy, but and
doesn’t give a result of True
, it gives a result of 2
. 0 or 2
? True, but Python tells us 1
. 0 or 1
?
01:00
True, but Python tells us 1
. What’s happening here? In evaluating an expression with and
, Python returns the first falsy value it finds or the value of the last operand if all of the ones preceding it were truthy.
01:21
or
returns the first truthy value it finds or the value of the last operand if all of the preceding ones were falsy. So a simple function to return the value of a and b
won’t always be a value of True
or False
.
01:41
If I have this function that wants to tell me if a
and b
are both True
and I use it simply returning a Boolean expression, I ask it to tell me if 1
and 2
are both True
—it doesn’t say “Yes, True
” or “No, False
,” it tells me 2
.
02:00
That’s not useful. There are several ways that you can fix this, and we’ll take a look at three of them. First, we can explicitly use an if
statement and return
statements that return the values True
and False
.
02:16
This has a similar structure to some of the other functions we’ve seen in previous lessons. We can redefine both_true()
.
02:29
If a and b
evaluates to a truthy value, we want to return True
. And if it doesn’t, it must be a falsy value. We skip the if
block and we can return False
.
02:46
Now if I want to ask if 1
and 2
are both True
values, we see that they are. If I want to check to see if 1
and 0
are both True
values, I’m told that they’re not. That’s one way.
03:03
Another way is to use Python’s ternary operator. This is a three-part statement with an if
condition, a value to return if the condition is True
, which comes before the word if
, and a value to return if the condition is False
, which comes after the word else
.
03:25
You can see the Real Python article on Python’s ternary operator for more information on how this operator is used. In our case, we’re simply repeating the logic of our previous version of the function using the ternary operator. If a and b
evaluates to a truthy value, we return True
.
03:45
Otherwise, we return False
.
03:49 Let’s rewrite our function to use this version.
03:55
We want this function to return True
if a and b
are True
, otherwise it should return False
.
04:09
You see that 1
and 2
are still both True
,
04:14
and we see that 1
and 0
are not both True
.
04:20
One final way I’ll show you is to use Python’s built-in function bool()
. This function returns True
or False
based on the argument provided being truthy or falsy.
04:34
We provide a and b
as the argument to Python’s bool()
function. It will evaluate a and b
, and if it’s truthy, bool()
will return True
.
04:44
If it’s falsy, bool()
will return False
, and that’s exactly what we want returned.
04:54
So again, this tells us 1
and 2
, both True
? True
.
05:00
1
and 0
, both True
? False
. Remember, many times we can simply put a Boolean expression as the return value for our function that we want to return a value of True
or False
, but if the Boolean expression involves and
or or
, we’ll probably need to use one of these three variations.
05:21
Next up is how we can use a return
statement to shortcut the execution of loops.
Become a Member to join the conversation.