1 00:00:00,570 --> 00:00:06,080 Okay, so that's sentences, now paragraphs, let's talk about paragraphs. 2 00:00:06,080 --> 00:00:08,790 Paragraphs are the combination of sentences to make 3 00:00:08,790 --> 00:00:11,885 sort of a thought together. Multiple sentences, multiple lines. 4 00:00:11,885 --> 00:00:16,920 So, the interactive Python that I just showed you 5 00:00:16,920 --> 00:00:22,170 is fine for running one, two, or five or six commands. 6 00:00:22,170 --> 00:00:26,700 But ultimately, we're going to write much longer bits of Python. 7 00:00:26,700 --> 00:00:30,830 And so we write what's called a Python script, or a Python program. 8 00:00:30,830 --> 00:00:33,220 And we put these in a file. 9 00:00:33,220 --> 00:00:35,950 And we, and if you went through the 10 00:00:35,950 --> 00:00:39,460 prerequisite, you will see have seen me edit in a text 11 00:00:39,460 --> 00:00:43,210 editor, saved a file, and then run from the Python file. 12 00:00:44,220 --> 00:00:45,090 Okay? 13 00:00:45,090 --> 00:00:47,960 And so we call these files put .py on the end of them, P-Y on the end of them. 14 00:00:49,020 --> 00:00:52,220 And we're giving Python a script to execute. 15 00:00:54,040 --> 00:00:56,902 [COUGH] So, interactive, you're typing directly into Python 16 00:00:56,902 --> 00:00:59,360 and it's doing it right as you're talking. 17 00:00:59,360 --> 00:01:02,550 You're still doing it in order, and the order does matter. 18 00:01:02,550 --> 00:01:06,220 In a script you type it all into a file once and say Python, do it all. 19 00:01:11,700 --> 00:01:13,200 [SOUND] Now when you write one of 20 00:01:13,200 --> 00:01:16,670 these things there are patterns for combining these. 21 00:01:16,670 --> 00:01:21,870 There are things that we do to these lines that sort of treat them differently. 22 00:01:21,870 --> 00:01:24,990 Is like a recipe, a set of instructions. 23 00:01:24,990 --> 00:01:25,785 Start at the beginning. 24 00:01:25,785 --> 00:01:29,300 [SOUND] But it's a little more complex than that. 25 00:01:29,300 --> 00:01:31,460 Some steps are just sequential. 26 00:01:31,460 --> 00:01:33,560 Some steps might be skipped. 27 00:01:33,560 --> 00:01:35,800 Some steps we do multiple times. 28 00:01:35,800 --> 00:01:39,600 And other times we have kind of like a set of steps we do over and over again. 29 00:01:40,680 --> 00:01:42,730 So here's some pictures. 30 00:01:42,730 --> 00:01:47,040 And here's the four lines of Python, a little simple paragraph. 31 00:01:48,060 --> 00:01:51,100 And it's got a sentence that says, x equals 2. 32 00:01:51,100 --> 00:01:52,750 Print x. 33 00:01:52,750 --> 00:01:56,390 x equals x plus 2, which says, go grab the old value of x, add 2 to it. 34 00:01:56,390 --> 00:01:57,850 Stick it back in x. 35 00:01:57,850 --> 00:01:58,380 And print x. 36 00:01:58,380 --> 00:02:02,280 The output of this program is 2, then 4. 37 00:02:02,280 --> 00:02:04,480 because x was 2, we printed it, and we added 2 38 00:02:04,480 --> 00:02:07,570 to it, and then we printed it again so it was 4. 39 00:02:07,570 --> 00:02:10,400 Now, these flowcharts, don't worry, I'm not going to make you draw these. 40 00:02:10,400 --> 00:02:12,760 I just draw these in case, cognitively, it makes 41 00:02:12,760 --> 00:02:14,906 it easier for you to understand what's going on. 42 00:02:14,906 --> 00:02:18,750 So, x = 1. 43 00:02:18,750 --> 00:02:19,630 is the first step. 44 00:02:19,630 --> 00:02:21,140 Sequentially, it just continues on. 45 00:02:21,140 --> 00:02:22,520 It runs the print. 46 00:02:22,520 --> 00:02:24,040 x equals x plus 1. 47 00:02:24,040 --> 00:02:24,740 Runs the print. 48 00:02:24,740 --> 00:02:26,150 So this is just straight through. 49 00:02:26,150 --> 00:02:29,520 It'll make more sense when we see a little more convoluted things. 50 00:02:29,520 --> 00:02:32,100 So, this program just starts naturally. 51 00:02:32,100 --> 00:02:35,890 Python starts at the beginning and works its way down through the end. 52 00:02:35,890 --> 00:02:37,540 That's sequential stuff. 53 00:02:37,540 --> 00:02:39,790 That's the normal order of business. 54 00:02:39,790 --> 00:02:44,620 Now, a conditional is a step that may or may not get executed. 55 00:02:44,620 --> 00:02:48,370 If all we did was sequential steps, programs would be kind of dull. 56 00:02:48,370 --> 00:02:52,070 Right, they would just be like blah, blah, blah, blah, stop. 57 00:02:52,070 --> 00:02:54,710 So, there's things like oh what if you do this or 58 00:02:54,710 --> 00:02:57,520 what if you do that and so we do things like if. 59 00:02:57,520 --> 00:02:59,460 If you have more than 40 hours, I'm going to pay 60 00:02:59,460 --> 00:03:02,900 you a different rate than if I have under 40 hours. 61 00:03:02,900 --> 00:03:05,320 Those kinds of things are if, the word if. 62 00:03:06,660 --> 00:03:10,720 So in Python, the way we express this is we use the keyword if. 63 00:03:10,720 --> 00:03:15,510 So we say x equals 5, and then we say if x is less than 10. 64 00:03:15,510 --> 00:03:20,780 This is a question that's being asked, is x less than 10 or not, yes or no? 65 00:03:20,780 --> 00:03:24,060 If it is, we execute the indented bit. 66 00:03:24,060 --> 00:03:25,400 If it's not, we skip it. 67 00:03:26,860 --> 00:03:31,550 This case, since x is 5, we execute it. 68 00:03:31,550 --> 00:03:34,250 Then we come back here, and we're going to do another one. 69 00:03:34,250 --> 00:03:37,550 If x is greater than 20, well, this turns out to be false. 70 00:03:39,760 --> 00:03:41,530 So we skip that. 71 00:03:41,530 --> 00:03:43,230 So Bigger does not run. 72 00:03:43,230 --> 00:03:44,560 That line never runs. 73 00:03:44,560 --> 00:03:46,930 So, we, the output is Smaller, Finis. 74 00:03:48,990 --> 00:03:52,790 Now, here, we can take a look at it, sort of, in the picture diagram. 75 00:03:52,790 --> 00:03:54,970 We run x equals 5. 76 00:03:54,970 --> 00:03:59,840 We ask a question, this doesn't hurt x to ask the question, is x less than 10? 77 00:03:59,840 --> 00:04:02,720 The answer is yes so we kind of drive down this little path. 78 00:04:02,720 --> 00:04:05,410 We print Smaller and then we rejoin the freeway. 79 00:04:06,670 --> 00:04:11,220 Is x less than 20? No, so we skip and we continue. 80 00:04:12,250 --> 00:04:14,520 So, this never gets executed. 81 00:04:14,520 --> 00:04:16,220 So you can think of it either way. 82 00:04:16,220 --> 00:04:17,960 You can think of either sort of like 83 00:04:17,960 --> 00:04:22,020 Gestalt, say, if this true, do what's indented. 84 00:04:22,020 --> 00:04:23,560 Or you can imagine sort of a little car 85 00:04:23,560 --> 00:04:27,410 driving down a highway making turn choices as it goes. 86 00:04:27,410 --> 00:04:30,890 They're equivalent. Over time it's probably you'll just 87 00:04:30,890 --> 00:04:33,310 start seeing this and start thinking this way. 88 00:04:33,310 --> 00:04:36,400 But sometimes it helps to think about it this way, for a little while. 89 00:04:38,710 --> 00:04:39,950 Okay. 90 00:04:39,950 --> 00:04:42,280 Now, the next thing I want to show you is 91 00:04:42,280 --> 00:04:46,630 repeated steps, steps that happen over and over and over again. 92 00:04:46,630 --> 00:04:46,960 Okay? 93 00:04:46,960 --> 00:04:51,700 And that again, when I said oh, computers are good at handling a billion words. 94 00:04:51,700 --> 00:04:55,760 Well that means it has to have a loop or a repeated section, one for each word. 95 00:04:55,760 --> 00:04:57,980 It's going to do something for each word. 96 00:04:57,980 --> 00:04:58,570 And so, 97 00:05:00,830 --> 00:05:01,640 so here we go. 98 00:05:01,640 --> 00:05:06,000 And in Python, let's pick a different festive color. 99 00:05:06,000 --> 00:05:08,830 Let's pick purple as a festive color. 100 00:05:08,830 --> 00:05:12,280 So here's our program, starts at the beginning, sets the variable n to 5. 101 00:05:12,280 --> 00:05:16,550 And then a key word, reserved word while, while 102 00:05:16,550 --> 00:05:20,210 n greater than 0, again this is asking a question. 103 00:05:20,210 --> 00:05:22,670 This is asking a question. 104 00:05:22,670 --> 00:05:24,080 Is n greater than 0? 105 00:05:24,080 --> 00:05:25,130 That's a question. 106 00:05:25,130 --> 00:05:27,870 If yes we're going to do this, if no we're going to do that. 107 00:05:27,870 --> 00:05:30,170 Over here if it's true we're going to execute the 108 00:05:30,170 --> 00:05:33,630 indented part, and then come back and do it again. 109 00:05:33,630 --> 00:05:36,120 If it's false we're going to skip down. 110 00:05:36,120 --> 00:05:38,660 So it's kind of like an if, except it 111 00:05:38,660 --> 00:05:42,370 keeps doing it over and over and over again. 112 00:05:42,370 --> 00:05:44,640 So, it comes in, sets n to 5. 113 00:05:44,640 --> 00:05:45,830 Is n greater than 0? 114 00:05:45,830 --> 00:05:46,850 Yeah, sure. 115 00:05:46,850 --> 00:05:49,460 So we print n, out comes 5. 116 00:05:49,460 --> 00:05:52,580 Then it says, and n equals n minus 1, so n becomes 4. 117 00:05:52,580 --> 00:05:55,850 We can change colors. 118 00:05:55,850 --> 00:05:57,250 Then it goes back up. 119 00:05:58,920 --> 00:06:01,948 Goes back up and asks the question again. 120 00:06:01,948 --> 00:06:04,100 n is 4. 121 00:06:04,100 --> 00:06:07,170 It's still greater than 0, so it comes through. 122 00:06:07,170 --> 00:06:09,090 Prints out 4, subtracts 1. 123 00:06:09,090 --> 00:06:11,230 So n is now 3. 124 00:06:11,230 --> 00:06:13,630 Goes back up. 125 00:06:13,630 --> 00:06:14,855 Is n 0? 126 00:06:14,855 --> 00:06:16,660 Is n greater than 0? 127 00:06:16,660 --> 00:06:17,930 Yes, it is. 128 00:06:17,930 --> 00:06:19,470 Print out 3. 129 00:06:19,470 --> 00:06:19,940 Subtract 1. 130 00:06:19,940 --> 00:06:20,530 Now it's 2. 131 00:06:20,530 --> 00:06:23,513 So out come 3 and 2. 132 00:06:25,060 --> 00:06:28,130 Then it goes back up, still greater than 0. 133 00:06:28,130 --> 00:06:29,220 Yes, it is. 134 00:06:29,220 --> 00:06:30,410 Print out 2. 135 00:06:30,410 --> 00:06:31,710 Or, wait. 136 00:06:31,710 --> 00:06:32,568 Now it's 1. 137 00:06:32,568 --> 00:06:37,140 [COUGH] Now we subtract 1, it becomes 0. 138 00:06:37,140 --> 00:06:39,070 Is it greater than 0? 139 00:06:39,070 --> 00:06:40,580 No, and we finally leave. 140 00:06:42,280 --> 00:06:44,370 And we finally drop down. 141 00:06:44,370 --> 00:06:46,480 And so the last thing that comes out is the printed Blastoff. 142 00:06:48,170 --> 00:06:50,420 So this is a loop, the notion that we're 143 00:06:50,420 --> 00:06:53,585 going to run this little bit of code five times. 144 00:06:53,585 --> 00:06:55,770 [COUGH] 145 00:06:55,770 --> 00:06:56,270 Excuse me. 146 00:06:59,890 --> 00:07:02,000 We're going to run this little bit of code five times, 147 00:07:03,040 --> 00:07:06,750 And loops have these things we call iteration variables. 148 00:07:06,750 --> 00:07:08,480 And that is this n. 149 00:07:08,480 --> 00:07:11,040 It's a variable that specifically is changing 150 00:07:11,040 --> 00:07:12,870 each time it goes through the loop. 151 00:07:12,870 --> 00:07:15,010 And that way, we can sort of control the loop. 152 00:07:15,010 --> 00:07:17,340 We can decide when it starts and when it stops. 153 00:07:17,340 --> 00:07:18,730 We can tell if we're at the beginning or at 154 00:07:18,730 --> 00:07:20,710 the end, or the first one or the last one. 155 00:07:20,710 --> 00:07:22,370 We'll do a lot of stuff with loops. 156 00:07:22,370 --> 00:07:24,160 This is an iteration variable, because 157 00:07:24,160 --> 00:07:27,146 we iterate repeatedly iterate through the loop. 158 00:07:27,146 --> 00:07:28,250 Okay? 159 00:07:28,250 --> 00:07:32,654 Any questions? 160 00:07:32,654 --> 00:07:34,760 We can't do questions. 161 00:07:34,760 --> 00:07:39,210 Okay, so now, if we go back to the little 162 00:07:39,210 --> 00:07:42,830 story that I, you've got several chapters to understand, don't worry. 163 00:07:42,830 --> 00:07:45,030 You actually got like through chapter nine, so 164 00:07:45,030 --> 00:07:47,040 don't try to understand this program right now. 165 00:07:47,040 --> 00:07:49,410 I'm just trying to give you a sense of 166 00:07:49,410 --> 00:07:51,940 what the picture is going to be, right? 167 00:07:51,940 --> 00:07:58,540 So, so here are some sequential statements, because they aren't indented. 168 00:07:58,540 --> 00:08:00,990 Those five lines are sequential. 169 00:08:00,990 --> 00:08:03,120 They just go one after the other. 170 00:08:03,120 --> 00:08:06,910 Then we have for, and it's indented. 171 00:08:06,910 --> 00:08:08,050 This is a loop. 172 00:08:08,050 --> 00:08:10,380 This is going to run a bunch of times. 173 00:08:10,380 --> 00:08:11,120 Then we're done with that. 174 00:08:11,120 --> 00:08:13,420 We do some more sequential stuff. 175 00:08:13,420 --> 00:08:16,120 Now, we have a for loop, and that's going to run a bunch of times. 176 00:08:17,390 --> 00:08:21,060 And then we have an if, which may or may not run. 177 00:08:21,060 --> 00:08:22,570 So these little, this little block of 178 00:08:22,570 --> 00:08:25,760 code is conditionally executed based on something. 179 00:08:25,760 --> 00:08:27,570 And here is the question that we're asking. 180 00:08:28,680 --> 00:08:29,700 So that's the question. 181 00:08:30,860 --> 00:08:32,250 And then at the end, we do a print. 182 00:08:32,250 --> 00:08:35,340 Now, again, don't try to make too much sense of this. 183 00:08:35,340 --> 00:08:42,758 I'm just trying to show you sequential, repeated, repeated, conditional. 184 00:08:42,758 --> 00:08:43,499 Okay? 185 00:08:43,499 --> 00:08:48,110 Just, those concepts show up in every, pretty much every program that we build. 186 00:08:49,270 --> 00:08:49,770 Okay.