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