1 00:00:00,170 --> 00:00:04,880 Hello, and welcome to Chapter Three of Python for Informatics. 2 00:00:04,880 --> 00:00:06,910 Chapter One, Chapter Two, now we're 3 00:00:06,910 --> 00:00:08,640 going to get to something kind of programmy. 4 00:00:08,640 --> 00:00:15,650 I mean, assignment statements and reserved words, that just kind of gurgling. 5 00:00:15,650 --> 00:00:18,030 Now we're going to start seeing composition. 6 00:00:18,030 --> 00:00:20,563 We're going to start seeing the conditional execution. 7 00:00:20,563 --> 00:00:22,770 Gets us started, sort of, seeing the power 8 00:00:22,770 --> 00:00:25,520 of computers, where you're starting to make decisions. 9 00:00:25,520 --> 00:00:31,320 So, as always, this lecture and audio, video, and slides are also available, 10 00:00:31,320 --> 00:00:34,870 are copyright, Creative Commons Attribution. 11 00:00:34,870 --> 00:00:40,350 So, conditional steps are steps that may or may not be executed. 12 00:00:40,350 --> 00:00:42,550 So here's, here's a bit of code. 13 00:00:42,550 --> 00:00:45,930 So, and, and I draw these pictures. I won't 14 00:00:45,930 --> 00:00:48,070 draw too many of these pictures on the left-hand side. 15 00:00:48,070 --> 00:00:50,750 If you've taken a programming class, you may have seen these. 16 00:00:50,750 --> 00:00:53,020 They're sometimes called flow charts. 17 00:00:53,020 --> 00:00:55,165 Sometimes people really think these are important. 18 00:00:55,165 --> 00:00:58,520 I don't think they're all that important for understanding. 19 00:00:58,520 --> 00:01:00,144 The, the Python code is here on the 20 00:01:00,144 --> 00:01:03,227 right-hand side, and this picture's on the left-hand side. 21 00:01:03,227 --> 00:01:08,407 And, and the reality is is that this may, initially, make more sense, cognitively, 22 00:01:08,407 --> 00:01:13,710 to you, than this. But this part on the right-hand side is what's important. 23 00:01:13,710 --> 00:01:15,840 I like to call these like road maps, so you can sort of 24 00:01:15,840 --> 00:01:19,600 trace where the code is going by driving down a little road. 25 00:01:19,600 --> 00:01:22,030 That's kind of a, something that you do once or 26 00:01:22,030 --> 00:01:24,610 twice and then, pretty soon, you'll start reading the code. 27 00:01:24,610 --> 00:01:25,980 So I'm going to start on the right-hand 28 00:01:25,980 --> 00:01:28,770 side here, and just walk through the code. 29 00:01:28,770 --> 00:01:30,890 Remember, code operates in sequence. 30 00:01:30,890 --> 00:01:36,670 Well, there is a if, which is a special reserved word. 31 00:01:36,670 --> 00:01:39,920 It's one of those things that you can't, you can't name a variable if. 32 00:01:41,190 --> 00:01:45,483 And it is our indication that to Python, that the next 33 00:01:45,483 --> 00:01:50,018 statement that we're going to do may or may not be executed, if. 34 00:01:50,018 --> 00:01:52,874 And the thing that comes on the same line as the if, 35 00:01:52,874 --> 00:01:57,130 up to and including the, the little colon, is a question. 36 00:01:57,130 --> 00:01:58,940 This is a question. 37 00:01:58,940 --> 00:02:01,050 You're asking a question. 38 00:02:01,050 --> 00:02:05,360 So an assignment statement is moving a value into a variable. 39 00:02:05,360 --> 00:02:06,440 And a if statement 40 00:02:06,440 --> 00:02:07,826 is asking a question. 41 00:02:07,826 --> 00:02:10,280 In this case, we're asking a question about a variable. 42 00:02:10,280 --> 00:02:15,890 So always think, when you're, sort of, here, that this is a question to be asked. 43 00:02:15,890 --> 00:02:17,828 And, you'll notice when I'm doing the same 44 00:02:17,828 --> 00:02:19,889 thing over here, I put a question mark there. 45 00:02:19,889 --> 00:02:22,717 Is x less than 10? Yes or no? 46 00:02:22,717 --> 00:02:24,780 It's a question that has a yes or no. 47 00:02:24,780 --> 00:02:27,820 And so, the way this works is, this statement 48 00:02:27,820 --> 00:02:31,640 that's indented, after the if, is either executed or 49 00:02:31,640 --> 00:02:34,490 not executed based on the result of that question. 50 00:02:34,490 --> 00:02:38,110 So the way to sort of read this in English is set x to 5. 51 00:02:38,110 --> 00:02:43,880 If x is less than 10, which it is because x is 5, then we're going to execute this. 52 00:02:43,880 --> 00:02:45,710 So print Smaller comes out. 53 00:02:45,710 --> 00:02:48,626 And then we come back out and we continue and say, oh, okay, now 54 00:02:48,626 --> 00:02:52,068 I have another if statement, and then a bit of, a block of indented code. 55 00:02:52,068 --> 00:02:55,551 If x is less than 20, that's the question. 56 00:02:55,551 --> 00:02:56,766 The answer to that 57 00:02:56,766 --> 00:03:00,747 is no, and so it does not run that line, and so it runs Finis. 58 00:03:00,747 --> 00:03:04,823 So the printout of this program is Smaller, followed by Finis. 59 00:03:04,823 --> 00:03:11,115 What happens is, this line never executes because the answer 60 00:03:11,115 --> 00:03:16,863 to this question is false. Okay? So, let's go through that a little faster. 61 00:03:16,863 --> 00:03:20,581 Set x to five. If x is less than 10, print Smaller. 62 00:03:20,581 --> 00:03:21,895 Then, if x is greater 63 00:03:21,895 --> 00:03:25,843 than 20, which it's not, skip that, and then print Finis. 64 00:03:25,843 --> 00:03:28,160 That's the short version of it, okay? 65 00:03:28,160 --> 00:03:29,630 Conditional steps. 66 00:03:29,630 --> 00:03:32,040 This step is conditional, this step is conditional. 67 00:03:32,040 --> 00:03:36,180 They may or may not be executed based on the result of the question. 68 00:03:36,180 --> 00:03:38,850 Now, if we're thinking of this as like a GPS 69 00:03:38,850 --> 00:03:41,860 road map or something, we can look at this right-hand side. 70 00:03:41,860 --> 00:03:46,810 So, the CPU comes roaring down here, x equals 5, okay, I'll run that. 71 00:03:46,810 --> 00:03:52,240 Then it's faced with a choice. Do, is x less than 10, yes or no? 72 00:03:52,240 --> 00:03:55,700 If it is yes, and it is, I will go this way. 73 00:03:55,700 --> 00:03:57,630 If it was no, I would go that way. 74 00:03:57,630 --> 00:04:00,860 So if it's yes, I go here and I run this little thing and I print Smaller, great. 75 00:04:00,860 --> 00:04:01,951 And I follow the little road. 76 00:04:01,951 --> 00:04:05,890 And now the road takes me to here. And it's asking another question. 77 00:04:05,890 --> 00:04:07,550 Is x greater than 20? 78 00:04:07,550 --> 00:04:11,520 This time, the answer is no, so I'd come down here, right? 79 00:04:11,520 --> 00:04:12,180 And so, 80 00:04:12,180 --> 00:04:14,970 this bit of code is never executed. 81 00:04:14,970 --> 00:04:20,010 Now, this is a very simple example, but you get the basic idea. 82 00:04:20,010 --> 00:04:22,052 Okay? So that's conditional execution. 83 00:04:22,052 --> 00:04:26,665 Now there's a number of conditional operators that we 84 00:04:26,665 --> 00:04:30,130 want to use, just like we had multiplication, division. 85 00:04:30,130 --> 00:04:34,330 Some of them are pretty intuitive, and the others, you just kind of have 86 00:04:34,330 --> 00:04:37,240 to memorize, like less than and greater than make a lot of sense. 87 00:04:38,320 --> 00:04:42,040 The one that probably, that, easy, like less than or equal to, 88 00:04:42,040 --> 00:04:44,720 or greater than or equal to, those kind of make sense, too. 89 00:04:44,720 --> 00:04:47,288 They're less than or equal to. 90 00:04:47,288 --> 00:04:50,024 Just because we don't have a less than or equal to sign on a 91 00:04:50,024 --> 00:04:53,916 symbol or a greater than or equal sign, which we would use in mathematics. 92 00:04:53,916 --> 00:04:57,206 Equality, asking the question of whether something is 93 00:04:57,206 --> 00:05:00,213 equal to something else or not, is double equal. 94 00:05:00,213 --> 00:05:04,008 And that's because we're already using single equals as assignment. 95 00:05:04,008 --> 00:05:10,120 So when we say x equals 3, that is an assignment and sticks a value into x. 96 00:05:10,120 --> 00:05:12,320 This is the question. 97 00:05:12,320 --> 00:05:14,290 Is x equal to? 98 00:05:14,290 --> 00:05:15,760 If I was building a language, I would make 99 00:05:15,760 --> 00:05:18,800 it be equal question mark, or something like that. 100 00:05:18,800 --> 00:05:21,770 I'd be like, huh, is it equal? Kind of a question mark. 101 00:05:21,770 --> 00:05:23,390 But that's not what we do. 102 00:05:23,390 --> 00:05:26,280 I didn't invent this, so we're, double equals 103 00:05:26,280 --> 00:05:30,140 is the question, is something equal to another. 104 00:05:30,140 --> 00:05:35,260 Single equals changes something, x equals five changes x. 105 00:05:35,260 --> 00:05:40,250 Okay, and then, not equal, exclamation is commonly used to mean not in 106 00:05:40,250 --> 00:05:44,584 computer contexts, so if something is not equal to something, 107 00:05:44,584 --> 00:05:50,270 it is exclamation equal. Here are some examples. 108 00:05:50,270 --> 00:05:52,059 Just kind of running through them. 109 00:05:52,059 --> 00:05:55,209 They're all, they all turn out to be true, because I set x to 5. 110 00:05:55,209 --> 00:05:58,490 If x equals 5, print Equals 5. 111 00:05:58,490 --> 00:06:02,140 Come out here, if x is greater than 4, which is true, print Greater than 4. 112 00:06:02,140 --> 00:06:04,730 If x greater than or equal to 5, yep. 113 00:06:04,730 --> 00:06:07,590 If x less than 6, print Less than 6. 114 00:06:07,590 --> 00:06:11,580 Now here's a, there are two, sort of, syntaxes to, to the if statement. 115 00:06:11,580 --> 00:06:16,160 One is where the if statement is down here on a separate line and it's indented, 116 00:06:16,160 --> 00:06:20,050 and the other is where there's a single line and it's right on the same line, 117 00:06:20,050 --> 00:06:23,040 if x less than 6, print Less than 6. 118 00:06:23,040 --> 00:06:26,330 So this is true, so this whole thing executes. 119 00:06:26,330 --> 00:06:28,710 Then it continues down, if x less than or equal to 5? 120 00:06:28,710 --> 00:06:30,500 Yep, print Less than or Equal 5. 121 00:06:30,500 --> 00:06:35,220 If x is not equal to 6, which is true, cuz it's 5, then Not equal to 6. 122 00:06:35,220 --> 00:06:39,950 So, all those will turn out to be true, and all those will execute. 123 00:06:39,950 --> 00:06:45,660 And so, the, the tricky bit, here, is, you know, just knowing, 124 00:06:45,660 --> 00:06:50,430 seeing this syntax for an if statement, where it's all one line, and this syntax, 125 00:06:50,430 --> 00:06:53,410 where you end the first line with a colon and then indent the second line. 126 00:06:54,420 --> 00:06:55,710 This, you can only do one line. 127 00:06:55,710 --> 00:06:58,550 We will soon see that you can put more than one line in the indented block. 128 00:06:59,940 --> 00:07:02,395 Okay. 129 00:07:03,395 --> 00:07:05,405 Here we have more than one int line in 130 00:07:05,405 --> 00:07:08,735 the indented block, these are called one-way decisions. 131 00:07:08,735 --> 00:07:10,575 And so, we say x equals 5, 132 00:07:10,575 --> 00:07:13,723 we print out Before 5, so that prints out. 133 00:07:13,723 --> 00:07:19,520 If x equals 5, remember the double equals is the question mark version of equality, 134 00:07:19,520 --> 00:07:21,960 single equals assignment, it says yes. 135 00:07:21,960 --> 00:07:24,550 So we indent, and the convention is to indent four spaces, 136 00:07:24,550 --> 00:07:28,030 although it doesn't really matter as long as you're consistent. 137 00:07:28,030 --> 00:07:29,470 Then it's going to run all three of those. 138 00:07:29,470 --> 00:07:33,250 Is 5, Still 5, Third 5, these lines all come out. 139 00:07:33,250 --> 00:07:34,500 And then it comes out and prints, 140 00:07:35,510 --> 00:07:39,350 and the de-indenting, the fact that this print has been moved to line up with 141 00:07:39,350 --> 00:07:41,770 the if, that's what indicates that this little 142 00:07:41,770 --> 00:07:46,770 block of conditional executed code is finished. 143 00:07:46,770 --> 00:07:52,700 So then prints out Afterwards 5, comes down some more, Before 6, then it asks 144 00:07:52,700 --> 00:07:55,060 another question, if x is equal to 6, 145 00:07:55,060 --> 00:07:57,020 again, that's the question mark version of it. 146 00:07:58,100 --> 00:08:00,610 And if this is false, now, because x 147 00:08:00,610 --> 00:08:01,980 happens to be 5, so the answer 148 00:08:01,980 --> 00:08:05,990 to this expression, the logical expression, is false. 149 00:08:05,990 --> 00:08:10,730 Then it skips all of the indented bits, so none of this executes. 150 00:08:10,730 --> 00:08:13,960 So, since it's false, it skips all of the indented bit, but then it, 151 00:08:13,960 --> 00:08:18,060 this print lines up, and so then it picks back up with Afterwards 6. 152 00:08:18,060 --> 00:08:20,820 So we call this a one-way decision, where you have the question, and then 153 00:08:20,820 --> 00:08:24,180 you have a couple of things that you're going to do on this true, true thing. 154 00:08:24,180 --> 00:08:26,170 Or, if it turns out that you're false, 155 00:08:26,170 --> 00:08:27,590 you're going to skip all those things. 156 00:08:30,208 --> 00:08:33,530 So, Python is actually one of the 157 00:08:33,530 --> 00:08:38,420 few languages that uses indentation as syntactically significant. 158 00:08:39,750 --> 00:08:42,850 We like to indent code to, for ifs, and 159 00:08:42,850 --> 00:08:44,850 in a moment, we'll see you learn about loops. 160 00:08:44,850 --> 00:08:46,260 We like to indent code as a way to 161 00:08:47,480 --> 00:08:50,578 make sense of stuff, it makes it easier to read. 162 00:08:50,578 --> 00:08:54,586 You know, if this thing's inside, and so, it, it's really quite nice. 163 00:08:54,586 --> 00:08:55,198 And then, 164 00:08:55,198 --> 00:08:57,374 we, sort of, use it as a matching, to 165 00:08:57,374 --> 00:09:01,390 help us cognitively understand what's inside of a program. 166 00:09:02,750 --> 00:09:05,540 But in Python, it's really, really important, and it's 167 00:09:05,540 --> 00:09:08,100 almost, it's, it's, you have to think of, like, 168 00:09:08,100 --> 00:09:10,930 when you are moving in, you mean something, 169 00:09:10,930 --> 00:09:13,310 and when you move back out, you mean something. 170 00:09:13,310 --> 00:09:16,170 So you can increase the indent, which you do after, like, 171 00:09:16,170 --> 00:09:18,400 an if statement, or any other statement that ends in a colon. 172 00:09:18,400 --> 00:09:20,530 You increase the indent, and then 173 00:09:20,530 --> 00:09:22,730 when you're done, you decrease the indent. 174 00:09:22,730 --> 00:09:25,140 You maintain the indent, sort of, for sequential code. 175 00:09:26,300 --> 00:09:28,670 Now blank lines and comments are ignored. 176 00:09:28,670 --> 00:09:31,000 So you can have a blank line and it, it, the 177 00:09:31,000 --> 00:09:34,470 indentation just goes right past it and the comments don't affect it. 178 00:09:34,470 --> 00:09:41,639 And so, while we're here, we'll interrupt us for a recommendation. 179 00:09:44,150 --> 00:09:50,030 In your text editor, Notepad Plus or Text Edit or TextWrangler, or whatever 180 00:09:50,030 --> 00:09:55,420 you're using, it may be set, when you hit the tab key, to move in four spaces. 181 00:09:56,720 --> 00:10:00,150 Sometimes you also might move in four spaces by hitting space bar four times. 182 00:10:01,170 --> 00:10:03,570 Python will see that as different. 183 00:10:03,570 --> 00:10:08,910 And it is possible in all of these word processors to say, hey, 184 00:10:08,910 --> 00:10:13,730 don't actually put tabs in my document, when I hit the tab, put in four spaces. 185 00:10:13,730 --> 00:10:17,539 Then, whether you're hitting the space bar or hitting the tab, at least you 186 00:10:17,539 --> 00:10:18,770 are putting the same thing into your 187 00:10:18,770 --> 00:10:21,330 document and don't, not freaking Python out. 188 00:10:22,750 --> 00:10:25,560 If you don't, you may get indentation errors. 189 00:10:25,560 --> 00:10:28,575 Indentation errors are syntax errors to Python. 190 00:10:28,575 --> 00:10:32,070 And what's really frustrating is, if you, it looks good 191 00:10:32,070 --> 00:10:33,810 to you in your text editor, you have an if, 192 00:10:33,810 --> 00:10:36,750 and the block goes in, and comes back out, but one of them is 193 00:10:36,750 --> 00:10:40,600 four spaces and one of them is a tab, then Python will yell at you. 194 00:10:40,600 --> 00:10:43,550 And this is really frustrating, when Python yells at you about that. 195 00:10:45,140 --> 00:10:49,290 So what I'd like you to do is go into your text editor, whatever it is, 196 00:10:50,385 --> 00:10:53,510 into the properties or the settings. 197 00:10:53,510 --> 00:10:59,480 And here is, you know, your, yours may be different, but here is where you set this. 198 00:11:01,640 --> 00:11:06,390 Auto expand tabs, that is on the Mac in TextWrangler, and then, 199 00:11:06,390 --> 00:11:08,730 in Notepad Plus Plus, there is replace 200 00:11:08,730 --> 00:11:11,270 tabs as spaces, and that's underneath preferences. 201 00:11:11,270 --> 00:11:12,710 So you have to find it. 202 00:11:12,710 --> 00:11:17,280 Stop right now, and go set this so you're not going to make yourself crazy. 203 00:11:18,840 --> 00:11:21,620 Okay, so, this is kind of a busy slide, but it gives 204 00:11:21,620 --> 00:11:27,983 you the sense that you have to explicitly think about indenting and de-indenting. 205 00:11:27,983 --> 00:11:29,814 Okay? And so I'm just going to walk through this. 206 00:11:29,814 --> 00:11:33,047 So, when you have two lines lining up 207 00:11:33,047 --> 00:11:36,300 that means they're going to run sequentially. 208 00:11:36,300 --> 00:11:39,590 If you see an if, or later here, we'll see a for. 209 00:11:39,590 --> 00:11:43,230 We haven't talked about for yet, but it's, it's like if. 210 00:11:43,230 --> 00:11:45,830 So, the fact that we go from this second line 211 00:11:45,830 --> 00:11:48,360 to this third line and move the indent in, we're actually 212 00:11:48,360 --> 00:11:51,160 creating a block that has to do with this if, 213 00:11:51,160 --> 00:11:53,350 and it, you can also kind of tell these, the if and 214 00:11:53,350 --> 00:11:56,000 the for end in a colon character. 215 00:11:56,000 --> 00:11:59,200 Now, we could pull this print back out, but we want 216 00:11:59,200 --> 00:12:02,470 it to be part of the if, so we maintain the indent. 217 00:12:02,470 --> 00:12:05,260 And then we're done with the if by pulling out. 218 00:12:05,260 --> 00:12:09,790 So we line the p with the i, and that means this is outside of the if. 219 00:12:11,110 --> 00:12:14,750 This for, which we haven't learned about for yet, for is another 220 00:12:14,750 --> 00:12:19,010 statement that ends in a colon, and afterwards you have to indent. 221 00:12:19,010 --> 00:12:22,230 Then you maintain the indent. Here's an if. 222 00:12:22,230 --> 00:12:25,080 But now we have an if, and we're already in, 223 00:12:25,080 --> 00:12:27,260 but that ends in a colon, so we go in farther. 224 00:12:28,580 --> 00:12:29,940 And now this is the block. 225 00:12:29,940 --> 00:12:35,660 Now, we come back out, and we line up with that if, right there, okay? 226 00:12:35,660 --> 00:12:39,220 And now, at the end of this, this indent, this x here 227 00:12:39,220 --> 00:12:42,610 comes all the way back out, so it lines up. 228 00:12:42,610 --> 00:12:44,820 The rest of these are kind of weird in that 229 00:12:44,820 --> 00:12:48,340 comments don't matter, blank lines don't matter. 230 00:12:48,340 --> 00:12:50,730 And so, it just is, sort of, you have to 231 00:12:50,730 --> 00:12:54,670 get, mentally get used to the notion that these don't count. 232 00:12:54,670 --> 00:12:56,686 They can really cognitively mess you up. 233 00:12:56,686 --> 00:12:59,590 So these don't count. 234 00:12:59,590 --> 00:13:01,310 And now, if I look through it, without, 235 00:13:01,310 --> 00:13:04,190 with the comments hidden, it starts in column one. 236 00:13:05,420 --> 00:13:09,530 Ignore, ignore, goes in, stays in, ignore, ignore, 237 00:13:09,530 --> 00:13:14,230 ignore, comes out. So that's, it all makes sense. 238 00:13:14,230 --> 00:13:17,720 Those comments and blank lines are just, kind of, confusion. 239 00:13:19,370 --> 00:13:23,970 So, increasing and decreasing indent has meaning in Python. 240 00:13:23,970 --> 00:13:26,380 We'll learn more about this in a bit. 241 00:13:26,380 --> 00:13:29,430 Our programs won't get this complex right away, but it's important to 242 00:13:29,430 --> 00:13:32,420 think, these indents aren't just pretty; 243 00:13:32,420 --> 00:13:35,220 they actually are communicating something to Python. 244 00:13:35,220 --> 00:13:38,239 And what they're communicating is, basically, what's in a block. 245 00:13:39,470 --> 00:13:41,210 And it shouldn't take you very long, when you 246 00:13:41,210 --> 00:13:45,340 start looking at Python, to sort of visualize these blocks. 247 00:13:45,340 --> 00:13:47,710 So, here, here's a big block. 248 00:13:47,710 --> 00:13:50,670 This block here, that's got these three things. 249 00:13:50,670 --> 00:13:52,460 And then, this is a block as well, and 250 00:13:52,460 --> 00:13:54,740 you can kind of say, well, here's an if statement. 251 00:13:54,740 --> 00:13:57,410 And then these are the two statements that are part of that if statement. 252 00:13:57,410 --> 00:14:00,930 So mentally, you kind of make these block pictures. 253 00:14:00,930 --> 00:14:03,700 So here's another block. This is that for loop. 254 00:14:03,700 --> 00:14:06,330 This part's the indented part, but then there's a block inside of the block. 255 00:14:06,330 --> 00:14:08,550 So you gotta kind of start seeing that as well. 256 00:14:08,550 --> 00:14:11,100 So this is a block that has to do with this green block 257 00:14:11,100 --> 00:14:14,550 is the, the one that has to do with the if. 258 00:14:15,580 --> 00:14:17,700 And then there's a block here, and then this is 259 00:14:17,700 --> 00:14:20,397 a great big block because this is where it finally de-indents. 260 00:14:20,397 --> 00:14:23,133 So, don't worry about it yet, but at some 261 00:14:23,133 --> 00:14:26,781 point you're just going to start seeing this indenting and de-indenting 262 00:14:26,781 --> 00:14:31,273 as defining blocks of code for various purposes. 263 00:14:31,273 --> 00:14:34,610 Now we don't have all the purposes yet, but we'll get there.