Returning Boolean Values
00:00
In this lesson, we’ll look at best practices when writing functions to return True
or False
. A Boolean-valued function, also known as a predicate function, is a function that returns a value of either True
or False
. It’s good programming to name this function beginning with the word is
. It’s like you’re asking a question, “Is something True
?” and the function then returns True
, “Yes it is,” or False
, “No, it isn’t.” Python has a set of rules to determine the truth value of any Python object.
00:33
Objects considered False
, which we call falsy, are constants like None
and False
, numeric types with a value of 0
, collection types that are empty, and objects with a .__bool__()
method, which evaluates to False
, or a .__len__()
method, which evaluates to 0
.
00:58 Any other Python object is considered truthy.
01:04
Here’s an example to determine if one number is divisible by another. It will return True
to tell us it is and False
to tell us it isn’t.
01:15
This function has two parameters, a
and b
, and we want to know if the argument provided for a
is divisible by the argument provided for b
. If a
is divisible by b
, then a
divided by b
will have a remainder of 0
, and that’s what the modulus operator (%
) gives us.
01:33
So, we’ll check if the operation a % b
is 0
. If it is, we return True
. If it’s not, we return False
.
01:42
Now, we could have written the condition with a comparison to 0
, like I have in this commented version, but let’s be more Pythonic about it.
01:51
If a % b
is 0
, then it’s considered a falsy value. Putting a not
in front of the falsy value makes the expression evaluate to True
, so the condition not a % b
will be True
when a % b
is 0
. If that’s the case, we want to return True
.
02:17
Since there’s no other condition to check for, we can have a return False
outside the if
block for the case when our condition is False
. Again, if a % b
isn’t 0
, then it has a truthy value and taking the not
of that truthy value evaluates to False
.
02:39
The if
condition will fail. We skip over the if
block and execute what follows it, which is the return False
statement. Let’s see this work.
02:59
We’ll see if 4
is divisible by 2
, and it is. We’ll ask if 7
is divisible by 4
, and it’s not.
03:13
Predicate functions often use the following: comparison operators, the membership operator, the identity operator, and the Boolean operator not
.
03:26
In these cases, you can usually write the Boolean expression in the return
statement. Here, we’re just returning the value of not a % b
.
03:37
It will return True
or False
for us based on how not a % b
is evaluated.
03:46
Let’s modify our version to look like that. We delete those two return
statements. We are now going to return the value of this expression directly.
04:00
And since this is no longer an if
statement, we’ll be sure to remove the colon that was there before. Let’s save this and try this out.
04:11 We’ll restart our interpreter,
04:18
import our new version, check that 4
still is divisible by 2
,
04:27
and that 7
still is not divisible by 4
. These evaluations work as before. A word of caution: you might not be able to return the expression directly if your condition involves or
or and
, and we’ll discuss that in the next lesson.
Become a Member to join the conversation.
torrepreciado on Dec. 30, 2021
return not a % b