1 00:00:00,000 --> 00:00:13,330 So sometimes, we can actually omit the else clause if we don't need it. 2 00:00:13,330 --> 00:00:16,365 We can just have an if with no accompanying else. 3 00:00:16,365 --> 00:00:21,420 So, in this code, we first assign the variable X to 10, and then we say, 4 00:00:21,420 --> 00:00:23,775 if X is less than zero, so in other words, 5 00:00:23,775 --> 00:00:25,095 if X is negative, 6 00:00:25,095 --> 00:00:26,925 then we print this out. 7 00:00:26,925 --> 00:00:30,345 Note here, we don't have an else after this if, 8 00:00:30,345 --> 00:00:32,895 instead we just have a print statement. 9 00:00:32,895 --> 00:00:37,500 That as the contents imply, always gets printed. 10 00:00:37,500 --> 00:00:39,750 So, when we run this code, 11 00:00:39,750 --> 00:00:41,415 because X is 10, 12 00:00:41,415 --> 00:00:44,520 we should expect the expression X less than zero to 13 00:00:44,520 --> 00:00:47,700 be false and so we should expect only, 14 00:00:47,700 --> 00:00:50,415 this is always printed to printout. 15 00:00:50,415 --> 00:00:53,460 If we run our code, that's the case. 16 00:00:53,460 --> 00:00:57,300 Now, what if we changed X to be negative 10? 17 00:00:57,300 --> 00:00:59,115 Rather than running this code, 18 00:00:59,115 --> 00:01:02,870 I'm just going to go to this multiple choice question which has that exact code. 19 00:01:02,870 --> 00:01:05,425 So, we first have X equals negative 10. 20 00:01:05,425 --> 00:01:08,265 Then we say, if X is less than zero, 21 00:01:08,265 --> 00:01:10,110 then printout the negative number. 22 00:01:10,110 --> 00:01:11,805 X is not valid here, 23 00:01:11,805 --> 00:01:14,580 then we printout this is always printed. 24 00:01:14,580 --> 00:01:20,625 So, we should expect that because negative 10 is less than zero, 25 00:01:20,625 --> 00:01:24,090 that this if clause is going to execute, 26 00:01:24,090 --> 00:01:26,325 so it should printout the negative number, 27 00:01:26,325 --> 00:01:29,115 negative 10 is not valid here. 28 00:01:29,115 --> 00:01:33,615 Then, this is always printed because it's outside of the if, 29 00:01:33,615 --> 00:01:35,745 should be printed as well. 30 00:01:35,745 --> 00:01:39,670 So, our answer should be B. 31 00:01:42,920 --> 00:01:47,025 In this question, we have similar code. 32 00:01:47,025 --> 00:01:49,695 So we say X is negative 10. 33 00:01:49,695 --> 00:01:51,240 If X is less than zero, 34 00:01:51,240 --> 00:01:52,560 then printout the negative number. 35 00:01:52,560 --> 00:01:53,985 X is not valid here. 36 00:01:53,985 --> 00:01:56,760 But then we add in else saying, 37 00:01:56,760 --> 00:02:00,330 otherwise print X is a positive number. 38 00:02:00,330 --> 00:02:06,500 But then we have another else that prints out this is always printed. 39 00:02:06,500 --> 00:02:10,985 The question is, will the following code cause an error? 40 00:02:10,985 --> 00:02:14,215 The answer is going to be yes. 41 00:02:14,215 --> 00:02:20,090 The reason is that every else has to have an accompanying if, right? 42 00:02:20,090 --> 00:02:25,160 So, you never would start off a conversation or a sentence by starting with otherwise. 43 00:02:25,160 --> 00:02:26,445 If you do that, 44 00:02:26,445 --> 00:02:28,560 then it's like well, otherwise what? 45 00:02:28,560 --> 00:02:32,455 You have to have a thing that you're saying otherwise in response to. 46 00:02:32,455 --> 00:02:36,695 So, every else always has to have an accompanying if. 47 00:02:36,695 --> 00:02:40,315 So, this else corresponds with this if. 48 00:02:40,315 --> 00:02:44,000 This else has no if that it corresponds with. 49 00:02:44,000 --> 00:02:47,000 So, Python says, I don't understand what we're actually 50 00:02:47,000 --> 00:02:50,330 writing here and so it's going to give us a syntax error. 51 00:02:50,330 --> 00:02:53,365 So yes, this code causes an error. 52 00:02:53,365 --> 00:02:58,640 One thing to note with conditionals is that inside of an if block or an else block, 53 00:02:58,640 --> 00:03:01,715 we can also have another set of conditionals. 54 00:03:01,715 --> 00:03:06,170 So remember that whatever goes inside of a block, so for example, 55 00:03:06,170 --> 00:03:09,375 inside of this else block is all indented 56 00:03:09,375 --> 00:03:14,460 and only runs if what's inside of this if is false. 57 00:03:14,460 --> 00:03:19,295 But, we can have other if and else statements inside of either these blocks. 58 00:03:19,295 --> 00:03:21,090 So for example, here, 59 00:03:21,090 --> 00:03:22,510 inside of this else block, 60 00:03:22,510 --> 00:03:27,530 we have another if statement saying if X is greater than Y, then do something. 61 00:03:27,530 --> 00:03:31,925 Otherwise in this else corresponds with this if. 62 00:03:31,925 --> 00:03:35,650 So, otherwise print X and Y must be equal. 63 00:03:35,650 --> 00:03:38,500 So, these are what are called nested conditionals. 64 00:03:38,500 --> 00:03:40,600 You can have any level of nesting. 65 00:03:40,600 --> 00:03:44,905 So for example, this if block could have another if else statement, 66 00:03:44,905 --> 00:03:48,830 which could have another if else statement as many times as we want it to. 67 00:03:48,830 --> 00:03:50,970 So, when we look at this code, 68 00:03:50,970 --> 00:03:53,850 we first assign X and Y to 10, 69 00:03:53,850 --> 00:03:55,575 on lines one and two. 70 00:03:55,575 --> 00:03:59,205 Then we say, if X is less than Y, 71 00:03:59,205 --> 00:04:02,880 so 10 is not less than 10. 72 00:04:02,880 --> 00:04:05,730 So, this statement is false. 73 00:04:05,730 --> 00:04:09,615 What will happen is we'll jump to the else. 74 00:04:09,615 --> 00:04:13,095 Then we say, if X is greater than Y, 75 00:04:13,095 --> 00:04:16,540 but 10 is not greater than 10. 76 00:04:16,540 --> 00:04:18,950 So, this is also false. 77 00:04:18,950 --> 00:04:22,085 So, we then jump to this else, 78 00:04:22,085 --> 00:04:26,950 and what gets printed out is X and Y must be equal. 79 00:04:26,950 --> 00:04:31,145 So, let's run our code just to verify that this is the case. 80 00:04:31,145 --> 00:04:34,780 So, we get X and Y must be equal. 81 00:04:34,780 --> 00:04:37,750 Let's change our values for X and Y a little bit. 82 00:04:37,750 --> 00:04:42,400 So, let's start by changing X2 instead be five. 83 00:04:42,400 --> 00:04:45,440 So, when we change X to five, 84 00:04:45,440 --> 00:04:49,350 then this if X is less than Y, 85 00:04:49,350 --> 00:04:51,930 so five is less than 10. 86 00:04:51,930 --> 00:04:57,840 That's true and so we printout X is less than Y, 87 00:04:57,840 --> 00:05:01,530 and we skip this entire else block. 88 00:05:01,530 --> 00:05:05,960 So, the only thing that gets printed out is X is less than Y. 89 00:05:07,440 --> 00:05:11,335 Okay? So, now let's change our numbers slightly again. 90 00:05:11,335 --> 00:05:14,165 So, I'm going to say X is 10 again, 91 00:05:14,165 --> 00:05:17,565 and I'm going to change Y to be three. 92 00:05:17,565 --> 00:05:25,195 So now, 10 less than three, this is false. 93 00:05:25,195 --> 00:05:29,625 So, we skip to the else, and then we say, 94 00:05:29,625 --> 00:05:31,395 if X is greater than Y, 95 00:05:31,395 --> 00:05:35,865 10 greater than three is true. 96 00:05:35,865 --> 00:05:40,405 So, we run what's inside of this nested if, 97 00:05:40,405 --> 00:05:44,060 and we printout X is greater than Y. 98 00:05:45,290 --> 00:05:48,860 So, we're going to come back to this code and learn 99 00:05:48,860 --> 00:05:53,015 one new construct of if and else statements called an elif. 100 00:05:53,015 --> 00:05:56,225 But before then, let's answer some questions. 101 00:05:56,225 --> 00:05:59,480 So, in this code, we have another nested conditional 102 00:05:59,480 --> 00:06:03,395 because this else has an if else inside of it. 103 00:06:03,395 --> 00:06:05,375 The question we're asking is, 104 00:06:05,375 --> 00:06:07,685 will this code cause an error? 105 00:06:07,685 --> 00:06:10,610 Well, I don't see anything invalid in 106 00:06:10,610 --> 00:06:15,035 this code because this else is allowed to have this if else in it. 107 00:06:15,035 --> 00:06:16,810 So I'm going to say, no, 108 00:06:16,810 --> 00:06:19,325 this code is not going to cause an error. 109 00:06:19,325 --> 00:06:22,895 So, let's actually figure out what this code is going to output. 110 00:06:22,895 --> 00:06:27,360 So, we assign X to be negative 10 first, and then we say, 111 00:06:27,360 --> 00:06:29,010 if X is less than zero, 112 00:06:29,010 --> 00:06:30,795 printout the negative number, 113 00:06:30,795 --> 00:06:33,435 negative 10 is not valid here. 114 00:06:33,435 --> 00:06:35,400 Because this if ran, 115 00:06:35,400 --> 00:06:37,590 then we entirely skip this else. 116 00:06:37,590 --> 00:06:40,760 So, the only thing that would get printed is the negative number, 117 00:06:40,760 --> 00:06:43,910 negative 10 is not valid here. 118 00:06:43,910 --> 00:06:47,944 Now, recall this code from the nested conditionals discussion. 119 00:06:47,944 --> 00:06:49,905 So here, we assign two variables, 120 00:06:49,905 --> 00:06:51,450 X and Y to be numbers. 121 00:06:51,450 --> 00:06:52,980 If X is less than Y, 122 00:06:52,980 --> 00:06:55,050 we want to printout X is less than Y. 123 00:06:55,050 --> 00:06:56,520 If X is greater than Y, 124 00:06:56,520 --> 00:06:58,530 we want to printout X is greater than Y, 125 00:06:58,530 --> 00:07:02,920 and if they're equal, we want to printout X and Y must be equal. 126 00:07:03,080 --> 00:07:07,530 So far, we've needed to do this through nested conditionals. 127 00:07:07,530 --> 00:07:08,685 So, in other words, 128 00:07:08,685 --> 00:07:14,220 this else statement had to contain another if statement and else statement. 129 00:07:14,220 --> 00:07:16,200 So, in other words, 130 00:07:16,200 --> 00:07:17,925 if X is less than Y, 131 00:07:17,925 --> 00:07:19,650 we printout X is less than Y. 132 00:07:19,650 --> 00:07:22,020 If X is not less than Y, 133 00:07:22,020 --> 00:07:24,105 but it is instead greater than Y, 134 00:07:24,105 --> 00:07:26,625 then we printout X is greater than Y. 135 00:07:26,625 --> 00:07:29,190 If X is not less than Y, 136 00:07:29,190 --> 00:07:31,230 and X is not greater than Y, 137 00:07:31,230 --> 00:07:33,465 then we printout that they must be equal. 138 00:07:33,465 --> 00:07:38,465 But Python offers a cleaner way to express conditionals like this. 139 00:07:38,465 --> 00:07:40,465 So, in this case, 140 00:07:40,465 --> 00:07:44,270 rather than having an if and else contained within this else, 141 00:07:44,270 --> 00:07:46,910 we can write something called an elif. 142 00:07:46,910 --> 00:07:50,330 So, an elif is short for else if. 143 00:07:50,330 --> 00:07:56,080 So, elif looks exactly like this. 144 00:08:03,620 --> 00:08:07,640 So now, we have the exact same logic that we had before, 145 00:08:07,640 --> 00:08:10,955 but it's a little flatter and doesn't require nesting. 146 00:08:10,955 --> 00:08:13,970 So, we say, if X is less than Y, 147 00:08:13,970 --> 00:08:17,005 then printout X is less than Y, just like before. 148 00:08:17,005 --> 00:08:18,480 But then after that, 149 00:08:18,480 --> 00:08:21,450 we write elif, which again is short for else if. 150 00:08:21,450 --> 00:08:25,170 So in other words, if X is not less than Y, 151 00:08:25,170 --> 00:08:28,050 but X is greater than Y, 152 00:08:28,050 --> 00:08:30,120 then we print this out. 153 00:08:30,120 --> 00:08:32,820 Now, this also saying, 154 00:08:32,820 --> 00:08:37,460 if none of the conditions in this elif or this if were true, 155 00:08:37,460 --> 00:08:39,625 then run what's in here. 156 00:08:39,625 --> 00:08:41,880 So, when I run this code, 157 00:08:41,880 --> 00:08:43,950 when X and Y are 10, 158 00:08:43,950 --> 00:08:47,830 then I'm going to printout X and Y must be equal. 159 00:08:48,690 --> 00:08:51,190 So, just like "else", 160 00:08:51,190 --> 00:08:54,370 every elif has to have an accompanying "if". 161 00:08:54,370 --> 00:08:57,910 So, this elif corresponds to this "if", 162 00:08:57,910 --> 00:09:01,210 and this "else" corresponds to this "if" as well. 163 00:09:01,210 --> 00:09:06,070 Remember, you don't start off a sentence or a conversation by saying otherwise. 164 00:09:06,070 --> 00:09:10,465 Likewise, you aren't going to start off an "if conditional" by saying elif, 165 00:09:10,465 --> 00:09:13,550 you always have to start with an "if". 166 00:09:13,590 --> 00:09:19,030 So, here's a diagram that explains where you use if, elif, and else. 167 00:09:19,030 --> 00:09:25,165 So, every conditional group always starts out with exactly one "if". 168 00:09:25,165 --> 00:09:28,750 So, we have "if" and then some Boolean expression and 169 00:09:28,750 --> 00:09:32,950 then some code to run if that Boolean expression is true. 170 00:09:32,950 --> 00:09:36,595 Then, we can have any number of elifs. 171 00:09:36,595 --> 00:09:41,455 In other words, we can have zero or more elifs that come right after that "if". 172 00:09:41,455 --> 00:09:44,305 So, if we say elif condition two, 173 00:09:44,305 --> 00:09:46,675 then this block runs, 174 00:09:46,675 --> 00:09:52,525 "if condition two is true and nothing earlier in this 'if' group run," so in other words, 175 00:09:52,525 --> 00:09:55,060 condition one was not true. 176 00:09:55,060 --> 00:09:59,995 This elif condition three runs if condition three is true, 177 00:09:59,995 --> 00:10:04,825 and condition one and condition two were not true, and so on. 178 00:10:04,825 --> 00:10:06,535 So, at condition x, 179 00:10:06,535 --> 00:10:14,385 then this runs if condition x is true and none of these other ifs or elifs were true. 180 00:10:14,385 --> 00:10:18,495 So then, after the "if" in any number of elifs, 181 00:10:18,495 --> 00:10:21,995 we can have either zero or one "else" statements. 182 00:10:21,995 --> 00:10:26,185 In other words, we can omit this "else" statement or we can have one of them. 183 00:10:26,185 --> 00:10:30,100 This "else" statement is going to run only if none of 184 00:10:30,100 --> 00:10:34,970 these conditions inside of this if block were true. 185 00:10:36,660 --> 00:10:39,085 So, in other words, this is saying, 186 00:10:39,085 --> 00:10:41,470 "If something's true, otherwise, 187 00:10:41,470 --> 00:10:45,625 if that thing was false and this thing is true, otherwise, 188 00:10:45,625 --> 00:10:49,525 if everything else so far was false and this is true, 189 00:10:49,525 --> 00:10:52,180 and then the 'else' is saying, otherwise, 190 00:10:52,180 --> 00:10:57,200 if everything so far was false, then run this." 191 00:10:57,330 --> 00:11:02,305 So, for example, here are some valid if, elif, else orders. 192 00:11:02,305 --> 00:11:05,320 So, we can have an "if" with nothing else, 193 00:11:05,320 --> 00:11:09,280 we can have an "if" with one "else" statement, 194 00:11:09,280 --> 00:11:14,150 we can have an "if" that has one elif and and an "else", 195 00:11:14,150 --> 00:11:20,520 and we can have an "if" with two or any number of elifs with no "else". 196 00:11:20,520 --> 00:11:25,065 So, let's answer some questions about if, elif, and else. 197 00:11:25,065 --> 00:11:28,685 So, here we have some code and the question asks, 198 00:11:28,685 --> 00:11:30,690 "Which one of one, two, 199 00:11:30,690 --> 00:11:34,545 or three gives the same result as the following nested 'if'". 200 00:11:34,545 --> 00:11:40,155 So, in other words, what we're doing is we're trying to replicate this code within elif. 201 00:11:40,155 --> 00:11:43,510 So, what I would do is I would say, "Okay, 202 00:11:43,510 --> 00:11:47,365 If we join this 'if' and 'else' into an elif, 203 00:11:47,365 --> 00:11:50,095 so I would say if x is less than zero, 204 00:11:50,095 --> 00:11:54,880 elif x greater than zero, and then 'else'". 205 00:11:54,880 --> 00:12:01,900 So, this code is not valid because we have "else" and then a conditional, 206 00:12:01,900 --> 00:12:04,975 but whenever we write "else" we can't have a conditional, 207 00:12:04,975 --> 00:12:07,615 so we know that the answer isn't one, 208 00:12:07,615 --> 00:12:10,760 and in fact, one would cause a syntax error. 209 00:12:10,890 --> 00:12:16,315 For answer two, we have pretty much exactly what we wrote here. 210 00:12:16,315 --> 00:12:25,690 So, we joined this "else" and "if" and made this "else" a clause on this "if". 211 00:12:25,690 --> 00:12:29,230 So, I'm going to say that the answer is answer two. 212 00:12:29,230 --> 00:12:32,365 But let's look at answer three just in case. 213 00:12:32,365 --> 00:12:35,875 So, we have an "If x is less than zero, 214 00:12:35,875 --> 00:12:38,680 then print out the negative number x is not valid here", 215 00:12:38,680 --> 00:12:41,095 and then we have another "if". 216 00:12:41,095 --> 00:12:42,565 So, in other words, 217 00:12:42,565 --> 00:12:46,210 we have one "if" clause that goes from here here, 218 00:12:46,210 --> 00:12:51,490 and another "if" group that goes from here to here, 219 00:12:51,490 --> 00:12:53,485 then includes an "else". 220 00:12:53,485 --> 00:12:58,090 Now, this code looks like it's going to be exactly the same, 221 00:12:58,090 --> 00:13:00,715 but it's actually a little bit different. 222 00:13:00,715 --> 00:13:03,040 So, I'm going to answer. 223 00:13:03,040 --> 00:13:05,660 Number two is the answer here. 224 00:13:11,340 --> 00:13:15,260 We can tell that that's the right answer. 225 00:13:15,300 --> 00:13:17,725 So, this question asks, 226 00:13:17,725 --> 00:13:21,115 "What will the following code print if x is equal to three, 227 00:13:21,115 --> 00:13:22,720 y is equal to five, 228 00:13:22,720 --> 00:13:25,180 and z is equal to two?" 229 00:13:25,180 --> 00:13:30,340 So here, we have a slightly more complex Boolean expression to say, 230 00:13:30,340 --> 00:13:35,890 "if x is less than y and x is less than t." So first, 231 00:13:35,890 --> 00:13:40,645 is three less than five? 232 00:13:40,645 --> 00:13:45,925 Yes, that's true. Then we ask, 233 00:13:45,925 --> 00:13:49,900 "Is three less than two?" 234 00:13:49,900 --> 00:13:57,620 That's false. So, true and false is false, 235 00:14:00,720 --> 00:14:04,990 so this "if" does not execute. 236 00:14:04,990 --> 00:14:07,855 If y is less than x, 237 00:14:07,855 --> 00:14:11,995 so five is not less than three. 238 00:14:11,995 --> 00:14:16,610 So, we know that this is going to be false. 239 00:14:16,710 --> 00:14:20,860 We don't even need to know whether this is true or false 240 00:14:20,860 --> 00:14:25,490 because we know that false and any other value is going to be false. 241 00:14:26,880 --> 00:14:31,795 So, what that means is that we run what's inside of the "else" clause, 242 00:14:31,795 --> 00:14:38,125 so we print out C. In this question, 243 00:14:38,125 --> 00:14:41,785 we're asked to write code using one conditional 244 00:14:41,785 --> 00:14:46,045 to find whether the string false is in the string str1, 245 00:14:46,045 --> 00:14:47,890 which is specified here. 246 00:14:47,890 --> 00:14:53,035 So, I'm going to first write out the Boolean expression to express that. 247 00:14:53,035 --> 00:14:58,915 So, that's the string false in str1. 248 00:14:58,915 --> 00:15:01,240 In the question, we say, 249 00:15:01,240 --> 00:15:06,715 "If that's true, then assign the variable output to this string." 250 00:15:06,715 --> 00:15:09,535 So, I'm going to say, 251 00:15:09,535 --> 00:15:11,710 "If this is true, 252 00:15:11,710 --> 00:15:20,050 then output equals the string 'False. 253 00:15:20,050 --> 00:15:25,645 You aren't you?'" Then it asks, 254 00:15:25,645 --> 00:15:28,945 "Check to see if the string 'true' is in str1, 255 00:15:28,945 --> 00:15:31,645 and if that is, then assign 'True. 256 00:15:31,645 --> 00:15:33,610 You are you' to the variable output." 257 00:15:33,610 --> 00:15:36,250 So, I'm going to put that in an elif. 258 00:15:36,250 --> 00:15:46,420 So elif true is in str1, output equals "True. 259 00:15:46,420 --> 00:15:49,220 You are you." 260 00:15:50,340 --> 00:15:53,815 If neither are in str1, 261 00:15:53,815 --> 00:15:57,160 then assign this string to the variable output. 262 00:15:57,160 --> 00:16:00,715 So, we're going to express that with an "else". 263 00:16:00,715 --> 00:16:05,170 Else, remember else doesn't take a condition because its condition 264 00:16:05,170 --> 00:16:09,325 is implicitly that neither of these conditions were true. 265 00:16:09,325 --> 00:16:18,170 So, we say, "'else' output equals neither true nor false." 266 00:16:21,870 --> 00:16:27,080 I'll leave it up to you to complete the remaining exercises.