1 00:00:07,880 --> 00:00:12,240 Welcome back. So, we've already covered booleans which allow us 2 00:00:12,240 --> 00:00:15,690 to write expressions that evaluate to either true or false. 3 00:00:15,690 --> 00:00:20,235 But knowing if something is true or false isn't typically that useful on it's own. 4 00:00:20,235 --> 00:00:22,180 Most of the time when we use Booleans, 5 00:00:22,180 --> 00:00:25,590 we use them in combination with conditional execution. 6 00:00:25,590 --> 00:00:28,200 Conditional execution allows us to say, 7 00:00:28,200 --> 00:00:30,915 "Run this piece of code if something is true, 8 00:00:30,915 --> 00:00:33,570 run this piece of code if something else is true, 9 00:00:33,570 --> 00:00:35,890 or run this piece of code if something is false". 10 00:00:35,890 --> 00:00:39,000 So, let's jump right into conditional execution. 11 00:00:39,000 --> 00:00:43,400 So, we do conditional execution with if and else statements. 12 00:00:43,400 --> 00:00:44,930 So, in this code, 13 00:00:44,930 --> 00:00:50,150 we declare a variable X and set it here to the value 15. 14 00:00:50,150 --> 00:00:55,140 Then, we say, if x modulo two equals equals zero. 15 00:00:55,140 --> 00:00:56,305 As a quick reminder, 16 00:00:56,305 --> 00:00:58,700 modulo is the remainder operator. 17 00:00:58,700 --> 00:01:03,130 So, for example, if we take 15 modulo two, 18 00:01:03,130 --> 00:01:08,480 then because 15 divided by two is seven with a remainder of one, 19 00:01:08,480 --> 00:01:10,865 modulo gives us only are there remainder. 20 00:01:10,865 --> 00:01:13,400 So, 15 modulo two is one. 21 00:01:13,400 --> 00:01:16,175 If we did 14 modulo two, 22 00:01:16,175 --> 00:01:19,280 then because 14 divides evenly into two, 23 00:01:19,280 --> 00:01:21,565 a seven then we get a remainder of zero. 24 00:01:21,565 --> 00:01:23,970 If we did 13 modular two, 25 00:01:23,970 --> 00:01:27,050 then we get a remainder of one and so on. 26 00:01:27,050 --> 00:01:31,275 So, in other words, if we take x modulo two, 27 00:01:31,275 --> 00:01:33,795 then it's going to be zero if 28 00:01:33,795 --> 00:01:43,110 x is even and it can be divided by two with no remainder, 29 00:01:43,110 --> 00:01:47,940 and it's one if x is odd. 30 00:01:47,940 --> 00:01:50,220 So, in this code, 31 00:01:50,220 --> 00:01:53,690 we say if x modulo two is zero, 32 00:01:53,690 --> 00:01:57,860 which to us this is a Boolean expression that's going to evaluate to 33 00:01:57,860 --> 00:02:03,605 true if x is even and it's going to evaluate to false if x is odd. 34 00:02:03,605 --> 00:02:07,040 So, we say if it's even, 35 00:02:07,040 --> 00:02:11,660 then print out x is even then we say else, 36 00:02:11,660 --> 00:02:14,510 so you can think of else as being like otherwise. 37 00:02:14,510 --> 00:02:19,580 So else is saying otherwise if this condition is false, 38 00:02:19,580 --> 00:02:21,290 then do something else. 39 00:02:21,290 --> 00:02:23,870 So, we say if x is even, 40 00:02:23,870 --> 00:02:25,960 print out x is even. 41 00:02:25,960 --> 00:02:29,565 Otherwise, if x is not even, 42 00:02:29,565 --> 00:02:31,545 print out x is odd. 43 00:02:31,545 --> 00:02:34,990 So when I run this code while x is 15, 44 00:02:34,990 --> 00:02:41,340 then we should expect this else condition to print out x is odd. So, let's run our code. 45 00:02:41,890 --> 00:02:45,110 Okay. So, we printed out 15 is 46 00:02:45,110 --> 00:02:49,715 odd and that indicates that we ran what's in this else clause. 47 00:02:49,715 --> 00:02:53,390 Let's modify our code slightly to set x instead to 48 00:02:53,390 --> 00:02:57,380 an even number like 14 and run it again. 49 00:02:57,380 --> 00:03:01,535 Now, we instead get 14 is even. 50 00:03:01,535 --> 00:03:09,080 So, one thing to note is that we specify what's inside of this if through indentation, 51 00:03:09,080 --> 00:03:11,645 so just the same way that we specify 52 00:03:11,645 --> 00:03:14,990 what's inside of a for-loop versus outside of the for-loop. 53 00:03:14,990 --> 00:03:18,640 Just like for-loops, we can have any number of lines here. 54 00:03:18,640 --> 00:03:26,325 So, I can say, "print it is even" in all capital letters, 55 00:03:26,325 --> 00:03:29,895 and this is inside of this if. 56 00:03:29,895 --> 00:03:35,085 I can put here print it is odd. 57 00:03:35,085 --> 00:03:37,345 So, now, when I run my code, 58 00:03:37,345 --> 00:03:39,460 I get 14 is even, 59 00:03:39,460 --> 00:03:44,240 it is even and this else clause is ignored. 60 00:03:44,240 --> 00:03:49,460 So, let's answer some questions around conditional execution. 61 00:03:49,460 --> 00:03:51,410 So, this question asks, 62 00:03:51,410 --> 00:03:53,540 how many lines of code can appear in 63 00:03:53,540 --> 00:03:57,620 the indented code block below the if and else lines in a conditional? 64 00:03:57,620 --> 00:03:59,575 So, in other words, what this is asking, 65 00:03:59,575 --> 00:04:01,525 is after we have an if, 66 00:04:01,525 --> 00:04:04,830 then we have some Boolean expression. 67 00:04:07,350 --> 00:04:12,095 How many lines can we have inside of here? 68 00:04:12,095 --> 00:04:16,435 Well really, we can have pretty much any number of lines. 69 00:04:16,435 --> 00:04:19,075 So, I'm going to put one or more. 70 00:04:19,075 --> 00:04:25,030 Because we have to have at least one thing inside of this block but we can have more, 71 00:04:25,030 --> 00:04:27,340 we can have any number of things inside of 72 00:04:27,340 --> 00:04:32,270 our if block and same thing with our else block. 73 00:04:34,260 --> 00:04:36,840 So, this question asks, 74 00:04:36,840 --> 00:04:38,885 what does the following code print? 75 00:04:38,885 --> 00:04:41,750 Choose from A, B, C or nothing. 76 00:04:41,750 --> 00:04:45,280 So, here, we say if and then we have 77 00:04:45,280 --> 00:04:49,380 a Boolean expression four plus five equals equals ten. 78 00:04:49,380 --> 00:04:52,300 So, when Python evaluates this Boolean expression, 79 00:04:52,300 --> 00:04:54,490 it's first going to evaluate four plus five 80 00:04:54,490 --> 00:04:57,550 because that's higher precedence then equals equals, 81 00:04:57,550 --> 00:05:01,765 and so four plus five evaluates to nine and then it says, 82 00:05:01,765 --> 00:05:04,160 "Is nine equal to 10?" 83 00:05:04,160 --> 00:05:06,330 When it evaluates this expression, 84 00:05:06,330 --> 00:05:08,500 then we get false. 85 00:05:10,140 --> 00:05:16,300 So, what that means is if we say if and then this expression evaluates to false, 86 00:05:16,300 --> 00:05:19,525 then we do not run what's inside of this if clause, 87 00:05:19,525 --> 00:05:22,540 instead we run what's inside of the else clause. 88 00:05:22,540 --> 00:05:23,910 Inside of the else clause, 89 00:05:23,910 --> 00:05:29,560 we have print false and so that's what I expect to be printed in this code. 90 00:05:29,560 --> 00:05:33,440 So, I'm going to answer B. 91 00:05:36,300 --> 00:05:40,965 Okay. Here's a slightly trickier question. 92 00:05:40,965 --> 00:05:44,515 We have the same conditional that we had in the previous question. 93 00:05:44,515 --> 00:05:49,110 So, from that, I'm just going to note that this is false. 94 00:05:49,720 --> 00:05:55,295 So, this if does not run but what's inside of this else runs. 95 00:05:55,295 --> 00:05:58,650 Now, if we look after this else, 96 00:05:58,650 --> 00:06:03,265 then you should see this print true is not indented. 97 00:06:03,265 --> 00:06:08,525 So, in other words, this print true is outside of this if else clause. 98 00:06:08,525 --> 00:06:13,235 So, what's inside of this else clause is just print false. 99 00:06:13,235 --> 00:06:16,700 What that means is that this print true is 100 00:06:16,700 --> 00:06:20,930 run regardless of what happens in this if-else clause. 101 00:06:20,930 --> 00:06:22,885 So, from the previous question, 102 00:06:22,885 --> 00:06:27,215 we know that the else clause is run here because nine is not equal to 10. 103 00:06:27,215 --> 00:06:31,355 So, we know that this print false is what runs first, 104 00:06:31,355 --> 00:06:33,755 but then this print true gets 105 00:06:33,755 --> 00:06:37,735 executed regardless of what happened in this if-else clause. 106 00:06:37,735 --> 00:06:39,840 So, I expect false to be printed, 107 00:06:39,840 --> 00:06:41,355 then true to be printed, 108 00:06:41,355 --> 00:06:48,490 so in other words answer is C. So, 109 00:06:48,490 --> 00:06:51,550 now I'm just going to go through one of these questions. 110 00:06:51,550 --> 00:06:53,740 The rest are fairly similar to this, 111 00:06:53,740 --> 00:06:56,215 so you should try them on your own. 112 00:06:56,215 --> 00:07:02,480 Here, we're asked to write code that assigns a string you can apply to SI to output. 113 00:07:02,480 --> 00:07:06,555 If the string SI 106 is in the list courses. 114 00:07:06,555 --> 00:07:09,885 So, I'm going to start by writing 115 00:07:09,885 --> 00:07:15,110 the Boolean expression to evaluate whether this is true or false. 116 00:07:15,110 --> 00:07:20,025 So, is the string SI 106 inside of the list courses. 117 00:07:20,025 --> 00:07:24,235 So, you might remember that the way that we do that is we can say, 118 00:07:24,235 --> 00:07:30,180 "SI 106 in courses." 119 00:07:30,180 --> 00:07:34,460 This is a Boolean expression that evaluates to true if SI 120 00:07:34,460 --> 00:07:39,305 106 is in courses and false if SI 106 is not in courses. 121 00:07:39,305 --> 00:07:41,300 Now, if I want to do something, 122 00:07:41,300 --> 00:07:42,515 if this Boolean is true, 123 00:07:42,515 --> 00:07:47,550 then I can write If and then add a colon after this. 124 00:07:47,550 --> 00:07:52,364 Now, what do I want to do if SI 106 is in courses. 125 00:07:52,364 --> 00:07:55,430 Well, it says write code to assign the string you can 126 00:07:55,430 --> 00:07:59,180 apply to SI to the variable output in that case. 127 00:07:59,180 --> 00:08:02,855 So, inside of this If, I'm going to write, 128 00:08:02,855 --> 00:08:09,780 "Output equals the string you can apply to SI!". 129 00:08:10,280 --> 00:08:14,895 Now, it says something to do if SI 106 is not in courses. 130 00:08:14,895 --> 00:08:17,530 So, if it's none courses, assign the value, 131 00:08:17,530 --> 00:08:21,005 take SI 106 to the variable output. 132 00:08:21,005 --> 00:08:25,380 So, I'm going to say, "Else". 133 00:08:26,720 --> 00:08:30,465 So, in other words, if SI 106 is not in courses, 134 00:08:30,465 --> 00:08:36,550 output equals take SI 106!. 135 00:08:40,820 --> 00:08:45,560 Okay. So, even though my code doesn't actually print anything here, 136 00:08:45,560 --> 00:08:51,480 you can see from the tests passing that we correctly assigned the variable output.