WEBVTT 00:00:01.720 --> 00:00:07.420 Hi. This lecture is all about true and false values, also known as Boolean 00:00:07.420 --> 00:00:13.903 values. In this lecture, we'll explore the Python's type bool, and the operators that 00:00:13.903 --> 00:00:20.308 we can apply to Boolean values. Earlier, we used Python's arithmetic operators like 00:00:20.308 --> 00:00:26.165 multiplication and subtraction. And now, we're going to use some of Python's 00:00:26.165 --> 00:00:32.657 comparison operators. For example, let's compare values three and four using the 00:00:32.657 --> 00:00:39.670 less than operator. When this expression is evaluated, we're going to get a true 00:00:39.670 --> 00:00:46.473 value or false value back. The type of value that we get is type bool. Let's 00:00:46.473 --> 00:00:53.278 compare three with eight, asking if three is greater than eight, and it's not. So, 00:00:53.278 --> 00:01:00.061 the value that this expression evaluates to is false. When we evaluate eight 00:01:00.061 --> 00:01:07.557 greater than three, we get true. And if we were to evaluate 3.5 greater than or equal 00:01:07.557 --> 00:01:14.705 to 3.4, it's also true. Lets compare two ends, seven with seven. Notice that the 00:01:14.705 --> 00:01:22.634 operation that we're performing now is the equality operation. And, we need to use 00:01:22.634 --> 00:01:29.328 two equal signs, not one, to signify equality. That's because the single equal 00:01:29.328 --> 00:01:36.199 sign is already used for the assignment operation. Seven is equal to seven. How 00:01:36.199 --> 00:01:43.157 about seven and 7.0? I type into operand with a type float operand. This is also 00:01:43.157 --> 00:01:49.826 true. Let's assign a couple of variable values, x gets seven, y gets eight. And 00:01:49.826 --> 00:01:55.954 now, we can apply the same equality operator to two variable operands. First, 00:01:55.954 --> 00:02:02.408 we work up the value that x refers to, which is seven. And y refers to eight. And 00:02:02.408 --> 00:02:08.045 then, seven is compared with eight. Another operator is the inequality 00:02:08.045 --> 00:02:14.156 operator. We can check whether three is not equal to four, and that's true. The 00:02:14.156 --> 00:02:19.754 comparison operators take two values and return a Boolean value, either true or 00:02:19.754 --> 00:02:24.943 false. Python also has three logical operators, which are operators that are 00:02:24.943 --> 00:02:29.787 applied to Boolean values and yield Boolean results. The first logical 00:02:29.787 --> 00:02:34.907 operator that we'll use is the not operator. And we'll begin by creating a 00:02:34.907 --> 00:02:40.234 variable grade and assigning it the value 80. So, grade refers to 80. Now, let's 00:02:40.234 --> 00:02:45.908 write up an expression that checks to see whether the grade i s a passing grade. Is 00:02:45.908 --> 00:02:50.960 grade greater than or equal to 50? And that's true. In Canada, 50 is a pass. 00:02:50.960 --> 00:02:56.423 We'll apply the not operator to that expression now. So, we're going to check 00:02:56.423 --> 00:03:01.492 to see whether grade is not greater than or equal to 50. The order that this 00:03:01.492 --> 00:03:07.630 expression is evaluated works from inside out. So, the grade greater than or equal 00:03:07.630 --> 00:03:13.844 to 50 part of the expression is evaluated first and that gives the value true and 00:03:13.844 --> 00:03:19.901 then the not operator is applied to true. Something that is not true is false. And 00:03:19.901 --> 00:03:25.918 that's the result that we get back. We can apply this not operator two times in a 00:03:25.918 --> 00:03:32.141 row, saying that this is asking whether this is not, not true, which is equivalent 00:03:32.141 --> 00:03:38.397 to just saying, is grade greater than or equal to 50? So, rather than including two 00:03:38.397 --> 00:03:44.334 nots in a row, eliminate double negation, instead write the simpler version of that, 00:03:44.334 --> 00:03:49.975 which is to just say, it's great, greater than or equal to 50. Next, let's use the 00:03:49.975 --> 00:03:55.864 and operator. First, we'll make another variable named grade two. That refers to 00:03:55.864 --> 00:04:02.682 the value 70. And now, we'll write an expression involving both variables grade 00:04:02.682 --> 00:04:09.237 and grade two. This expression, we'll check to see whether both of these are 00:04:09.237 --> 00:04:15.967 passing grades. So, is grade greater than or equal to 50, and is grade two also 00:04:15.967 --> 00:04:22.610 greater than or equal to 50? And evaluates True, if both operands are true. So, 00:04:22.610 --> 00:04:29.533 first, this expression is evaluated and it is true, so then, this expression is 00:04:29.533 --> 00:04:38.154 evaluated and it is also true, making this entire result a true result. Let's change 00:04:38.154 --> 00:04:47.193 the value of the variable grade for moment and set it to 40. We'll rerun this Boolean 00:04:47.193 --> 00:04:55.295 expression involving the and, and check to see what we get. Because this first 00:04:55.295 --> 00:05:03.996 operand is false, the Boolean expression is false. And we don't even go on to check 00:05:03.996 --> 00:05:12.086 the second operand's value. Now, let's set grade back to 80, and this time, change 00:05:12.086 --> 00:05:20.279 grade two to be a failing grade. When this is expression is evaluated, first, this 00:05:20.279 --> 00:05:28.436 part of the expression is evaluated. And that's true so we move on to evaluating 00:05:28.436 --> 00:05:35.593 this part of the expression which is false. And so, the expression ev aluates 00:05:35.597 --> 00:05:42.866 to false. To summarize, and again, only evaluates to true if both of its operands 00:05:42.866 --> 00:05:50.497 are true. Otherwise. it evaluates to false. Finally, lets use the logical 00:05:50.497 --> 00:05:58.555 operator or which also applies to two operands. We'll start by assign grade and 00:05:58.555 --> 00:06:07.326 grade two to passing grades. And now, we'll write the same expression as before 00:06:07.326 --> 00:06:15.588 replacing the and with an or. This expression will evaluate to true if at 00:06:15.588 --> 00:06:23.549 least one of the operands is true. So in this case, we get true. Now, lets assign 00:06:23.549 --> 00:06:30.261 to grade a failing grade and reevaluate the expression. Python will first evaluate 00:06:30.261 --> 00:06:36.728 the first part of this expression and determine that it is false. So, it will go 00:06:36.728 --> 00:06:43.113 on to evaluate the second part of the expression, which is true, and because, at 00:06:43.113 --> 00:06:49.662 least one operand is true, the expression evaluates to true. If we set grade to a 00:06:49.662 --> 00:06:55.878 passing grade, and grade two to a failing grade. Then, when the expression is 00:06:55.878 --> 00:07:01.841 evaluated, it works as follows. Because grade is a passing grade, the, the 00:07:01.841 --> 00:07:08.406 expression is evaluated to true at this point, without even having to go on to 00:07:08.406 --> 00:07:14.606 look as the second operand. So, to summarize, a bool, the Boolean operator or 00:07:14.606 --> 00:07:21.450 evaluates to true if at least one of its operands is true. And it evaluates to 00:07:21.450 --> 00:07:27.816 false otherwise. Now, let's combine the operators into single expressions. I've 00:07:27.816 --> 00:07:33.537 assigned grade and grade two passing grades of 80 and 90, and I'd like to 00:07:33.537 --> 00:07:39.641 evaluate this expression. I'm going to apply not to grade greater than or equal 00:07:39.641 --> 00:07:46.054 to 50, or grade two greater than or equal to 50. And there are a couple of different 00:07:46.054 --> 00:07:51.077 ways that we can interpret this expression, depending on order of 00:07:51.077 --> 00:07:57.209 precedence. The first would be to have the or operator applied first, and I'll use 00:07:57.209 --> 00:08:05.080 parentheses to signal that, followed by the not operator. The second would be to 00:08:05.080 --> 00:08:15.643 actually have the not operator apply first to the first part of the expression. And 00:08:15.643 --> 00:08:25.510 then, the or operator apply second. So, not first, followed by or, or, or first 00:08:25.510 --> 00:08:34.631 followed by not. Let's evaluate the expression and see what happens. [typing 00:08:34.631 --> 00:08:40.306 sound] The value that the expression evaluates to is true. An d let's use the 00:08:40.306 --> 00:08:45.815 parentheses to see which of the two operators applied first. We'll begin by 00:08:45.815 --> 00:08:51.287 putting the parentheses around the or part of the expression, ensuring that or 00:08:51.287 --> 00:08:57.884 applies before not. And when we do that, the result is false. So, that's not what 00:08:57.884 --> 00:09:04.426 happened when we left off the parentheses, parentheses. And that means that that's 00:09:04.426 --> 00:09:11.129 not the order of precedence. Instead, the order of precedence is that not is applied 00:09:11.129 --> 00:09:17.912 first, as I can show here, followed by or. The order of precedence for logical 00:09:17.912 --> 00:09:23.949 operators is not, and, and then or. And when we're working with multiple logical 00:09:23.949 --> 00:09:29.979 operators within an expression, we can use parentheses to ensure that the operations 00:09:29.979 --> 00:09:35.220 apply in the order we'd like without having to worry about what the order of 00:09:35.220 --> 00:09:40.189 precedence is. Sometimes, we'll use parentheses to make an expression more 00:09:40.189 --> 00:09:45.431 readable. For example, in this expression, the arithmetic operators have higher 00:09:45.431 --> 00:09:50.876 precedence than the Boolean operators or logical operators, so these parentheses 00:09:50.876 --> 00:09:56.254 are unnecessary but we include them for readability. Instead, we could have left 00:09:56.254 --> 00:10:02.061 off the parentheses and had the following, where some, I find a little harder to read 00:10:02.061 --> 00:10:03.114 and understand.