1 00:00:01,150 --> 00:00:02,550 - [Instructor] In this video we're going to 2 00:00:02,550 --> 00:00:05,570 take a look at the Boolean Operators 3 00:00:05,570 --> 00:00:08,380 called and, or, and not. 4 00:00:08,380 --> 00:00:11,620 Now these operators enable you to create more complex 5 00:00:11,620 --> 00:00:14,380 conditions then what we've been looking at so far. 6 00:00:14,380 --> 00:00:17,210 So for example we've done conditions like great, 7 00:00:17,210 --> 00:00:19,070 greater then or equal to 60 8 00:00:19,070 --> 00:00:20,970 and that's just a simple condition 9 00:00:20,970 --> 00:00:23,770 and it's true if the great is greater then or equal to 60 10 00:00:23,770 --> 00:00:25,060 and false otherwise, 11 00:00:25,060 --> 00:00:28,050 but sometimes we need to test multiple values 12 00:00:28,050 --> 00:00:31,140 and form a more compound or complex condition 13 00:00:31,140 --> 00:00:33,940 in order to get the results that we require. 14 00:00:33,940 --> 00:00:36,330 So lets say we go and define a variable, 15 00:00:36,330 --> 00:00:39,940 we'll call it gender and we'll set it to the string female 16 00:00:39,940 --> 00:00:41,700 and lets create another variable, 17 00:00:41,700 --> 00:00:43,910 we'll call it age and we'll set it to 70 18 00:00:43,910 --> 00:00:47,460 and lets say we want to print the strings senior female, 19 00:00:47,460 --> 00:00:51,220 only if the gender is female and the age is greater then or 20 00:00:51,220 --> 00:00:54,680 equal to 65, so we can go and write such an expression. 21 00:00:54,680 --> 00:00:58,480 So if gender double equal sign female 22 00:00:58,480 --> 00:01:00,860 and of course you have to spell it correctly. 23 00:01:00,860 --> 00:01:04,200 By the way if we were to use different key sensitivities 24 00:01:04,200 --> 00:01:06,740 in the two strings they would not compare as equals, 25 00:01:06,740 --> 00:01:08,670 so please keep that in mind. 26 00:01:08,670 --> 00:01:12,650 And then, we want to combine this with another condition, 27 00:01:12,650 --> 00:01:15,860 age greater then or equal to 65, 28 00:01:15,860 --> 00:01:18,700 so if both of those are conditions are true, 29 00:01:18,700 --> 00:01:21,210 then we would like a print statement to execute 30 00:01:21,210 --> 00:01:23,856 that displays senior female. 31 00:01:23,856 --> 00:01:25,750 (typing) 32 00:01:25,750 --> 00:01:28,530 And in this case, both conditions are true, 33 00:01:28,530 --> 00:01:31,890 so indeed, senior female gets displayed. 34 00:01:31,890 --> 00:01:34,410 Now the way this expression works 35 00:01:34,410 --> 00:01:38,160 is the condition to the left of the and gets evaluated. 36 00:01:38,160 --> 00:01:40,050 If this is false then, 37 00:01:40,050 --> 00:01:42,460 the if statement terminates immediately 38 00:01:42,460 --> 00:01:44,900 and we would move onto the next statement 39 00:01:44,900 --> 00:01:46,770 after the if statement. 40 00:01:46,770 --> 00:01:48,040 If it's true, 41 00:01:48,040 --> 00:01:51,540 I still don't yet know if the whole condition is true 42 00:01:51,540 --> 00:01:54,560 because I need both of these conditions to be true 43 00:01:54,560 --> 00:01:56,500 for the entire condition to be true, 44 00:01:56,500 --> 00:01:58,560 so if this is true 45 00:01:58,560 --> 00:02:01,980 and age is also greater then or equal to 65, 46 00:02:01,980 --> 00:02:04,740 which it is, then and only then, 47 00:02:04,740 --> 00:02:09,740 will this print statement actually execute as it did 48 00:02:09,790 --> 00:02:11,580 in this particular case. 49 00:02:11,580 --> 00:02:15,940 So, what's happening in the evaluation of this expression 50 00:02:15,940 --> 00:02:20,400 is a case of short circuit evaluation in python. 51 00:02:20,400 --> 00:02:23,560 If the left side of this expression were false, 52 00:02:23,560 --> 00:02:27,050 the rest of the expression would not evaluate. 53 00:02:27,050 --> 00:02:30,050 So that's a little bit of a performance feature 54 00:02:30,050 --> 00:02:33,040 of the and operator and that's true of the 55 00:02:33,040 --> 00:02:36,630 and operator in most sea based languages. 56 00:02:36,630 --> 00:02:38,410 Now we also have the or operator. 57 00:02:38,410 --> 00:02:41,730 So for this one, let's go and create another variable, 58 00:02:41,730 --> 00:02:43,780 we'll call it semester average 59 00:02:43,780 --> 00:02:45,960 and we'll give it the value of 83 60 00:02:45,960 --> 00:02:49,160 and we'll create another one called final exam 61 00:02:49,160 --> 00:02:50,870 and we'll give that 95, 62 00:02:50,870 --> 00:02:53,260 by the way, notice the naming convention 63 00:02:53,260 --> 00:02:54,980 on these two variables. 64 00:02:54,980 --> 00:02:58,170 This is from the style guide from python code. 65 00:02:58,170 --> 00:03:01,390 When you have multi word variable names, 66 00:03:01,390 --> 00:03:03,520 they recommend for python, 67 00:03:03,520 --> 00:03:06,600 that you separate the words with underscores 68 00:03:06,600 --> 00:03:11,600 unless you are coding for some library that already has 69 00:03:12,270 --> 00:03:16,150 or some code base I should say, that already has a different 70 00:03:16,150 --> 00:03:18,640 naming convention in which case you should use 71 00:03:18,640 --> 00:03:21,990 the naming convention of that code base, 72 00:03:21,990 --> 00:03:24,570 but generally when you have mulit word names 73 00:03:24,570 --> 00:03:28,680 you're going to separate them with underscores for variable 74 00:03:28,680 --> 00:03:32,570 names and function names in the python programing language. 75 00:03:32,570 --> 00:03:35,020 So let's say we want to use these two variables 76 00:03:35,020 --> 00:03:37,790 in a condition that says, 77 00:03:37,790 --> 00:03:41,500 if the semester average is greater then or equal to 90 78 00:03:41,500 --> 00:03:45,570 and, or, excuse me, or the final exam grade 79 00:03:45,570 --> 00:03:49,530 is greater then or equal to 90 in either of those two cases 80 00:03:49,530 --> 00:03:52,130 we're going to give the student an A. 81 00:03:52,130 --> 00:03:53,140 So lets try that, 82 00:03:53,140 --> 00:03:56,830 so if, oops I didn't mean to hit the tab there, 83 00:03:56,830 --> 00:04:00,950 so if semester average is greater then or equal to 90 84 00:04:00,950 --> 00:04:02,440 or 85 00:04:02,440 --> 00:04:04,490 final exam 86 00:04:04,490 --> 00:04:07,360 is greater then or equal to 90 87 00:04:07,360 --> 00:04:10,810 in either of those two cases or both of those cases 88 00:04:10,810 --> 00:04:12,333 we would like to print, 89 00:04:13,700 --> 00:04:15,960 quote student, 90 00:04:15,960 --> 00:04:17,580 I cannot type, 91 00:04:17,580 --> 00:04:19,653 student grade, 92 00:04:20,590 --> 00:04:23,313 or student gets and A, sorry about that. 93 00:04:24,270 --> 00:04:27,710 All right, so let's go ahead and execute that, 94 00:04:27,710 --> 00:04:30,640 now in this case, semester average is not 95 00:04:30,640 --> 00:04:33,600 greater then or equal to 90, so this is false. 96 00:04:33,600 --> 00:04:37,740 But it's either or, either of the two can be true 97 00:04:37,740 --> 00:04:39,850 in order for the entire thing to be true. 98 00:04:39,850 --> 00:04:42,250 So if semester average is false, I'm sorry, 99 00:04:42,250 --> 00:04:44,780 if the semester average is greater then 90, is false 100 00:04:44,780 --> 00:04:45,810 or 101 00:04:45,810 --> 00:04:47,310 gets evaluated 102 00:04:47,310 --> 00:04:49,920 and I then look at the condition to the right of that 103 00:04:49,920 --> 00:04:52,880 to see if the final exam is greater then or equal to 90. 104 00:04:52,880 --> 00:04:57,250 In this case it is, so student gets an A, is displayed. 105 00:04:57,250 --> 00:04:59,280 If both of these are false, 106 00:04:59,280 --> 00:05:02,290 then the entire expression is false. 107 00:05:02,290 --> 00:05:05,060 Now again, we get short circuit evaluation, 108 00:05:05,060 --> 00:05:08,070 so if we already know the left side is true, 109 00:05:08,070 --> 00:05:09,131 then the right side 110 00:05:09,131 --> 00:05:12,800 would not get evaluated in that particular case. 111 00:05:12,800 --> 00:05:14,410 So with the or operator, 112 00:05:14,410 --> 00:05:17,150 if the left is true or the right is true 113 00:05:17,150 --> 00:05:20,550 or both are true, the entire expression is true 114 00:05:20,550 --> 00:05:24,740 and the body will execute, otherwise if both are false, 115 00:05:24,740 --> 00:05:27,173 the entire expression is false. 116 00:05:28,180 --> 00:05:30,620 Now there's one other logical operator 117 00:05:30,620 --> 00:05:33,380 or Boolean operator as their called in python, 118 00:05:33,380 --> 00:05:36,240 which is the not operator. 119 00:05:36,240 --> 00:05:39,300 So, let's go and create a grade variable. 120 00:05:39,300 --> 00:05:42,550 We'll give it the value 87 for the moment here 121 00:05:43,420 --> 00:05:48,420 and lets do kind of a sentinel control loop type of test. 122 00:05:49,420 --> 00:05:50,900 I'll do it with an if statement here 123 00:05:50,900 --> 00:05:53,330 just cause we don't need a loop for the moment. 124 00:05:53,330 --> 00:05:54,940 But let's say we have the condition, 125 00:05:54,940 --> 00:05:57,970 not grade double equals minus one. 126 00:05:57,970 --> 00:05:59,260 So you may remember 127 00:05:59,260 --> 00:06:02,250 when we were doing our sentential controlled iteration, 128 00:06:02,250 --> 00:06:06,660 we used a flag value to indicate when to terminate 129 00:06:06,660 --> 00:06:10,040 a given loop, so in this case, we have the not operator. 130 00:06:10,040 --> 00:06:13,560 So what's going to happen is the not operator will reverse 131 00:06:13,560 --> 00:06:15,600 the condition to its right, 132 00:06:15,600 --> 00:06:19,200 so if grade is equal to minus one, 133 00:06:19,200 --> 00:06:21,490 this highlighted piece would be true 134 00:06:21,490 --> 00:06:24,440 and not true would evaluate to false, 135 00:06:24,440 --> 00:06:26,210 if the highlighted piece is false, 136 00:06:26,210 --> 00:06:28,280 not false would evaluate to true. 137 00:06:28,280 --> 00:06:31,740 So it just reverses the value of the condition, 138 00:06:31,740 --> 00:06:33,590 so in this case I would print 139 00:06:34,834 --> 00:06:37,992 (typing) the next grade 140 00:06:37,992 --> 00:06:39,020 (typing) 141 00:06:39,020 --> 00:06:42,420 is and then we'll simply output the grade. 142 00:06:42,420 --> 00:06:44,200 We could use a format string 143 00:06:44,200 --> 00:06:46,580 or an f-string if we want to here as well. 144 00:06:46,580 --> 00:06:47,480 Now at the moment, 145 00:06:47,480 --> 00:06:48,890 grade of course is 146 00:06:48,890 --> 00:06:50,960 not equal to minus one, 147 00:06:50,960 --> 00:06:53,580 so grade double equals minus one will be false, 148 00:06:53,580 --> 00:06:55,030 not false will be true, 149 00:06:55,030 --> 00:06:57,650 and therefore this print statement will execute 150 00:06:57,650 --> 00:07:02,650 and you see the next grade is 87 does indeed get displayed 151 00:07:04,020 --> 00:07:05,693 in that case, so, 152 00:07:06,850 --> 00:07:10,180 this is just a way to reverse the value of a condition, 153 00:07:10,180 --> 00:07:15,180 you can also apply the not operator to other values, 154 00:07:15,370 --> 00:07:16,203 so remember, 155 00:07:16,203 --> 00:07:20,340 every object has a true or false value associated with it 156 00:07:20,340 --> 00:07:24,660 any numeric value that is zero is considered to be false, 157 00:07:24,660 --> 00:07:28,930 any numeric that is nonzero is considered to be true, 158 00:07:28,930 --> 00:07:32,540 so the not operator would simply reverse those conditions 159 00:07:32,540 --> 00:07:37,180 and as I told you, you'll see more complex objects 160 00:07:37,180 --> 00:07:40,160 be used as true false values in 161 00:07:40,160 --> 00:07:42,560 number of scenarios in python 162 00:07:42,560 --> 00:07:45,383 and will encounter a lot of those as we move forward.