1 00:00:09,054 --> 00:00:12,272 So, so far we've seen two ways of creating Booleans. 2 00:00:12,272 --> 00:00:18,776 We've seen literal expressions That's either true or false. 3 00:00:18,776 --> 00:00:22,570 We've seen comparison operators, 4 00:00:29,637 --> 00:00:31,290 That compare two values. 5 00:00:31,290 --> 00:00:36,288 So there's equal to, not equal to, less than, less than or equal to, greater than, 6 00:00:36,288 --> 00:00:37,798 greater than or equal to. 7 00:00:37,798 --> 00:00:44,200 And all of these compare one thing on the left with the one thing on the right. 8 00:00:48,547 --> 00:00:52,938 And the value of the overall expression is going to be true or false, again, 9 00:00:52,938 --> 00:00:53,632 a Boolean. 10 00:00:53,632 --> 00:00:59,450 Now, there's another way to create Booleans, which are logical operators. 11 00:01:06,702 --> 00:01:11,406 So like comparison operators, logical operators expect one thing on the left and 12 00:01:11,406 --> 00:01:13,670 one thing on the right. 13 00:01:13,670 --> 00:01:20,964 So the logical operators that we'll cover, and, or, and not. 14 00:01:20,964 --> 00:01:23,910 So and, and or, just like comparison operators, 15 00:01:23,910 --> 00:01:29,340 expect one thing on the left, And one thing on the right. 16 00:01:31,986 --> 00:01:38,970 Not, only expects one thing that I'll call B short for Boolean. 17 00:01:38,970 --> 00:01:43,290 Unlike comparison operators where the thing on the left and the thing on 18 00:01:43,290 --> 00:01:47,970 the right can be an integer or string or pretty much any time logical operators 19 00:01:47,970 --> 00:01:52,882 expect the thing on the left and the thing on the right to be Boolean expressions. 20 00:01:52,882 --> 00:01:59,740 So, the value of L and R is going to be true if both L and R are true. 21 00:01:59,740 --> 00:02:05,059 The value of L or R is going to be true if either L is true or 22 00:02:05,059 --> 00:02:07,856 R is true, or both are true. 23 00:02:07,856 --> 00:02:12,650 And, the value of not B is going to be true only if B is false. 24 00:02:14,150 --> 00:02:16,930 In order to make it clear how these logical operators work, 25 00:02:16,930 --> 00:02:19,223 I'm going to draw what's called a truth table. 26 00:02:19,223 --> 00:02:22,560 So, I'll start by drawing the truth table for, and. 27 00:02:24,220 --> 00:02:27,412 So, remember that L and R are both Booleans. 28 00:02:27,412 --> 00:02:31,460 And, so L is either going to be true or false. 29 00:02:31,460 --> 00:02:35,483 So I'm going to represent whether L is true or false in columns. 30 00:02:35,483 --> 00:02:39,150 So here, the first column is going to represent L being true. 31 00:02:42,060 --> 00:02:45,970 And the second column is going to represent L being false. 32 00:02:49,373 --> 00:02:55,029 I'm going to represent the value of R in rows so 33 00:02:55,029 --> 00:02:58,799 R being true is the first row and 34 00:02:58,799 --> 00:03:02,730 R being false is the second row. 35 00:03:03,870 --> 00:03:07,124 So every cell represents the combination between L and 36 00:03:07,124 --> 00:03:09,820 R values represented by that row and column. 37 00:03:09,820 --> 00:03:14,570 So here, this cell represents L is true and R is true. 38 00:03:14,570 --> 00:03:17,246 This represents L is true and R is false. 39 00:03:17,246 --> 00:03:21,944 This represents L is false and R is true and this represents L is false and 40 00:03:21,944 --> 00:03:22,740 R is false. 41 00:03:24,230 --> 00:03:28,848 And I'm going to write the value of L and R in each cell. 42 00:03:28,848 --> 00:03:33,820 So here if L is true and R is true then L and R is true. 43 00:03:36,190 --> 00:03:43,484 If R is false, but L is still true, then the value of L and R is going to be false. 44 00:03:43,484 --> 00:03:47,436 And if the reverse were true, so if L were false but R was true, 45 00:03:47,436 --> 00:03:50,260 then the value of L and R is going to be false. 46 00:03:51,430 --> 00:03:58,305 And if both are false, the value of L and R is going to be false. 47 00:03:58,305 --> 00:04:03,620 So you can see that L and R is true only if L is true and if R is true. 48 00:04:04,790 --> 00:04:06,228 Let's write out the tree table for or. 49 00:04:09,030 --> 00:04:13,004 So just like and, we have L and R. 50 00:04:13,004 --> 00:04:19,653 So we have L true, L false and 51 00:04:19,653 --> 00:04:25,480 we have R true, R false. 52 00:04:28,030 --> 00:04:33,490 So if the value of both of these is true then the value of L or 53 00:04:33,490 --> 00:04:35,030 R is going to be true. 54 00:04:36,420 --> 00:04:39,870 If L is true and R is false, then L or R is true. 55 00:04:41,790 --> 00:04:46,086 Same thing with if R is true but L is false. 56 00:04:46,086 --> 00:04:51,809 The only way that L or R is going to be false is if L is false and R is false. 57 00:04:56,026 --> 00:05:01,820 So you can see L or R is going to be true if either L is true or R is true. 58 00:05:01,820 --> 00:05:07,661 And then the table for not is a little simpler. 59 00:05:07,661 --> 00:05:10,060 So if B is true 60 00:05:16,512 --> 00:05:21,070 If B is true, then the value of not B is going to be false. 61 00:05:22,420 --> 00:05:27,724 If B is false, the value of not B is going to be true. 62 00:05:27,724 --> 00:05:32,330 So you can see that not B is only true if B is false. 63 00:05:33,420 --> 00:05:35,800 So, when we write out logical operators, 64 00:05:35,800 --> 00:05:39,340 you don't need to write out these truth tables entirely. 65 00:05:39,340 --> 00:05:44,270 But, I use these truth tables as a way to understand systematically how and, 66 00:05:44,270 --> 00:05:45,890 or, and not work. 67 00:05:48,150 --> 00:05:49,360 So, let's look at some code. 68 00:05:51,180 --> 00:05:55,450 In this code, we assign the variable x to have the value five, and 69 00:05:55,450 --> 00:06:00,920 we print up X greater than 0 and X less than 10. 70 00:06:00,920 --> 00:06:06,192 So, here on the left, we have one Boolean expression. 71 00:06:06,192 --> 00:06:09,930 And, we have another Boolean expression here on the right. 72 00:06:09,930 --> 00:06:14,369 What that means is that the value of this overall Boolean expression is 73 00:06:14,369 --> 00:06:19,197 going to be true only if this expression is true and this expression is true. 74 00:06:19,197 --> 00:06:22,620 So we can check, is x greater than 0? 75 00:06:22,620 --> 00:06:26,514 Well x is equal to 5 and so yes 5 is greater than 0. 76 00:06:26,514 --> 00:06:27,970 So this is true. 77 00:06:29,490 --> 00:06:31,138 Is x less than 10? 78 00:06:31,138 --> 00:06:36,110 Well, I see that x is 5 and yes 5 is less than 10. 79 00:06:36,110 --> 00:06:40,940 So this is true, and what that means is that if this is true and 80 00:06:40,940 --> 00:06:44,960 this is true, then we're here in our truth table, 81 00:06:44,960 --> 00:06:48,870 meaning that the value of this overall expression is true. 82 00:06:52,490 --> 00:06:58,008 On line four, we assign a variable n to have the value 25. 83 00:06:58,008 --> 00:07:02,250 And we print N modulo 2 equals 0. 84 00:07:02,250 --> 00:07:05,214 Now remember that modulo means remainder, so 85 00:07:05,214 --> 00:07:09,626 what this is saying is what's the remainder of when we divide n by 2. 86 00:07:09,626 --> 00:07:14,517 So when we pick modulo 2, we're either going to get 0 if it's an even number, 87 00:07:14,517 --> 00:07:18,751 or one if it's an odd number because even numbers will divide by 2 and 88 00:07:18,751 --> 00:07:20,470 have no remainder. 89 00:07:20,470 --> 00:07:23,200 Odd numbers will divide by 2 and have remainder of 1. 90 00:07:23,200 --> 00:07:28,162 Here we see that n is 25 which is an odd number, 91 00:07:28,162 --> 00:07:31,400 so n modulo 2 is going to be 1. 92 00:07:31,400 --> 00:07:38,953 If we take N modulo 3, Matt's asking is there a remainder when we divide by 3 And 93 00:07:38,953 --> 00:07:44,742 we should actually have a remainder of 1 when we divide by 3. 94 00:07:44,742 --> 00:07:49,546 And so what that means is that this expression, n modulo 3==0, 95 00:07:49,546 --> 00:07:53,931 the value of this expression is going to be false because it's 96 00:07:53,931 --> 00:07:57,750 comparing 1 with 0 and checking if they're equal. 97 00:07:59,926 --> 00:08:02,567 And n%3 == 0. 98 00:08:02,567 --> 00:08:06,036 The value of this overall expression is also going to be false 99 00:08:08,880 --> 00:08:11,517 Because 1 is not equal to 0. 100 00:08:11,517 --> 00:08:18,250 And so when we ask false or false, then we fall here in the truth table. 101 00:08:18,250 --> 00:08:20,994 And false or false is going to be false. 102 00:08:23,867 --> 00:08:27,580 So I'm printing out true and then I'm printing out false. 103 00:08:27,580 --> 00:08:29,322 Let's check our work. 104 00:08:29,322 --> 00:08:35,837 So you can see we get true, and then false So if 105 00:08:35,837 --> 00:08:41,440 you recall from a previous lecture, Python has a notion of operator precedence. 106 00:08:41,440 --> 00:08:49,924 So at the highest precedence Is when we have parentheses. 107 00:08:55,920 --> 00:08:58,284 And then just below that is exponentiation. 108 00:09:01,789 --> 00:09:07,040 That's when we say something like x**2 which stands for x squared. 109 00:09:08,100 --> 00:09:11,296 Below that, we have multiplication and division. 110 00:09:15,002 --> 00:09:20,782 So that's x times 5 or x divided by 5, 111 00:09:20,782 --> 00:09:29,120 then we have addition and subtraction, x plus 2. 112 00:09:29,120 --> 00:09:30,848 And so our comparison and 113 00:09:30,848 --> 00:09:34,719 logical operators do fall in this order of precedence. 114 00:09:34,719 --> 00:09:39,635 In fact, comparison operators fall just under addition and subtraction, so 115 00:09:39,635 --> 00:09:45,500 here we have comparison Like equals equals 116 00:09:48,566 --> 00:09:55,360 And then below that, we have the not operator like not x. 117 00:09:56,390 --> 00:09:59,780 And then below that we have and or 118 00:09:59,780 --> 00:10:06,360 or so that's like x and y. 119 00:10:08,060 --> 00:10:12,840 What that means is that if we have an expression like x greater 120 00:10:12,840 --> 00:10:17,720 than five and y less than two. 121 00:10:18,830 --> 00:10:23,770 Then Python is first going to evaluate this expression because 122 00:10:23,770 --> 00:10:28,810 this comparison between x and five has a higher precedence than the and 123 00:10:30,010 --> 00:10:32,650 And then it's going to evaluate this overall expression. 124 00:10:34,040 --> 00:10:35,700 That's all for now until next time