Navigating Boolean Operators
00:00 It wouldn’t be an exaggeration to say that Boolean are truly fundamental to programming: true and false, yes and no, zero and one. Boolean logic is applied from high-level programs all the way down to the building blocks of chip design.
00:13 So in accordance with their significance, both this lesson and the next are all about Boolean operators in Python. In this lesson, you’ll explore Boolean operators, Boolean expressions using those operators, and predicate functions, a common way to use Booleans in your code.
00:29
First, let’s look at the operators in question. The and operator is binary and used in expressions like x and y and it returns True if both x and y are true and false otherwise. The or operator, also binary, is used in expressions like x or y and returns True if either x or y are true and false otherwise.
00:54
Then there’s the not operator, the first unary operator we’ve seen for a while. It is effectively the Boolean equivalent of the negation operator.
01:03
A sample expression would be not x and would return True if x is false and false if x is true. I’m sure you can guess what’s next.
01:12 Pop open the REPL and we’ll write some code.
01:15 Okay, so to use Boolean operators, you first need some Booleans to work with. You might remember that the comparison operators all return Booleans, so that’s one potential source.
01:25
For example, if you have a variable age holding the value of 20, age = 20. You can compare it against another value and store the Boolean result in a new variable called is_ adult. is_adult = age > 18.
01:40
Because the value of age is greater than 18 is_adult now holds the Boolean True. And you can use the built-in type() function to validate that this is a Boolean now. type(is_adult) returns class bool, which is short for Boolean.
01:56
Now let’s look at some simple Boolean expressions using Boolean operators to return to a value. First and. 5 < 7 and 3 == 3.
02:07
This returns True because both sides of the operation evaluate to True. 5 is less than 7 and 3 is equal to 3.
02:14 If either operand evaluates to false, it will also return false.
02:23
Yep. False because while 5 is still less than `7, 3 is not not equal to 3. The or operator on the other hand will return True if either operand is true and false only if both operands are false.
02:38
So back to the previous example, using or instead of and: 5 < 7 or 3 == 3 returns True.
02:47
Both operands are true, which satisfies or. And if you change one of them as in 5 < 7 or 3 != 3, you still get True even though 3 is of course equal to 3.
03:01
But if both operands evaluate to false such as 5 > 7 or 3 != 3, you finally get false because both sides of the operation evaluate to false.
03:14
And then the unary operator not can be used to negate or flip any Boolean. So starting with 5 < 7, which evaluates to True, you can apply not as in not (5 < 7), which returns false.
03:31 But using comparison operators isn’t the only way you interact with Boolean values in your code. Another way to create Booleans is with predicate functions.
03:39
These are functions that compare arguments against some condition and return True or False. Here’s an example: number = 42.
03:48
validation_conditions = ( a tuple containing a call to isinstance() passing in number and the int class
04:00
The isinstance() function takes a value and a class and evaluates if the value is an object of the provided class. And as you should already know, number % 2 will return zero, which after being compared to the integer zero should return True.
04:13
So if you look at validation_conditions,
04:16
you’ll see it holds (True, True), which is one way to check that both conditions are true. A quicker way though would be to use the all() predicate function,
04:28
which also returns True. all() takes an iterable and if every item contained within is truthy, it returns True. And notice how I said truthy.
04:38
You can actually use Boolean operators on regular non-Boolean objects and in this case, they can be considered either truthy or falsy. The general rule is numeric types equal to zero are falsy, while any other numeric value is truthy, negative numbers included. To see whether an object is truthy or falsy, pass it into the bool() constructor.
04:58
Passing in the integer 0 returns, false
05:02
passing in the float 0.0 also returns false,
05:06
but passing in the integer -3 returns True, and even passing in the float 0.01 returns True.
05:15 So that’s numbers. But what about non-numerics? Well, by default, most objects in Python are considered truthy. The exception to this would be empty container types.
05:25
For example, an empty string passed to bool() returns,
05:29
false, meaning it’s falsy, but add even an empty space to the string bool(" ").
05:37
And True, it’s now considered truthy. The same applies to other container types like lists. bool([]) returns false. bool([1, 2]) returns true.
05:52
This pattern continues with tuples, sets, dictionaries, etc. Feel free to go ahead and experiment on your own if you don’t believe me. So knowing that all objects have an inherent truthiness or falsy-ness, what happens when you use regular objects in Boolean expressions, like 3 and 4? That returns 4.
06:12 Well, that’s weird, isn’t it? Next lesson, we’ll get into why this happens and how to work with it.
Become a Member to join the conversation.
