1 00:00:01,000 --> 00:00:01,870 Hello. 2 00:00:01,870 --> 00:00:04,080 And welcome to Python for Informatics. 3 00:00:04,080 --> 00:00:05,630 Right now we're going to cover Chapter One. 4 00:00:05,630 --> 00:00:07,650 I'm Charles Severance from the University of Michigan. 5 00:00:07,650 --> 00:00:09,190 And I'm the author. 6 00:00:09,190 --> 00:00:13,590 And I'll be your lecturer, for this online lecture of the first chapter of the book. 7 00:00:15,570 --> 00:00:19,910 This lecture and my slides, and the book, as a matter of fact, are all open. 8 00:00:19,910 --> 00:00:21,930 Open content, open materials. 9 00:00:23,380 --> 00:00:25,520 They're copyright Creative Commons Attribution. 10 00:00:25,520 --> 00:00:29,510 And this video recording is also copyright Creative Commons Attribution. 11 00:00:29,510 --> 00:00:31,440 It's important to be explicit about copyright, and 12 00:00:31,440 --> 00:00:33,630 so I say it right at the beginning. 13 00:00:35,690 --> 00:00:39,580 So, computers basically want to be helpful. 14 00:00:39,580 --> 00:00:41,255 They are programmed. 15 00:00:41,255 --> 00:00:44,940 [SOUND] Matter of fact, this is a microprocessor. 16 00:00:44,940 --> 00:00:47,260 This is really just an electrical part. 17 00:00:47,260 --> 00:00:50,280 It's got wires and circuits inside of it. 18 00:00:50,280 --> 00:00:55,190 And somebody spent a lot of engineering time, to make it so that these pins in the 19 00:00:55,190 --> 00:01:00,580 back take instructions from us, from operating systems, 20 00:01:00,580 --> 00:01:02,960 from the hard drive, from the memory. 21 00:01:02,960 --> 00:01:05,330 Instructions come into here and then results come out. 22 00:01:05,330 --> 00:01:07,710 It's really sort of a very programmable hand 23 00:01:07,710 --> 00:01:10,690 calculator and it's our job to put instructions in. 24 00:01:10,690 --> 00:01:13,950 This thing, in a sense, is wired to 25 00:01:13,950 --> 00:01:17,570 be curious about what's next, right? It's like 26 00:01:18,900 --> 00:01:21,260 It, it's, like, tell me what you want to do next. 27 00:01:21,260 --> 00:01:22,170 What do you want to do next? 28 00:01:22,170 --> 00:01:24,510 What do you want to do next, and after that, what do you want to do next, 29 00:01:24,510 --> 00:01:27,950 and it just happens to do that a billion or so times a second. 30 00:01:27,950 --> 00:01:32,120 And, so that's sort of the, the low-level piece, and, but you can also think, if 31 00:01:32,120 --> 00:01:35,350 you have, like, a PDA, something like this, 32 00:01:35,350 --> 00:01:38,690 all the buttons on here are some kind of, 33 00:01:38,690 --> 00:01:40,580 you know, "what's next?" 34 00:01:40,580 --> 00:01:41,290 Right? 35 00:01:41,290 --> 00:01:43,090 Each of those is sort of something begging 36 00:01:43,090 --> 00:01:46,620 for my attention, some application developer who's built a 37 00:01:46,620 --> 00:01:49,940 really cool application and says please use me, please 38 00:01:49,940 --> 00:01:52,510 click me, I am sort of nothing without you. 39 00:01:52,510 --> 00:01:56,460 We humans are the things that sort of cause computers to start doing 40 00:01:56,460 --> 00:02:00,976 something and this will sit here happily until I caused it to do something. 41 00:02:00,976 --> 00:02:04,370 Now, [SOUND] whoa! 42 00:02:04,370 --> 00:02:05,220 Whoa. 43 00:02:05,220 --> 00:02:06,950 Hope it's still okay. 44 00:02:06,950 --> 00:02:08,480 Yeah, it seems to be fine. 45 00:02:08,480 --> 00:02:09,420 Seems to be fine. 46 00:02:09,420 --> 00:02:11,080 Takes a lickin', and keeps on tickin'. 47 00:02:11,080 --> 00:02:14,790 So, these, anyone can use, right? 48 00:02:14,790 --> 00:02:18,800 They say even animals can use a Macintosh, smartphone. 49 00:02:18,800 --> 00:02:21,070 And so you don't have to be a programmer. 50 00:02:21,070 --> 00:02:24,580 But to get this to do what you want 51 00:02:24,580 --> 00:02:27,030 you need to learn a different language, and we need to 52 00:02:27,030 --> 00:02:30,940 learn the language of the instructions to tell it what to do. 53 00:02:30,940 --> 00:02:32,410 So, that's what we're going to do. 54 00:02:32,410 --> 00:02:34,420 We're going to learn how to talk to this. 55 00:02:34,420 --> 00:02:35,420 Yo! 56 00:02:35,420 --> 00:02:38,320 because it's asking us a question, we have to give the answer. 57 00:02:40,870 --> 00:02:42,770 So, what's a programmer? 58 00:02:42,770 --> 00:02:44,490 A programmer is somebody who 59 00:02:44,490 --> 00:02:49,470 writes a program, which is a script or a set of instructions that tell 60 00:02:49,470 --> 00:02:53,810 one of these kinds of things what it is that they're supposed to do. 61 00:02:53,810 --> 00:02:57,870 And sometimes you're running a program like Moodle, an open source 62 00:02:57,870 --> 00:02:59,550 learning management system, or Sakai, 63 00:02:59,550 --> 00:03:02,010 another open source learning management system. 64 00:03:02,010 --> 00:03:04,330 And sometimes you'll even get paid to do that, right? 65 00:03:04,330 --> 00:03:05,800 Sometimes you do it for free, sometimes you 66 00:03:05,800 --> 00:03:08,500 get paid, sometimes you write things for yourself. 67 00:03:10,050 --> 00:03:12,900 And and but, if you think about it, all these 68 00:03:12,900 --> 00:03:16,690 applications on my iPhone, somebody's making some money off of these. 69 00:03:16,690 --> 00:03:19,430 They may not be able to quit their job, but a surprising number 70 00:03:19,430 --> 00:03:20,990 have been able to quit their job or start 71 00:03:20,990 --> 00:03:24,170 small companies, maybe not gigantic companies, but small companies. 72 00:03:26,380 --> 00:03:29,840 So these people that can put applications inside of our computers 73 00:03:29,840 --> 00:03:35,160 are programmers, because they understand the way that we talk to these computers. 74 00:03:35,160 --> 00:03:38,920 And part of what I'm going to try to do is to get you to move from 75 00:03:38,920 --> 00:03:43,850 the mindset of the end user who thinks of this as something just to click on 76 00:03:43,850 --> 00:03:46,500 to the mindset of the programmer, who's kind of 77 00:03:46,500 --> 00:03:49,680 on the inside trying to get out to you. 78 00:03:49,680 --> 00:03:51,870 So that's, as we sort of move from user to 79 00:03:51,870 --> 00:03:55,090 programmer, we move from outside to inside and we think 80 00:03:55,090 --> 00:03:56,420 of the world out there and it's like what are 81 00:03:56,420 --> 00:03:59,100 they going to put, push, what button are they going to push. 82 00:03:59,100 --> 00:04:00,450 So here's kind of a picture of that. 83 00:04:01,800 --> 00:04:04,550 So on the outside, we're users, we click on buttons, we click 84 00:04:04,550 --> 00:04:08,600 on websites, we click on buttons on our phones, et cetera, et cetera. 85 00:04:08,600 --> 00:04:12,980 But what's really going on inside all of that is there's a computer with 86 00:04:12,980 --> 00:04:17,810 a bunch of hardware inside of that, and it has inside of it data, 87 00:04:17,810 --> 00:04:24,490 networks, other information. And software is what makes all that make sense. 88 00:04:25,660 --> 00:04:28,550 And so, part of what I want you to do is I want you to stop 89 00:04:28,550 --> 00:04:30,420 thinking about how to use these things from 90 00:04:30,420 --> 00:04:34,380 the outside, and we move to becoming a programmer. 91 00:04:34,380 --> 00:04:35,610 We're someone on the inside. 92 00:04:35,610 --> 00:04:39,410 We're with the CPU, we are with the memory. 93 00:04:39,410 --> 00:04:41,370 We are with the network connection. 94 00:04:41,370 --> 00:04:45,600 We are doing things on behalf of the user, and presenting them back up to the user. 95 00:04:47,870 --> 00:04:49,580 So, why be a programmer? 96 00:04:49,580 --> 00:04:52,850 Now, this class is specifically not trying to turn you 97 00:04:52,850 --> 00:04:55,650 into a professional programmer, even though I'd be very proud 98 00:04:55,650 --> 00:04:58,180 if after five, ten more classes you were a 99 00:04:58,180 --> 00:05:01,530 professional programmer. But that's not the purpose of this class. 100 00:05:01,530 --> 00:05:03,320 Sometimes you just want to get something done. 101 00:05:03,320 --> 00:05:06,860 You got an Excel spreadsheet at work and the data's not right. 102 00:05:06,860 --> 00:05:08,750 You got the data from somebody else and it's 103 00:05:08,750 --> 00:05:12,240 got like extra spaces where it shouldn't have it or 104 00:05:12,240 --> 00:05:17,120 does missing fields or something, you gotta do something to it that Excel can't do. 105 00:05:17,120 --> 00:05:18,640 And you're, you're stuck, like, saying, Aw, I 106 00:05:18,640 --> 00:05:20,920 want to, I want to mess with this data and 107 00:05:20,920 --> 00:05:23,620 put it in Excel so I can do my job, but it's a pain in the neck. 108 00:05:23,620 --> 00:05:26,960 And I have to sit and bring it into a text editor like Microsoft Word 109 00:05:26,960 --> 00:05:30,530 and go line by line and make all kinds of mistakes and clean the data up. 110 00:05:30,530 --> 00:05:31,960 You can write a program to do that, 111 00:05:31,960 --> 00:05:33,320 and that's the kind of programs we're going to do. 112 00:05:33,320 --> 00:05:37,810 Programs that serve our needs inside the computer, but to serve our needs. 113 00:05:37,810 --> 00:05:42,690 Professional programmers tend to build things for other people to use, right? 114 00:05:42,690 --> 00:05:44,650 They, they tend to build things that everyone else does. 115 00:05:44,650 --> 00:05:49,480 But we're going to build stuff primarily for ourselves. 116 00:05:49,480 --> 00:05:50,720 So. 117 00:05:50,720 --> 00:05:51,820 What is code? 118 00:05:51,820 --> 00:05:52,570 What is software? 119 00:05:52,570 --> 00:05:55,660 We use these words pretty much independently, a program. 120 00:05:55,660 --> 00:05:58,530 It's really a sequence of stored instructions. 121 00:05:58,530 --> 00:06:01,790 We learn the language that this talks, and then 122 00:06:01,790 --> 00:06:05,110 we will feed the instructions in, one at a time. 123 00:06:05,110 --> 00:06:08,720 It takes them one at a time, it gives us back a result, we give it the next one. 124 00:06:08,720 --> 00:06:11,020 It gives it back, in, out, in, out. 125 00:06:11,020 --> 00:06:13,030 So it's really a sequence of stored instructions. 126 00:06:13,030 --> 00:06:15,890 But it's kind of more than that. 127 00:06:15,890 --> 00:06:19,290 It's, it's sort of like our creativity, and if you've been using some of 128 00:06:19,290 --> 00:06:21,040 my software, like my MOOC software, I 129 00:06:21,040 --> 00:06:23,980 spent about a month writing all that stuff. 130 00:06:23,980 --> 00:06:25,700 And it's like, it's me. 131 00:06:25,700 --> 00:06:29,070 I mean I'm, it's my vision of how cool stuff ought to work, right? 132 00:06:29,070 --> 00:06:32,380 And so it's more than just getting something done. 133 00:06:32,380 --> 00:06:34,930 It's also a sense of pride and a sense of accomplishment. 134 00:06:34,930 --> 00:06:37,580 Especially if you're giving something that other people can make use of. 135 00:06:37,580 --> 00:06:40,090 It's really, I think it's very creative. 136 00:06:40,090 --> 00:06:42,580 And it's what attracted me to being a programmer in the first place. 137 00:06:42,580 --> 00:06:46,230 It's that I could, I could leverage the abilities inside of here, 138 00:06:46,230 --> 00:06:49,790 and I could do things, cool things, on behalf of the user. 139 00:06:49,790 --> 00:06:54,570 So, code, software, a program. 140 00:06:54,570 --> 00:06:58,720 So, let's get a non-technical example of this. 141 00:07:00,270 --> 00:07:03,880 So, I'll have you link out to the YouTube [COUGH] for this. 142 00:07:03,880 --> 00:07:06,170 This is the Macarena. 143 00:07:06,170 --> 00:07:09,700 The Macarena is a song that 144 00:07:09,700 --> 00:07:11,750 has with it a well-known dance that everyone 145 00:07:11,750 --> 00:07:14,730 seems to know, or either get taught very quickly. 146 00:07:14,730 --> 00:07:18,993 So I'll, I'll stop and let you watch the Macarena, and then come back. 147 00:07:18,993 --> 00:07:22,016 [BLANK_AUDIO] 148 00:07:22,016 --> 00:07:23,590 So welcome back. 149 00:07:23,590 --> 00:07:24,680 I hope you enjoyed that. 150 00:07:25,910 --> 00:07:28,600 In a sense what we've got there is 151 00:07:28,600 --> 00:07:29,580 a program. 152 00:07:29,580 --> 00:07:30,830 A program for human beings. 153 00:07:31,868 --> 00:07:33,749 And, maybe you learned that at a club or 154 00:07:33,749 --> 00:07:36,160 something, and they told you what to do next. 155 00:07:36,160 --> 00:07:37,740 Well, I can teach you how to do 156 00:07:37,740 --> 00:07:41,160 the Macarena by writing a simple program right now. 157 00:07:41,160 --> 00:07:42,210 So here's my Macarena. 158 00:07:43,740 --> 00:07:45,990 While the music plays, means you do it over and over 159 00:07:45,990 --> 00:07:48,570 and over again, to the beat, that's kind of like computers. 160 00:07:48,570 --> 00:07:49,620 They do things in a beat. 161 00:07:49,620 --> 00:07:52,000 They happen to have three billion beats a second. 162 00:07:52,000 --> 00:07:55,300 But as it were, so we're going to do this multiple times so 163 00:07:55,300 --> 00:07:58,180 we have this whole group of instructions that we're going to do, right? 164 00:07:59,270 --> 00:08:05,130 Left hand out and up, right hand out and up, flip left hand, flip right hand, left 165 00:08:05,130 --> 00:08:11,960 hand to right shoulder, right hand to left shoulder, et cetera, et cetera. 166 00:08:11,960 --> 00:08:17,170 Now this particular little program has a mistake in it. 167 00:08:17,170 --> 00:08:18,350 Actually several. 168 00:08:18,350 --> 00:08:22,141 I want you to look and see if you can find the mistakes in the program. 169 00:08:22,141 --> 00:08:29,093 [BLANK_AUDIO] 170 00:08:29,093 --> 00:08:30,000 Okay. 171 00:08:30,000 --> 00:08:33,480 So here are the places that have the mistake. 172 00:08:33,480 --> 00:08:34,040 Right? 173 00:08:34,040 --> 00:08:34,850 The mistake is 174 00:08:37,540 --> 00:08:43,910 right ham to the back of the head, and left hand to right hit, not hip. 175 00:08:43,910 --> 00:08:47,790 Now if you're in a bar and you take a ham and you hit somebody 176 00:08:47,790 --> 00:08:52,360 in the back of the head, that's not very nice when you're dancing to this song. 177 00:08:52,360 --> 00:08:54,270 These are what's called bugs. 178 00:08:54,270 --> 00:08:56,550 Now a human reading this 179 00:08:56,550 --> 00:09:00,910 would say, oh, I think they meant to say "hand" here. 180 00:09:00,910 --> 00:09:03,380 But a computer is much more literal than people. 181 00:09:03,380 --> 00:09:06,530 We'll see a couple of exercises where we'll see that people 182 00:09:06,530 --> 00:09:11,780 can correct little mistakes like this, but computers they cannot, right? 183 00:09:11,780 --> 00:09:14,560 So we have to fix these bugs and we have to say 184 00:09:14,560 --> 00:09:16,750 right hand and we have to say hip when we mean hip. 185 00:09:16,750 --> 00:09:18,290 So we have to be explicit. 186 00:09:18,290 --> 00:09:20,680 Computers do exactly what we say. 187 00:09:20,680 --> 00:09:22,730 They don't do what we mean to do. 188 00:09:22,730 --> 00:09:24,800 So, let's clear that. 189 00:09:24,800 --> 00:09:27,120 Here is another example, okay? 190 00:09:27,120 --> 00:09:28,742 Let's see how this comes out. 191 00:09:28,742 --> 00:09:34,192 [SOUND] 192 00:09:34,192 --> 00:09:34,518 [MUSIC] 193 00:09:34,518 --> 00:09:37,158 You're supposed to count the number of 194 00:09:37,158 --> 00:09:40,580 times the word "the" appears in this sentence. 195 00:09:42,240 --> 00:09:43,830 Count it. 196 00:09:43,830 --> 00:09:46,030 And the word "the", how many times? 197 00:09:46,030 --> 00:09:53,015 [MUSIC] 198 00:09:53,015 --> 00:09:59,373 Okay, it's your turn. 199 00:09:59,373 --> 00:10:06,402 [BLANK_AUDIO] 200 00:10:06,402 --> 00:10:07,033 Now, here. 201 00:10:07,033 --> 00:10:10,212 [BLANK_AUDIO] 202 00:10:10,212 --> 00:10:12,150 This is not something humans are good at. 203 00:10:12,150 --> 00:10:14,690 I moved it around, I played a little music, I confused you, 204 00:10:14,690 --> 00:10:17,920 I put a picture of a clown car in the upper left-hand corner. 205 00:10:17,920 --> 00:10:20,130 Et cetera, et cetera, et cetera. 206 00:10:20,130 --> 00:10:23,840 Now it turns out that computers, once we tell 207 00:10:23,840 --> 00:10:27,490 them what to do, are very good at concentration. 208 00:10:27,490 --> 00:10:32,720 It can easily go through 30 words and find the most common word. 209 00:10:32,720 --> 00:10:35,250 Or three million words and find the most common word. 210 00:10:35,250 --> 00:10:36,800 And it'll never make a mistake. 211 00:10:36,800 --> 00:10:38,649 We first have to give it a set of instructions. 212 00:10:39,700 --> 00:10:44,670 So, I don't want you to learn this right now, but this is a Python program. 213 00:10:44,670 --> 00:10:49,770 Let's say that I want to teach, let you count words in files, okay? 214 00:10:49,770 --> 00:10:52,510 I say hey, I know how to program Python, I'll 215 00:10:52,510 --> 00:10:54,670 send you an email, and I'll send you this program. 216 00:10:54,670 --> 00:10:57,210 Just stick it into Python, and it will count words for you. 217 00:10:58,310 --> 00:10:58,500 Right? 218 00:10:58,500 --> 00:11:00,540 You've got a million words, million lines in a 219 00:11:00,540 --> 00:11:02,310 file, you want to find the most common word. 220 00:11:03,610 --> 00:11:04,890 And so, so here we go. 221 00:11:04,890 --> 00:11:07,750 So I will send you this file called words.py 222 00:11:07,750 --> 00:11:10,810 I spent a little time, it's a friendly gift to you. 223 00:11:10,810 --> 00:11:12,640 And this is what I type in. Now I'll give 224 00:11:12,640 --> 00:11:15,110 you kind of an outline on what this is going to do. 225 00:11:15,110 --> 00:11:16,630 First thing it's going to do is open a 226 00:11:16,630 --> 00:11:19,480 file and read it, then it's going to split 227 00:11:19,480 --> 00:11:23,790 the lines and files into words based on the spaces. 228 00:11:23,790 --> 00:11:26,000 Then it's going to run through and accumulate 229 00:11:26,000 --> 00:11:29,000 numbers like, you know, this word is one, 230 00:11:29,000 --> 00:11:31,770 this word is one, oh I saw that again so I turn that to two. 231 00:11:31,770 --> 00:11:33,650 That's what this does, it's a loop, it goes 232 00:11:33,650 --> 00:11:36,360 round and round and round, one for each word. 233 00:11:36,360 --> 00:11:38,264 Then what we're going to do is we're 234 00:11:38,264 --> 00:11:40,840 going to write another loop that's going to figure out 235 00:11:40,840 --> 00:11:42,688 which is the most common word by looking 236 00:11:42,688 --> 00:11:45,450 at all those little histograms that we built up. 237 00:11:45,450 --> 00:11:47,590 And then it's going to print those things out at the very end. 238 00:11:48,870 --> 00:11:53,570 And this can certainly do Python words.py and read clown.txt 239 00:11:53,570 --> 00:11:56,730 and tell us that the word "the" occurs seven times. 240 00:11:56,730 --> 00:12:00,290 But, you know, it can go, it can find out that a different thing 241 00:12:00,290 --> 00:12:04,220 has the word "to" and that it occurs sixteen times and it's just as fast. 242 00:12:04,220 --> 00:12:07,690 So yeah, you have to learn a language and you have to tell it what to 243 00:12:07,690 --> 00:12:13,050 do, but once you do it'll do it for a million or a billion words and happily. 244 00:12:13,050 --> 00:12:14,510 So you don't have to do menial work 245 00:12:14,510 --> 00:12:19,585 once you understand the way to instruct the computer to do menial work. 246 00:12:19,585 --> 00:12:22,520 [NOISE] 247 00:12:22,520 --> 00:12:28,570 So, we always start all programming classes with hardware architecture. 248 00:12:28,570 --> 00:12:32,120 I, I don't think it's essential, so don't get too excited about it. 249 00:12:32,120 --> 00:12:33,440 It's a good 250 00:12:33,440 --> 00:12:35,310 use of terminology so we can have some words 251 00:12:35,310 --> 00:12:37,810 like, say, like CPU and you don't freak out. 252 00:12:37,810 --> 00:12:41,980 Or memory, or RAM, or disk drive and you don't freak out. 253 00:12:41,980 --> 00:12:43,910 I don't want to turn you into a hardware nut. I just want you 254 00:12:43,910 --> 00:12:47,260 to kind of have a few words so we can talk about what's going on inside. 255 00:12:47,260 --> 00:12:49,640 Because, in a sense, we're going to be writing 256 00:12:49,640 --> 00:12:53,150 programs to do stuff, both data, instructions, et cetera. 257 00:12:54,480 --> 00:12:58,120 So, here's some hardware that I just bought 258 00:12:58,120 --> 00:13:00,420 a couple weeks ago and I'm really in love with, 259 00:13:00,420 --> 00:13:02,400 and that is the Raspberry Pi. 260 00:13:02,400 --> 00:13:06,130 This is a single-board computer. 261 00:13:06,130 --> 00:13:10,530 It's got storage on an SD card right there. 262 00:13:10,530 --> 00:13:12,680 That's the operating system and the data. 263 00:13:12,680 --> 00:13:17,120 And it's got the both a microprocessor and the memory is in 264 00:13:17,120 --> 00:13:20,200 here, as well, and it hooks up with USB and HDMI and 265 00:13:20,200 --> 00:13:23,220 various things, and if you want, in this course, you probably can 266 00:13:23,220 --> 00:13:28,460 do all of the homework using a Raspberry Pi if you so desire. 267 00:13:28,460 --> 00:13:30,490 So, this is what hardware really looks like. 268 00:13:30,490 --> 00:13:32,100 It's kind of the inside of something. 269 00:13:32,100 --> 00:13:34,980 Normally, it's in some kind of case and you don't get to see it. 270 00:13:34,980 --> 00:13:35,900 And that's what it looks like. 271 00:13:35,900 --> 00:13:40,340 It's kind of got this green and little silver and gold. 272 00:13:40,340 --> 00:13:41,600 I think they're very beautiful. 273 00:13:43,460 --> 00:13:44,970 They're very pretty. 274 00:13:44,970 --> 00:13:46,670 A lot of engineering goes into making these things. 275 00:13:48,950 --> 00:13:51,980 And and so we kind of have a block diagram of 276 00:13:51,980 --> 00:13:56,520 what's going on in here, and there's some, just some terminology. 277 00:13:56,520 --> 00:14:00,370 The, the brains of it all, well. 278 00:14:00,370 --> 00:14:02,820 We draw this black diagram partly because, and this is 279 00:14:02,820 --> 00:14:05,990 a, from a, oh, parts are coming off of this. 280 00:14:07,700 --> 00:14:07,750 Eh. 281 00:14:07,750 --> 00:14:09,830 I don't know what that was. 282 00:14:09,830 --> 00:14:12,100 It's okay, it's broken anyways. 283 00:14:12,100 --> 00:14:15,280 And if you have a desktop computer, this is more like what it looks like inside. 284 00:14:15,280 --> 00:14:17,850 This part is called a motherboard, and it's kind 285 00:14:17,850 --> 00:14:20,620 of like the thing that connects and brings everything together. 286 00:14:20,620 --> 00:14:22,150 It's got a bunch of wires. 287 00:14:22,150 --> 00:14:24,600 Each one of those little lines here is a wire. 288 00:14:24,600 --> 00:14:26,930 It's covered with sort of a lacquer. 289 00:14:26,930 --> 00:14:28,560 And then there are things that penetrate 290 00:14:28,560 --> 00:14:31,830 the board, and then connect to various chips. 291 00:14:31,830 --> 00:14:35,490 And this whole thing is what this picture is. 292 00:14:35,490 --> 00:14:37,700 But it really is connecting a number of different components. 293 00:14:39,260 --> 00:14:42,510 The central processing unit that I've spoken of before. 294 00:14:42,510 --> 00:14:43,370 Put that back down. 295 00:14:43,370 --> 00:14:46,054 [SOUND] Central processing unit is the closest thing a 296 00:14:46,054 --> 00:14:48,750 computer has to a brain, but it's barely a brain. 297 00:14:48,750 --> 00:14:52,380 It's really just a super fast programmable calculator. 298 00:14:52,380 --> 00:14:55,670 It, we make it flexible by our creativity when we write programs. 299 00:14:55,670 --> 00:14:57,320 We make it seem intelligent. 300 00:14:57,320 --> 00:15:00,770 It's people that make it intelligent by taking our own knowledge 301 00:15:00,770 --> 00:15:02,000 and putting it in. 302 00:15:02,000 --> 00:15:05,600 This is not itself intelligent, there's nothing to fear from this. 303 00:15:05,600 --> 00:15:06,630 It's just not that smart. 304 00:15:08,040 --> 00:15:12,400 So, this is the thing that's programmed to ask the question "what's next?" 305 00:15:12,400 --> 00:15:14,680 And then we have to have a set of instructions 306 00:15:14,680 --> 00:15:18,370 that feed this thing really fast, billions of times a second. 307 00:15:18,370 --> 00:15:21,500 And that's what this is, this is the random access memory. 308 00:15:21,500 --> 00:15:23,290 And we have memory chips and 309 00:15:23,290 --> 00:15:25,790 they're connected together through the motherboard. 310 00:15:25,790 --> 00:15:30,990 So, we have the main memory and we have the central processing unit. 311 00:15:30,990 --> 00:15:33,870 And this is where our high-speed instructions come 312 00:15:33,870 --> 00:15:36,470 from, this is where our high-speed data is stored. 313 00:15:36,470 --> 00:15:40,110 And this is the thing that asks "what next?" and it reads its instructions from here. 314 00:15:40,110 --> 00:15:41,640 And you'll see that kind of like, oop, they're not 315 00:15:41,640 --> 00:15:44,790 quite connected together but eventually they're kind of connecting together. 316 00:15:44,790 --> 00:15:46,560 Don't feel too bad about this hardware it's all 317 00:15:46,560 --> 00:15:49,840 old and it's all broken and it can't be hurt. 318 00:15:49,840 --> 00:15:53,610 So, the next thing we got is input/output device. 319 00:15:53,610 --> 00:15:55,160 So I'll go back to my Raspberry Pi. 320 00:15:55,160 --> 00:15:57,980 So the Raspberry Pi has a USB 321 00:15:57,980 --> 00:16:01,190 that you can connect a mouse or a keyboard. 322 00:16:01,190 --> 00:16:05,150 It has a HDMI that you can connect a monitor to. 323 00:16:05,150 --> 00:16:06,490 I has an Ethernet connector. 324 00:16:06,490 --> 00:16:09,770 So these are all examples of 325 00:16:09,770 --> 00:16:11,100 input/output devices. 326 00:16:12,890 --> 00:16:17,870 And then the last thing on the screen is the secondary memory. 327 00:16:17,870 --> 00:16:22,380 So, this RAM [SOUND] on the Raspberry Pi, the CPU, the central processing 328 00:16:22,380 --> 00:16:25,495 unit and the RAM are all on this one chip in the middle. 329 00:16:25,495 --> 00:16:29,780 It's called SOC or system on a chip where they put more parts there. 330 00:16:29,780 --> 00:16:33,280 So in a sense it collapsed 331 00:16:35,300 --> 00:16:40,590 this, and this, and a lot of this all down in the Raspberry Pi 332 00:16:41,840 --> 00:16:43,140 to one little guy. 333 00:16:43,140 --> 00:16:45,320 But it's still architecturally the same thing. 334 00:16:45,320 --> 00:16:46,900 There's a central processing unit. 335 00:16:46,900 --> 00:16:47,690 There's main memory. 336 00:16:47,690 --> 00:16:49,376 There's graphics cards, etc. 337 00:16:49,376 --> 00:16:53,030 [SOUND] So, input/output devices. 338 00:16:53,030 --> 00:16:53,940 Oh, this big 339 00:16:53,940 --> 00:16:59,020 this guy has input/output devices too, like USB and keyboard, monitor, etc. 340 00:16:59,020 --> 00:17:00,230 So they're, they're very similar. 341 00:17:00,230 --> 00:17:02,770 It's just, this is new, and this is old. 342 00:17:02,770 --> 00:17:05,370 Everything gets smaller when it gets newer. 343 00:17:06,560 --> 00:17:07,607 Okay. 344 00:17:07,607 --> 00:17:10,640 [SOUND] Okay. 345 00:17:10,640 --> 00:17:13,990 So the last thing we gotta talk about is the secondary memory. 346 00:17:13,990 --> 00:17:14,490 Well, 347 00:17:16,310 --> 00:17:19,720 when the power goes off, these things sort of go away. 348 00:17:19,720 --> 00:17:21,830 The data in this RAM goes away. 349 00:17:21,830 --> 00:17:24,210 It's just designed to be really fast. 350 00:17:24,210 --> 00:17:25,450 But not permanent. 351 00:17:25,450 --> 00:17:28,600 So we need a place that's permanent, that's what secondary storage is. 352 00:17:28,600 --> 00:17:32,260 That's what, that's what this secondary storage is for, this is permanent. 353 00:17:33,520 --> 00:17:38,820 This is fast, and it cha cha cha cha cha really fast. 354 00:17:38,820 --> 00:17:42,290 And but this is permanent, and this is slower. 355 00:17:42,290 --> 00:17:42,900 Okay? 356 00:17:42,900 --> 00:17:44,110 So the secondary memory. 357 00:17:44,110 --> 00:17:46,480 [SOUND] I've got two kind of secondary memory. 358 00:17:46,480 --> 00:17:48,105 Oh, dropped it on the floor. 359 00:17:48,105 --> 00:17:50,740 [SOUND] Two kinds of secondary memory. 360 00:17:50,740 --> 00:17:52,480 I'll start with the Raspberry Pi. 361 00:17:52,480 --> 00:17:56,940 The secondary memory of the Raspberry Pi is this SD card. 362 00:17:56,940 --> 00:17:58,380 It's like a disk drive. 363 00:17:58,380 --> 00:18:00,210 It still is permanent. 364 00:18:00,210 --> 00:18:02,540 Does not require power to maintain its data. 365 00:18:02,540 --> 00:18:04,110 The data stays permanent. 366 00:18:04,110 --> 00:18:06,220 So, in the future we will see more 367 00:18:06,220 --> 00:18:10,220 of these flash-style drives and SD-style drives. 368 00:18:10,220 --> 00:18:13,040 So the Raspberry Pi is kind of alluding to the future. 369 00:18:13,040 --> 00:18:14,540 [SOUND] There's a disk drive in here. 370 00:18:14,540 --> 00:18:15,410 It's not really a disk. 371 00:18:15,410 --> 00:18:17,190 It's also flash memory. 372 00:18:17,190 --> 00:18:22,970 But in the old days, in the good old days, back when I was a kid, 373 00:18:22,970 --> 00:18:28,150 we, our secondary memory was [SOUND] a disk drive, 374 00:18:28,150 --> 00:18:33,760 and it had [SOUND] platters, and it spun, and it made a satisfying noise. 375 00:18:33,760 --> 00:18:35,640 And it would move in and out to read data, and 376 00:18:35,640 --> 00:18:37,495 I'll show you a video of this just in a bit. 377 00:18:37,495 --> 00:18:41,420 [SOUND] And so these would record the data on the magnetic platters 378 00:18:41,420 --> 00:18:43,450 and then when the power's taken off, the data would still be magnetized. 379 00:18:43,450 --> 00:18:45,480 And then it would go and move to the 380 00:18:45,480 --> 00:18:48,260 right spot, [SOUND] spin it around, and read the data. 381 00:18:48,260 --> 00:18:52,740 And again, this is kind of messed up in a pretty bad way. 382 00:18:52,740 --> 00:18:54,290 So, there we go. 383 00:18:55,310 --> 00:19:01,570 Central processing unit, brains of the operation, main memory, fast, 384 00:19:01,570 --> 00:19:06,060 but goes away when we power on, input output devices, keyboards, et cetera. 385 00:19:06,060 --> 00:19:11,740 And then storage, that has, maintains its data across power cycles. 386 00:19:11,740 --> 00:19:15,480 Okay, and I just said all that. 387 00:19:15,480 --> 00:19:15,980 Okay? 388 00:19:17,220 --> 00:19:21,340 So then the question is, where do you belong in this? 389 00:19:21,340 --> 00:19:22,570 Where do programs live? 390 00:19:22,570 --> 00:19:23,980 Where do we write? 391 00:19:23,980 --> 00:19:28,180 And the answer is, we kind of live in the memory, right. 392 00:19:28,180 --> 00:19:31,320 What we do is we put our programs into the 393 00:19:31,320 --> 00:19:36,250 memory and then the CPU pulls the programs out of the memory. 394 00:19:36,250 --> 00:19:39,530 So we have to write our programs and put them into the memory. 395 00:19:39,530 --> 00:19:41,990 When we start them and run them we're really loading them 396 00:19:41,990 --> 00:19:45,590 into the memory so they can be fed rapidly to the CPU. 397 00:19:45,590 --> 00:19:49,400 Now the computers don't really execute Python, like "if x less 398 00:19:49,400 --> 00:19:52,420 than three print", but that's what we tend to want to write. 399 00:19:52,420 --> 00:19:55,290 Because what the computers really execute is a thing called 400 00:19:55,290 --> 00:19:58,490 machine language, which is a series of zeroes and ones 401 00:19:58,490 --> 00:20:01,880 that pretty much translate directly to what's on these pins. 402 00:20:01,880 --> 00:20:04,900 There's voltages that go up and down. 403 00:20:04,900 --> 00:20:06,830 That's called machine language. 404 00:20:06,830 --> 00:20:10,900 Source code, like Python, is written in a 405 00:20:10,900 --> 00:20:14,260 way that's most convenient, well, at least more convenient. 406 00:20:14,260 --> 00:20:16,510 Machine language is what's most convenient 407 00:20:16,510 --> 00:20:17,700 for the hardware. 408 00:20:17,700 --> 00:20:20,320 So we either, we have to translate from source code to 409 00:20:20,320 --> 00:20:23,720 machine language, and that's what the Python program does for us. 410 00:20:23,720 --> 00:20:28,030 We write in Python, and Python translates to machine language for us. 411 00:20:28,030 --> 00:20:33,570 So, I got a couple of videos that give you a sense of how this all works. 412 00:20:33,570 --> 00:20:36,500 We'll start with CPU. 413 00:20:36,500 --> 00:20:40,270 And what this is going to do is, this is going to show you the intensity of 414 00:20:40,270 --> 00:20:43,380 how much electricity, the thing that gets hot 415 00:20:43,380 --> 00:20:45,530 inside your computer is this little guy right here. 416 00:20:45,530 --> 00:20:48,625 And we're going to see in this video just how hot it can get. 417 00:20:48,625 --> 00:20:56,050 [SOUND]. 418 00:20:56,050 --> 00:20:58,290 Okay, so welcome back. 419 00:20:58,290 --> 00:20:59,860 So the next thing I'm going to show you 420 00:20:59,860 --> 00:21:02,660 I showed you a hard disk that sort of didn't work. 421 00:21:02,660 --> 00:21:05,920 But we're actually going to show you a real short video on how a hard 422 00:21:05,920 --> 00:21:07,990 disk works [SOUND] that someone took the 423 00:21:07,990 --> 00:21:10,220 cover off and actually applied power to it. 424 00:21:10,220 --> 00:21:11,730 You don't want to do this yourself if you have a 425 00:21:11,730 --> 00:21:15,770 hard drive, I've read, and some people say that you can do 426 00:21:15,770 --> 00:21:18,910 it for a, for a few minutes and then the drive 427 00:21:18,910 --> 00:21:22,590 kind of destroys itself if you run it with the cover off. 428 00:21:22,590 --> 00:21:29,562 So let's take a look at this. 429 00:21:29,562 --> 00:21:36,340 Okay, so. 430 00:21:36,340 --> 00:21:39,760 Now, we're going to talk a little bit about Python. 431 00:21:41,470 --> 00:21:43,690 Python is a programming language. 432 00:21:43,690 --> 00:21:46,640 Python is a way that we communicate. 433 00:21:47,640 --> 00:21:50,410 Now Python wasn't invented by computers. 434 00:21:50,410 --> 00:21:55,820 We invented Python as humans as a way to encapsulate our instructions. 435 00:21:55,820 --> 00:21:57,690 And there's lots of different programming languages. 436 00:21:57,690 --> 00:22:02,120 Python, Javascript, C++, tons of them. 437 00:22:02,120 --> 00:22:04,470 Python's just one of them that we happen to teach in this class. 438 00:22:07,020 --> 00:22:09,560 Now, I'll start with a little Harry Potter reference. 439 00:22:09,560 --> 00:22:13,179 Parseltongue is the language of serpents and those who converse with them. 440 00:22:14,650 --> 00:22:18,120 An individual who can speak Parseltongue is known as a Parselmouth. 441 00:22:18,120 --> 00:22:21,350 And it's a very uncommon skill, and it may be even hereditary. 442 00:22:21,350 --> 00:22:25,580 Nearly all known Parselmouths are descended from Salazar Slytherin. 443 00:22:25,580 --> 00:22:27,950 There's our Harry Potter reference. 444 00:22:27,950 --> 00:22:30,830 Python is the language of the Python 445 00:22:30,830 --> 00:22:33,650 interpreter, and those who can converse with it. 446 00:22:33,650 --> 00:22:37,210 We're going to converse with the inside of a computer pretty soon. 447 00:22:37,210 --> 00:22:40,980 An individual who can speak Python is known as a Pythonista. 448 00:22:40,980 --> 00:22:43,340 It is an uncommon skill and may be hereditary. 449 00:22:44,450 --> 00:22:46,120 It may not be hereditary too. 450 00:22:46,120 --> 00:22:51,480 Nearly all known Pythonistas use software initially developed by Guido van Rossum. 451 00:22:51,480 --> 00:22:54,150 Guido van Rossum, this guy right here. 452 00:22:54,150 --> 00:22:56,390 Yo Guido, what's up? 453 00:22:56,390 --> 00:22:58,860 Let's put a mustache on, yo Guido. 454 00:22:58,860 --> 00:22:59,350 Sorry. 455 00:22:59,350 --> 00:23:00,690 I should be nice to him. 456 00:23:00,690 --> 00:23:02,190 He is the inventor of Python. 457 00:23:02,190 --> 00:23:04,430 Python's over 20 years old. 458 00:23:04,430 --> 00:23:09,330 He invented it to make it an easy language but was both easy and powerful. 459 00:23:09,330 --> 00:23:13,640 And that's why it's a great language to start your learning with. 460 00:23:13,640 --> 00:23:17,680 It's a powerful language but it's also designed to be easy to use. 461 00:23:17,680 --> 00:23:23,088 Can anyone guess what the reason for the Python language name is? 462 00:23:23,088 --> 00:23:32,072 [BLANK_AUDIO] 463 00:23:32,072 --> 00:23:37,695 So let's see. 464 00:23:37,695 --> 00:23:40,200 [MUSIC] 465 00:23:40,200 --> 00:23:45,912 Python was named after a famous British comedy show that was in the 60s and 70s 466 00:23:45,912 --> 00:23:52,250 and 80s, I think, named Monty Python, Monty Python's Flying Circus. 467 00:23:52,250 --> 00:23:53,640 And so 468 00:23:53,640 --> 00:23:56,100 And I, I think he was trying to capture a 469 00:23:56,100 --> 00:24:00,850 playfulness, a certain kind of silly fun aspect of Python. 470 00:24:00,850 --> 00:24:03,600 And and so there we go, enough of that. 471 00:24:03,600 --> 00:24:04,280 We done? 472 00:24:04,280 --> 00:24:07,380 Yeah, okay, the music's done now, thank heaven for that. 473 00:24:07,380 --> 00:24:14,750 Okay, so again this is a language and this Guido, he made it for us, he made choices. 474 00:24:14,750 --> 00:24:16,960 He said we're going to put a colon here. 475 00:24:16,960 --> 00:24:19,240 I think we should like indent this and do these things. 476 00:24:19,240 --> 00:24:20,880 And he, he's made choices. 477 00:24:20,880 --> 00:24:25,510 And [COUGH] and some languages have people like some different better than others. 478 00:24:25,510 --> 00:24:27,170 It's kind of an artistic choice. 479 00:24:27,170 --> 00:24:31,080 And, and I like to kind of equate this to 480 00:24:31,080 --> 00:24:35,170 learning a language to speak with people, with humans, you know. 481 00:24:36,690 --> 00:24:39,630 You know, when we're a baby we don't know how to talk and 482 00:24:39,630 --> 00:24:43,260 we start babbling, blah, blah, blah, blah, blah, blah. Maybe we just start crying. 483 00:24:43,260 --> 00:24:44,240 That's the first thing that we do. 484 00:24:46,870 --> 00:24:48,633 [SOUND] But we're like, we're like on this strange 485 00:24:48,633 --> 00:24:50,640 planet. We got to talk to this thing. 486 00:24:50,640 --> 00:24:52,000 So we have to learn its language. 487 00:24:52,000 --> 00:24:53,960 And we're not going to learn it right away. 488 00:24:53,960 --> 00:24:56,750 You don't go like, hey study all night and you know this language. 489 00:24:56,750 --> 00:24:58,530 There's no way you can do that. 490 00:24:58,530 --> 00:25:00,340 Although Rosetta Stone might be a good way to 491 00:25:00,340 --> 00:25:03,470 teach programming, maybe I should take some of their ideas. 492 00:25:06,100 --> 00:25:08,690 So, the thing that's different about learning a 493 00:25:08,690 --> 00:25:12,660 human language versus learning a programming language is that 494 00:25:12,660 --> 00:25:15,320 when we're learning a human language we're talking to 495 00:25:15,320 --> 00:25:17,510 a human, and they're going to do correction for us. 496 00:25:17,510 --> 00:25:19,050 So if 497 00:25:19,050 --> 00:25:24,380 if I say a word incorrectly, like," Mama-alah." 498 00:25:24,380 --> 00:25:24,630 Right? 499 00:25:24,630 --> 00:25:25,050 I don't know. 500 00:25:25,050 --> 00:25:26,800 That was pretty dumb. 501 00:25:26,800 --> 00:25:29,630 Someone listening says, oh, I know what he said, I know what he said. 502 00:25:29,630 --> 00:25:34,010 But Python and computers aren't really listening. 503 00:25:34,010 --> 00:25:35,130 They're kind of dumb. 504 00:25:35,130 --> 00:25:38,240 They can't really correct our mistakes for us 505 00:25:38,240 --> 00:25:42,100 because they don't know what we're trying to say, they really don't. 506 00:25:42,100 --> 00:25:42,990 They're very literal. 507 00:25:44,370 --> 00:25:46,600 And, so it is really common 508 00:25:48,900 --> 00:25:52,490 in the beginning to get upset. 509 00:25:52,490 --> 00:25:58,530 Because we say something we think is cute, and it says, syntax error. 510 00:25:58,530 --> 00:26:01,030 And we'd be like, okay let me try this. 511 00:26:01,030 --> 00:26:02,630 And it says, syntax error. 512 00:26:02,630 --> 00:26:05,010 And so we get this notion, and I had this problem when I was 513 00:26:05,010 --> 00:26:08,640 first programming, I was like, I would like, here's my program, do you like it? 514 00:26:08,640 --> 00:26:09,750 It would say, syntax error. 515 00:26:11,140 --> 00:26:15,640 Now, the problem is, they could reword the messages to be a little nicer 516 00:26:15,640 --> 00:26:18,310 perhaps, but the syntax error isn't really a 517 00:26:18,310 --> 00:26:20,750 judgment on you that says you're a failure. 518 00:26:20,750 --> 00:26:26,400 The syntax error is really saying, I, I don't know what you're saying. 519 00:26:26,400 --> 00:26:27,450 I'm confused. 520 00:26:27,450 --> 00:26:33,450 I only know a few things, and what you just said is not something I understand. 521 00:26:33,450 --> 00:26:37,800 So instead of thinking of the program, the Python as some kind of 522 00:26:37,800 --> 00:26:43,620 evil, demonic monster that just hates you and just keeps saying syntax error, 523 00:26:43,620 --> 00:26:46,220 think of Python more like a dog. 524 00:26:47,470 --> 00:26:50,700 Right, the dog's, what can you talk to a dog? 525 00:26:50,700 --> 00:26:55,110 Can you say "lovely sunset we're having" to a dog? 526 00:26:55,110 --> 00:26:57,380 Because the dog's not going to understand that. 527 00:26:57,380 --> 00:27:01,270 The dog does understand some things like food, 528 00:27:01,270 --> 00:27:02,280 back, walk. 529 00:27:02,280 --> 00:27:05,700 But it doesn't understand the accumulated works of Shakespeare. 530 00:27:06,800 --> 00:27:09,400 So when you talk to a dog, you've gotta be careful 531 00:27:09,400 --> 00:27:12,920 to talk the subset of the vocabulary that the dog knows. 532 00:27:12,920 --> 00:27:16,750 And so this is a key thing when you're first learning. 533 00:27:17,920 --> 00:27:20,580 There's only a certain set of things that Python understands. 534 00:27:20,580 --> 00:27:24,370 It turns out it's easier to teach you Python 535 00:27:24,370 --> 00:27:26,940 than to teach Python to listen to whatever you have to say. 536 00:27:28,260 --> 00:27:30,430 Things like Google make it seem intelligent. 537 00:27:30,430 --> 00:27:34,010 So you can kind of type anything into Google, right? 538 00:27:34,010 --> 00:27:36,150 Well, yeah, billions of dollars later 539 00:27:36,150 --> 00:27:38,370 Google, for at least, like, short things, can 540 00:27:38,370 --> 00:27:40,210 seem like it knows what you're talking about. 541 00:27:42,080 --> 00:27:44,720 In terms of programming, it's a lot easier for you to 542 00:27:44,720 --> 00:27:48,130 figure out the exact precise way to say it, rather than make 543 00:27:48,130 --> 00:27:50,710 it so that we have to spend a billion dollars on 544 00:27:50,710 --> 00:27:54,100 something like Python to figure out what you mean in your programs. 545 00:27:54,100 --> 00:27:57,590 So, let's start talking to Python. 546 00:27:58,750 --> 00:28:00,420 We're talking to Python. 547 00:28:00,420 --> 00:28:01,320 So. 548 00:28:01,320 --> 00:28:04,240 If you've installed Python properly, whether it's on a 549 00:28:04,240 --> 00:28:08,390 Mac or a Windows or on a Raspberry Pi, at 550 00:28:08,390 --> 00:28:11,230 some point you'll be in a terminal program and 551 00:28:11,230 --> 00:28:15,160 you'll type Python to make Python run in interactive mode. 552 00:28:15,160 --> 00:28:19,060 You might have to type c:\ something something Python in Windows. 553 00:28:19,060 --> 00:28:21,580 But at some point you're running Python. 554 00:28:21,580 --> 00:28:25,280 Now Python itself is a program, it's a program 555 00:28:25,280 --> 00:28:28,760 that is asking you to type the Python language. 556 00:28:28,760 --> 00:28:31,500 Now the interesting thing is you've got this chevron prompt 557 00:28:31,500 --> 00:28:36,610 here, and it's kind of another version of "what's next?" 558 00:28:36,610 --> 00:28:39,000 I told you that this hardware was designed 559 00:28:39,000 --> 00:28:41,900 to always want the next instruction to come in. 560 00:28:41,900 --> 00:28:43,330 Well. 561 00:28:43,330 --> 00:28:46,510 Python, once we start it, it really has no idea what to do. 562 00:28:46,510 --> 00:28:49,090 It is, is waiting for you to tell it what to do. 563 00:28:49,090 --> 00:28:49,900 Okay? 564 00:28:49,900 --> 00:28:53,020 So, let me see if I can pop something up here. 565 00:28:54,600 --> 00:28:55,780 So here we go. 566 00:28:55,780 --> 00:28:56,210 Clear that. 567 00:28:56,210 --> 00:28:58,400 And now I'm going to type. 568 00:28:58,400 --> 00:29:01,480 Get this a little closer. 569 00:29:01,480 --> 00:29:03,280 I'm going to start Python, so it's the 570 00:29:03,280 --> 00:29:05,370 operating system now is asking me what next, 571 00:29:05,370 --> 00:29:08,680 I'm saying, oh the thing I want to do next is I want to run Python. 572 00:29:10,310 --> 00:29:12,090 So, here we are, we're sitting in 573 00:29:12,090 --> 00:29:15,340 the Python interpreter and it's asking "what next?" 574 00:29:17,270 --> 00:29:18,190 Okay? 575 00:29:18,190 --> 00:29:21,745 Now, I, it's like I just landed on a, on like a 576 00:29:21,745 --> 00:29:24,100 planet. 577 00:29:24,100 --> 00:29:24,660 And it's, like, 578 00:29:25,170 --> 00:29:30,065 take me to your leader. 579 00:29:30,065 --> 00:29:33,690 Take me to your leader. 580 00:29:33,690 --> 00:29:35,290 That's what you always say when you land on a 581 00:29:35,290 --> 00:29:37,950 planet and are confronted by some kind of a robot. 582 00:29:37,950 --> 00:29:40,140 And it says, 583 00:29:40,140 --> 00:29:40,980 Syntax error. 584 00:29:42,300 --> 00:29:43,470 Remember, it's a dog. 585 00:29:43,470 --> 00:29:44,970 It should just say aarf, right? 586 00:29:44,970 --> 00:29:48,080 Could say, take me to your leader, woof, okay? 587 00:29:51,610 --> 00:29:53,770 Are you friendly? 588 00:29:57,810 --> 00:30:00,490 I don't even spell, think I spelled friendly right. 589 00:30:00,490 --> 00:30:01,660 Syntax error. 590 00:30:06,740 --> 00:30:08,436 Are you dumb? 591 00:30:08,436 --> 00:30:11,726 Syntax error. 592 00:30:11,726 --> 00:30:14,563 Pretty dumb. 593 00:30:14,563 --> 00:30:17,344 I hate computers! 594 00:30:17,344 --> 00:30:22,330 [BLANK_AUDIO] 595 00:30:22,330 --> 00:30:23,870 Syntax error. 596 00:30:23,870 --> 00:30:26,460 It doesn't seem to have a sense of humor. 597 00:30:26,460 --> 00:30:26,910 Try this. 598 00:30:26,910 --> 00:30:27,630 Knock knock. 599 00:30:30,190 --> 00:30:30,800 Knock knock. 600 00:30:33,600 --> 00:30:35,380 No sense of humor. 601 00:30:36,580 --> 00:30:38,480 So here's the problem. 602 00:30:38,480 --> 00:30:40,390 It wants us, it's 603 00:30:40,390 --> 00:30:42,290 It doesn't hate us. 604 00:30:42,290 --> 00:30:45,060 It, it just wants to know what we want done. 605 00:30:46,800 --> 00:30:50,150 So, we need to know the Python language. 606 00:30:50,150 --> 00:30:52,320 Luckily, I know a bit of Python. 607 00:30:52,320 --> 00:30:57,230 So I'm going to say, hmm, hey Python, I'm going to want some data. 608 00:30:57,230 --> 00:30:59,620 I want to make a variable named "x". 609 00:30:59,620 --> 00:31:00,900 Just a little place in your memory. 610 00:31:00,900 --> 00:31:01,820 Go find it. 611 00:31:01,820 --> 00:31:05,080 Go find one of your spare places in memory. 612 00:31:05,080 --> 00:31:07,798 And I want you to put 100 in that. 613 00:31:07,798 --> 00:31:09,190 Okay? 614 00:31:09,190 --> 00:31:11,290 Do that. 615 00:31:11,290 --> 00:31:14,130 Now, it's happy because I know the language. 616 00:31:15,310 --> 00:31:16,099 Bonjour. 617 00:31:17,210 --> 00:31:18,850 That we know the language. 618 00:31:18,850 --> 00:31:20,370 Now I want to, but it's saying, "what next?" 619 00:31:20,370 --> 00:31:22,100 So we have to put a program in. 620 00:31:23,750 --> 00:31:25,230 So, let's see. 621 00:31:25,230 --> 00:31:27,730 I'm going to say, hey, Python. 622 00:31:27,730 --> 00:31:32,430 I'm going to make a variable called "y", another area in your memory labeled y, 623 00:31:32,430 --> 00:31:35,500 and I want you to go back and remember that x I gave you before? 624 00:31:35,500 --> 00:31:38,700 Go get that one back and add 50 to that. 625 00:31:38,700 --> 00:31:40,280 And put that in y. 626 00:31:40,280 --> 00:31:46,150 So now I've got something in x and I've got something in y, and let's print y out. 627 00:31:48,840 --> 00:31:49,910 What's in y? 628 00:31:49,910 --> 00:31:54,220 Go look in y where you put that and let's print it out. 629 00:31:54,220 --> 00:31:54,850 150. 630 00:31:54,850 --> 00:31:55,490 So. 631 00:31:56,690 --> 00:31:58,750 We're doing simple things, and actually most 632 00:31:58,750 --> 00:32:00,610 programming is a series of simple things. 633 00:32:00,610 --> 00:32:02,650 The number of statements, different statements 634 00:32:02,650 --> 00:32:06,020 you can do is is relatively few. 635 00:32:06,020 --> 00:32:08,470 So we are talking to Python. 636 00:32:10,010 --> 00:32:12,930 Let's run back to the slides, there we go. 637 00:32:12,930 --> 00:32:13,490 And so. 638 00:32:15,870 --> 00:32:18,010 We give it a series of commands and you 639 00:32:18,010 --> 00:32:20,380 can do the same thing sitting on your computer. 640 00:32:20,380 --> 00:32:23,050 And [SOUND] you type exit or quit with 641 00:32:23,050 --> 00:32:25,400 parentheses to get out of it when you're done. 642 00:32:25,400 --> 00:32:26,920 And that ends the interactive session. 643 00:32:26,920 --> 00:32:31,090 Now this is interactive Python where it's asking us command by command and 644 00:32:31,090 --> 00:32:33,410 then interpreting or running those commands as 645 00:32:33,410 --> 00:32:35,520 we run the command, as we finish. 646 00:32:35,520 --> 00:32:36,020 So. 647 00:32:37,130 --> 00:32:39,140 There'll be, you'll be doing it in some kind of a window. 648 00:32:39,140 --> 00:32:41,060 There's a different way to do it on Windows. 649 00:32:41,060 --> 00:32:44,140 My install documentation on pythonlearn.com gives you 650 00:32:44,140 --> 00:32:46,490 all of this, tells you everything to do. 651 00:32:46,490 --> 00:32:48,590 So now we're basically talking to Python. 652 00:32:49,730 --> 00:32:52,080 So, what language? 653 00:32:52,080 --> 00:32:53,600 I gotta still teach you this language. 654 00:32:55,140 --> 00:32:58,380 So, what do we say when we get ahold of Python, what kinds of things? 655 00:32:59,690 --> 00:33:04,530 Just like any language, a human language, there's like vocabulary, 656 00:33:04,530 --> 00:33:09,710 there's basic words, there's variables and reserved words in Python. 657 00:33:09,710 --> 00:33:12,190 Then we kind of combine those in lines to make 658 00:33:12,190 --> 00:33:15,630 sentence-like structures that themselves are not a full story. 659 00:33:15,630 --> 00:33:17,910 And then we kind of make a story out of it. 660 00:33:17,910 --> 00:33:21,420 Now, the story is in the Python language, not sort of English or 661 00:33:21,420 --> 00:33:25,680 French or a human language, but it still is kind of a 662 00:33:25,680 --> 00:33:28,430 a sequence of small pieces that build to make 663 00:33:28,430 --> 00:33:31,560 bigger pieces that then build to make a whole program. 664 00:33:31,560 --> 00:33:37,040 So here is, again, that same program, right? That same program 665 00:33:37,040 --> 00:33:40,440 of how to count the most common word in a file. 666 00:33:40,440 --> 00:33:43,810 And I mentioned before that it starts by opening the file. 667 00:33:43,810 --> 00:33:45,310 It reads the data from the file. 668 00:33:46,930 --> 00:33:49,350 Splits it into words, counts them all up and 669 00:33:49,350 --> 00:33:52,130 then finds the biggest one and then prints it out. 670 00:33:52,130 --> 00:33:57,240 So "name" is like a word, "equals" is another word, "raw" is a 671 00:33:57,240 --> 00:34:01,500 word, all these things are words, each of these things is like a sentence. 672 00:34:02,600 --> 00:34:05,450 There are blocks of stuff that are kind of paragraphs. 673 00:34:05,450 --> 00:34:08,000 There's kind of a paragraph, paragraph. 674 00:34:08,000 --> 00:34:10,150 I for sure, different color here. 675 00:34:10,150 --> 00:34:13,110 Here's like a paragraph, and a paragraph, and a 676 00:34:13,110 --> 00:34:15,830 paragraph, and then at the end of the day, once you 677 00:34:15,830 --> 00:34:18,300 kind of understand it, and you will understand this before 678 00:34:18,300 --> 00:34:21,690 it's all over, this is kind of like a story. 679 00:34:21,690 --> 00:34:21,890 Right? 680 00:34:21,890 --> 00:34:24,480 It holds together, it has a beginning, a middle, and an end. 681 00:34:25,730 --> 00:34:26,820 Again. 682 00:34:26,820 --> 00:34:30,400 Don't worry about the detail, we've got plenty of chapters to cover this detail. 683 00:34:30,400 --> 00:34:32,370 Don't worry about this particular program. 684 00:34:32,370 --> 00:34:35,400 I'm just sort of getting into the sense that, you'll get it. 685 00:34:35,400 --> 00:34:36,330 But we'll start simple. 686 00:34:37,500 --> 00:34:40,370 So, the first thing that you gotta know how to 687 00:34:42,610 --> 00:34:45,100 do in Python is know what not to do. 688 00:34:45,100 --> 00:34:49,160 Or when you use these reserved words, they have very special meaning to Python. 689 00:34:50,290 --> 00:34:54,280 It's like when you say, "I don't think you're going to get any food today" to a 690 00:34:54,280 --> 00:34:56,800 dog, the dog hears the word "food" and nothing 691 00:34:56,800 --> 00:34:59,760 else. So food is a reserved word for dogs. 692 00:34:59,760 --> 00:35:01,680 Walk, bath, there are other reserved words. 693 00:35:02,720 --> 00:35:04,150 So, what it really means is you can't use 694 00:35:04,150 --> 00:35:07,350 these for anything other than what they mean to Python. 695 00:35:07,350 --> 00:35:10,550 So, print tells it to print things. 696 00:35:10,550 --> 00:35:13,250 Return is used in functions. 697 00:35:13,250 --> 00:35:19,130 Else, if, these are words that if Python sees the word if, it's like, 698 00:35:19,130 --> 00:35:20,320 this means something. 699 00:35:20,320 --> 00:35:23,060 Don't use it for any other purpose except its stated purpose. 700 00:35:23,060 --> 00:35:23,900 We'll learn what those are. 701 00:35:25,520 --> 00:35:27,210 Now if we talk about sentences. 702 00:35:27,210 --> 00:35:31,610 Sentences are kind of, in Python, like a line that kind of have pieces to them. 703 00:35:31,610 --> 00:35:34,760 So here is three pieces of code. 704 00:35:34,760 --> 00:35:36,290 One is x equals 2. 705 00:35:36,290 --> 00:35:39,560 That says, take and find me a piece of memory. 706 00:35:39,560 --> 00:35:41,160 In your RAM. 707 00:35:41,160 --> 00:35:44,130 Allocate it, label it x, and stick 2 in it. 708 00:35:44,130 --> 00:35:47,380 This is kind of like a move 2 into x. 709 00:35:47,380 --> 00:35:54,010 Then this says go get x, add 2 to it, and then put the sum back into x. 710 00:35:54,010 --> 00:35:57,070 Again, little sentences that are kind of like subject predicate, right? 711 00:35:57,070 --> 00:36:00,020 Especially this assignment, it's kind of just, and then print. 712 00:36:00,020 --> 00:36:01,680 Print's a reserved word. 713 00:36:01,680 --> 00:36:02,790 Was one of the was on the list in 714 00:36:02,790 --> 00:36:05,140 the previous slide. And then go read that variable. 715 00:36:05,140 --> 00:36:10,090 So, these are like three sentences in our new little language. 716 00:36:14,570 --> 00:36:20,080 Okay, so that's sentences, now paragraphs, let's talk about paragraphs. 717 00:36:20,080 --> 00:36:22,790 Paragraphs are the combination of sentences to make 718 00:36:22,790 --> 00:36:25,885 sort of a thought together. Multiple sentences, multiple lines. 719 00:36:25,885 --> 00:36:30,920 So, the interactive Python that I just showed you 720 00:36:30,920 --> 00:36:36,170 is fine for running one, two, or five or six commands. 721 00:36:36,170 --> 00:36:40,700 But ultimately, we're going to write much longer bits of Python. 722 00:36:40,700 --> 00:36:44,830 And so we write what's called a Python script, or a Python program. 723 00:36:44,830 --> 00:36:47,220 And we put these in a file. 724 00:36:47,220 --> 00:36:49,950 And we, and if you went through the 725 00:36:49,950 --> 00:36:53,460 prerequisite, you will see have seen me edit in a text 726 00:36:53,460 --> 00:36:57,210 editor, saved a file, and then run from the Python file. 727 00:36:58,220 --> 00:36:59,090 Okay? 728 00:36:59,090 --> 00:37:01,960 And so we call these files put .py on the end of them, P-Y on the end of them. 729 00:37:03,020 --> 00:37:06,220 And we're giving Python a script to execute. 730 00:37:08,040 --> 00:37:10,902 [COUGH] So, interactive, you're typing directly into Python 731 00:37:10,902 --> 00:37:13,360 and it's doing it right as you're talking. 732 00:37:13,360 --> 00:37:16,550 You're still doing it in order, and the order does matter. 733 00:37:16,550 --> 00:37:20,220 In a script you type it all into a file once and say Python, do it all. 734 00:37:25,700 --> 00:37:27,200 [SOUND] Now when you write one of 735 00:37:27,200 --> 00:37:30,670 these things there are patterns for combining these. 736 00:37:30,670 --> 00:37:35,870 There are things that we do to these lines that sort of treat them differently. 737 00:37:35,870 --> 00:37:38,990 Is like a recipe, a set of instructions. 738 00:37:38,990 --> 00:37:39,785 Start at the beginning. 739 00:37:39,785 --> 00:37:43,300 [SOUND] But it's a little more complex than that. 740 00:37:43,300 --> 00:37:45,460 Some steps are just sequential. 741 00:37:45,460 --> 00:37:47,560 Some steps might be skipped. 742 00:37:47,560 --> 00:37:49,800 Some steps we do multiple times. 743 00:37:49,800 --> 00:37:53,600 And other times we have kind of like a set of steps we do over and over again. 744 00:37:54,680 --> 00:37:56,730 So here's some pictures. 745 00:37:56,730 --> 00:38:01,040 And here's the four lines of Python, a little simple paragraph. 746 00:38:02,060 --> 00:38:05,100 And it's got a sentence that says, x equals 2. 747 00:38:05,100 --> 00:38:06,750 Print x. 748 00:38:06,750 --> 00:38:10,390 x equals x plus 2, which says, go grab the old value of x, add 2 to it. 749 00:38:10,390 --> 00:38:11,850 Stick it back in x. 750 00:38:11,850 --> 00:38:12,380 And print x. 751 00:38:12,380 --> 00:38:16,280 The output of this program is 2, then 4. 752 00:38:16,280 --> 00:38:18,480 because x was 2, we printed it, and we added 2 753 00:38:18,480 --> 00:38:21,570 to it, and then we printed it again so it was 4. 754 00:38:21,570 --> 00:38:24,400 Now, these flowcharts, don't worry, I'm not going to make you draw these. 755 00:38:24,400 --> 00:38:26,760 I just draw these in case, cognitively, it makes 756 00:38:26,760 --> 00:38:28,906 it easier for you to understand what's going on. 757 00:38:28,906 --> 00:38:32,750 So, x = 1. 758 00:38:32,750 --> 00:38:33,630 is the first step. 759 00:38:33,630 --> 00:38:35,140 Sequentially, it just continues on. 760 00:38:35,140 --> 00:38:36,520 It runs the print. 761 00:38:36,520 --> 00:38:38,040 x equals x plus 1. 762 00:38:38,040 --> 00:38:38,740 Runs the print. 763 00:38:38,740 --> 00:38:40,150 So this is just straight through. 764 00:38:40,150 --> 00:38:43,520 It'll make more sense when we see a little more convoluted things. 765 00:38:43,520 --> 00:38:46,100 So, this program just starts naturally. 766 00:38:46,100 --> 00:38:49,890 Python starts at the beginning and works its way down through the end. 767 00:38:49,890 --> 00:38:51,540 That's sequential stuff. 768 00:38:51,540 --> 00:38:53,790 That's the normal order of business. 769 00:38:53,790 --> 00:38:58,620 Now, a conditional is a step that may or may not get executed. 770 00:38:58,620 --> 00:39:02,370 If all we did was sequential steps, programs would be kind of dull. 771 00:39:02,370 --> 00:39:06,070 Right, they would just be like blah, blah, blah, blah, stop. 772 00:39:06,070 --> 00:39:08,710 So, there's things like oh what if you do this or 773 00:39:08,710 --> 00:39:11,520 what if you do that and so we do things like if. 774 00:39:11,520 --> 00:39:13,460 If you have more than 40 hours, I'm going to pay 775 00:39:13,460 --> 00:39:16,900 you a different rate than if I have under 40 hours. 776 00:39:16,900 --> 00:39:19,320 Those kinds of things are if, the word if. 777 00:39:20,660 --> 00:39:24,720 So in Python, the way we express this is we use the keyword if. 778 00:39:24,720 --> 00:39:29,510 So we say x equals 5, and then we say if x is less than 10. 779 00:39:29,510 --> 00:39:34,780 This is a question that's being asked, is x less than 10 or not, yes or no? 780 00:39:34,780 --> 00:39:38,060 If it is, we execute the indented bit. 781 00:39:38,060 --> 00:39:39,400 If it's not, we skip it. 782 00:39:40,860 --> 00:39:45,550 This case, since x is 5, we execute it. 783 00:39:45,550 --> 00:39:48,250 Then we come back here, and we're going to do another one. 784 00:39:48,250 --> 00:39:51,550 If x is greater than 20, well, this turns out to be false. 785 00:39:53,760 --> 00:39:55,530 So we skip that. 786 00:39:55,530 --> 00:39:57,230 So Bigger does not run. 787 00:39:57,230 --> 00:39:58,560 That line never runs. 788 00:39:58,560 --> 00:40:00,930 So, we, the output is Smaller, Finis. 789 00:40:02,990 --> 00:40:06,790 Now, here, we can take a look at it, sort of, in the picture diagram. 790 00:40:06,790 --> 00:40:08,970 We run x equals 5. 791 00:40:08,970 --> 00:40:13,840 We ask a question, this doesn't hurt x to ask the question, is x less than 10? 792 00:40:13,840 --> 00:40:16,720 The answer is yes so we kind of drive down this little path. 793 00:40:16,720 --> 00:40:19,410 We print Smaller and then we rejoin the freeway. 794 00:40:20,670 --> 00:40:25,220 Is x less than 20? No, so we skip and we continue. 795 00:40:26,250 --> 00:40:28,520 So, this never gets executed. 796 00:40:28,520 --> 00:40:30,220 So you can think of it either way. 797 00:40:30,220 --> 00:40:31,960 You can think of either sort of like 798 00:40:31,960 --> 00:40:36,020 Gestalt, say, if this true, do what's indented. 799 00:40:36,020 --> 00:40:37,560 Or you can imagine sort of a little car 800 00:40:37,560 --> 00:40:41,410 driving down a highway making turn choices as it goes. 801 00:40:41,410 --> 00:40:44,890 They're equivalent. Over time it's probably you'll just 802 00:40:44,890 --> 00:40:47,310 start seeing this and start thinking this way. 803 00:40:47,310 --> 00:40:50,400 But sometimes it helps to think about it this way, for a little while. 804 00:40:52,710 --> 00:40:53,950 Okay. 805 00:40:53,950 --> 00:40:56,280 Now, the next thing I want to show you is 806 00:40:56,280 --> 00:41:00,630 repeated steps, steps that happen over and over and over again. 807 00:41:00,630 --> 00:41:00,960 Okay? 808 00:41:00,960 --> 00:41:05,700 And that again, when I said oh, computers are good at handling a billion words. 809 00:41:05,700 --> 00:41:09,760 Well that means it has to have a loop or a repeated section, one for each word. 810 00:41:09,760 --> 00:41:11,980 It's going to do something for each word. 811 00:41:11,980 --> 00:41:12,570 And so, 812 00:41:14,830 --> 00:41:15,640 so here we go. 813 00:41:15,640 --> 00:41:20,000 And in Python, let's pick a different festive color. 814 00:41:20,000 --> 00:41:22,830 Let's pick purple as a festive color. 815 00:41:22,830 --> 00:41:26,280 So here's our program, starts at the beginning, sets the variable n to 5. 816 00:41:26,280 --> 00:41:30,550 And then a key word, reserved word while, while 817 00:41:30,550 --> 00:41:34,210 n greater than 0, again this is asking a question. 818 00:41:34,210 --> 00:41:36,670 This is asking a question. 819 00:41:36,670 --> 00:41:38,080 Is n greater than 0? 820 00:41:38,080 --> 00:41:39,130 That's a question. 821 00:41:39,130 --> 00:41:41,870 If yes we're going to do this, if no we're going to do that. 822 00:41:41,870 --> 00:41:44,170 Over here if it's true we're going to execute the 823 00:41:44,170 --> 00:41:47,630 indented part, and then come back and do it again. 824 00:41:47,630 --> 00:41:50,120 If it's false we're going to skip down. 825 00:41:50,120 --> 00:41:52,660 So it's kind of like an if, except it 826 00:41:52,660 --> 00:41:56,370 keeps doing it over and over and over again. 827 00:41:56,370 --> 00:41:58,640 So, it comes in, sets n to 5. 828 00:41:58,640 --> 00:41:59,830 Is n greater than 0? 829 00:41:59,830 --> 00:42:00,850 Yeah, sure. 830 00:42:00,850 --> 00:42:03,460 So we print n, out comes 5. 831 00:42:03,460 --> 00:42:06,580 Then it says, and n equals n minus 1, so n becomes 4. 832 00:42:06,580 --> 00:42:09,850 We can change colors. 833 00:42:09,850 --> 00:42:11,250 Then it goes back up. 834 00:42:12,920 --> 00:42:15,948 Goes back up and asks the question again. 835 00:42:15,948 --> 00:42:18,100 n is 4. 836 00:42:18,100 --> 00:42:21,170 It's still greater than 0, so it comes through. 837 00:42:21,170 --> 00:42:23,090 Prints out 4, subtracts 1. 838 00:42:23,090 --> 00:42:25,230 So n is now 3. 839 00:42:25,230 --> 00:42:27,630 Goes back up. 840 00:42:27,630 --> 00:42:28,855 Is n 0? 841 00:42:28,855 --> 00:42:30,660 Is n greater than 0? 842 00:42:30,660 --> 00:42:31,930 Yes, it is. 843 00:42:31,930 --> 00:42:33,470 Print out 3. 844 00:42:33,470 --> 00:42:33,940 Subtract 1. 845 00:42:33,940 --> 00:42:34,530 Now it's 2. 846 00:42:34,530 --> 00:42:37,513 So out come 3 and 2. 847 00:42:39,060 --> 00:42:42,130 Then it goes back up, still greater than 0. 848 00:42:42,130 --> 00:42:43,220 Yes, it is. 849 00:42:43,220 --> 00:42:44,410 Print out 2. 850 00:42:44,410 --> 00:42:45,710 Or, wait. 851 00:42:45,710 --> 00:42:46,568 Now it's 1. 852 00:42:46,568 --> 00:42:51,140 [COUGH] Now we subtract 1, it becomes 0. 853 00:42:51,140 --> 00:42:53,070 Is it greater than 0? 854 00:42:53,070 --> 00:42:54,580 No, and we finally leave. 855 00:42:56,280 --> 00:42:58,370 And we finally drop down. 856 00:42:58,370 --> 00:43:00,480 And so the last thing that comes out is the printed Blastoff. 857 00:43:02,170 --> 00:43:04,420 So this is a loop, the notion that we're 858 00:43:04,420 --> 00:43:07,585 going to run this little bit of code five times. 859 00:43:07,585 --> 00:43:09,770 [COUGH] 860 00:43:09,770 --> 00:43:10,270 Excuse me. 861 00:43:13,890 --> 00:43:16,000 We're going to run this little bit of code five times, 862 00:43:17,040 --> 00:43:20,750 And loops have these things we call iteration variables. 863 00:43:20,750 --> 00:43:22,480 And that is this n. 864 00:43:22,480 --> 00:43:25,040 It's a variable that specifically is changing 865 00:43:25,040 --> 00:43:26,870 each time it goes through the loop. 866 00:43:26,870 --> 00:43:29,010 And that way, we can sort of control the loop. 867 00:43:29,010 --> 00:43:31,340 We can decide when it starts and when it stops. 868 00:43:31,340 --> 00:43:32,730 We can tell if we're at the beginning or at 869 00:43:32,730 --> 00:43:34,710 the end, or the first one or the last one. 870 00:43:34,710 --> 00:43:36,370 We'll do a lot of stuff with loops. 871 00:43:36,370 --> 00:43:38,160 This is an iteration variable, because 872 00:43:38,160 --> 00:43:41,146 we iterate repeatedly iterate through the loop. 873 00:43:41,146 --> 00:43:42,250 Okay? 874 00:43:42,250 --> 00:43:46,654 Any questions? 875 00:43:46,654 --> 00:43:48,760 We can't do questions. 876 00:43:48,760 --> 00:43:53,210 Okay, so now, if we go back to the little 877 00:43:53,210 --> 00:43:56,830 story that I, you've got several chapters to understand, don't worry. 878 00:43:56,830 --> 00:43:59,030 You actually got like through chapter nine, so 879 00:43:59,030 --> 00:44:01,040 don't try to understand this program right now. 880 00:44:01,040 --> 00:44:03,410 I'm just trying to give you a sense of 881 00:44:03,410 --> 00:44:05,940 what the picture is going to be, right? 882 00:44:05,940 --> 00:44:12,540 So, so here are some sequential statements, because they aren't indented. 883 00:44:12,540 --> 00:44:14,990 Those five lines are sequential. 884 00:44:14,990 --> 00:44:17,120 They just go one after the other. 885 00:44:17,120 --> 00:44:20,910 Then we have for, and it's indented. 886 00:44:20,910 --> 00:44:22,050 This is a loop. 887 00:44:22,050 --> 00:44:24,380 This is going to run a bunch of times. 888 00:44:24,380 --> 00:44:25,120 Then we're done with that. 889 00:44:25,120 --> 00:44:27,420 We do some more sequential stuff. 890 00:44:27,420 --> 00:44:30,120 Now, we have a for loop, and that's going to run a bunch of times. 891 00:44:31,390 --> 00:44:35,060 And then we have an if, which may or may not run. 892 00:44:35,060 --> 00:44:36,570 So these little, this little block of 893 00:44:36,570 --> 00:44:39,760 code is conditionally executed based on something. 894 00:44:39,760 --> 00:44:41,570 And here is the question that we're asking. 895 00:44:42,680 --> 00:44:43,700 So that's the question. 896 00:44:44,860 --> 00:44:46,250 And then at the end, we do a print. 897 00:44:46,250 --> 00:44:49,340 Now, again, don't try to make too much sense of this. 898 00:44:49,340 --> 00:44:56,758 I'm just trying to show you sequential, repeated, repeated, conditional. 899 00:44:56,758 --> 00:44:57,499 Okay? 900 00:44:57,499 --> 00:45:02,110 Just, those concepts show up in every, pretty much every program that we build. 901 00:45:03,270 --> 00:45:03,770 Okay. 902 00:45:05,190 --> 00:45:08,260 So, [COUGH] let's do a couple more little exercises 903 00:45:08,260 --> 00:45:10,800 that get you sort of in the mind set 904 00:45:10,800 --> 00:45:13,560 of being a programmer, and how programmers tend to 905 00:45:13,560 --> 00:45:17,270 have to think about problems a little bit differently. 906 00:45:17,270 --> 00:45:18,410 So, here we go. 907 00:45:18,410 --> 00:45:22,250 This I call this An Animated Short Story. 908 00:45:22,250 --> 00:45:26,440 And your job, I'm going to just give you a differ, several sets of numbers. 909 00:45:26,440 --> 00:45:30,340 And I want you to find the largest number in a list of numbers. 910 00:45:31,530 --> 00:45:34,960 Now, it's not so important to know what the large 911 00:45:34,960 --> 00:45:39,150 number is, but also to think about how your mind 912 00:45:39,150 --> 00:45:40,660 attacks the problem. 913 00:45:40,660 --> 00:45:42,040 What your eyes are doing. 914 00:45:42,040 --> 00:45:43,040 What your mind is doing. 915 00:45:43,040 --> 00:45:46,440 How you break a bigger problem down into smaller problems. 916 00:45:46,440 --> 00:45:48,840 How a human solves this problem. 917 00:45:48,840 --> 00:45:51,000 And then we'll focus on how a computer 918 00:45:51,000 --> 00:45:53,230 might have to look at the problem differently, okay? 919 00:45:53,230 --> 00:45:55,590 So don't just, like, get the answer. 920 00:45:55,590 --> 00:45:56,760 That's not so important. 921 00:45:56,760 --> 00:45:58,650 Think about how you get the answer. 922 00:45:58,650 --> 00:46:02,900 So, don't just like scroll ahead in your YouTube and cheat and go get the answer. 923 00:46:02,900 --> 00:46:05,480 Think about actually solving the problem, and then 924 00:46:05,480 --> 00:46:08,480 monitor what your brain is thinking as it goes. 925 00:46:08,480 --> 00:46:09,450 So, here we go. 926 00:46:09,450 --> 00:46:12,380 So, I'm going to give you a list of numbers, 927 00:46:12,380 --> 00:46:16,020 and you are to tell me what the largest number is. 928 00:46:16,020 --> 00:46:16,424 Ready? 929 00:46:16,424 --> 00:46:18,176 Set, go. 930 00:46:18,176 --> 00:46:28,176 [BLANK_AUDIO] 931 00:46:29,334 --> 00:46:33,615 [SOUND] I didn't make it easy, you're looking for the largest number. 932 00:46:33,615 --> 00:46:43,535 [BLANK_AUDIO] 933 00:46:43,535 --> 00:46:46,832 Did you get it? 934 00:46:46,832 --> 00:46:50,220 Did you have to go back a couple of times? 935 00:46:50,220 --> 00:46:52,190 Actually I don't care what the answer is. 936 00:46:52,190 --> 00:46:54,030 The question is 937 00:46:54,030 --> 00:46:55,650 How was your brain solving? 938 00:46:55,650 --> 00:46:57,890 Okay, you probably want to know what it is. 939 00:46:57,890 --> 00:47:01,340 The answer is 198, that was the largest number. 940 00:47:01,340 --> 00:47:03,770 Of course what I was doing is I was moving it to make it difficult. 941 00:47:04,900 --> 00:47:10,060 But here's the thing, how do humans look at this? 942 00:47:10,060 --> 00:47:14,270 Like, do humans, like, did you look at 25, then you looked at 1, then you looked 943 00:47:14,270 --> 00:47:19,090 at 114, and then did you just look at them slowly one at a time, like this? 944 00:47:19,090 --> 00:47:20,540 Or no, I doubt it. 945 00:47:20,540 --> 00:47:23,410 If you are, maybe you're a computer. 946 00:47:23,410 --> 00:47:26,080 Maybe I'm talking to computers, maybe you're all computers. 947 00:47:26,080 --> 00:47:28,210 I'm certainly not a computer, maybe you're all computers. 948 00:47:28,210 --> 00:47:30,160 Okay, enough of that. 949 00:47:30,160 --> 00:47:31,930 No, that's probably not how you did it. 950 00:47:33,150 --> 00:47:35,010 What you probably did was you had your 951 00:47:35,010 --> 00:47:38,290 eyes move around the whole thing very rapidly. 952 00:47:38,290 --> 00:47:41,550 And the first thing that you figured out is that there 953 00:47:41,550 --> 00:47:46,110 were one-digit blobs, there were small, medium, and large blobs of purple. 954 00:47:47,250 --> 00:47:50,660 And the first thing you knew, right away, was there 955 00:47:50,660 --> 00:47:55,230 was no point at looking at any of the small blobs. 956 00:47:55,230 --> 00:47:58,680 Your brain just threw the blobs away really quick. 957 00:47:58,680 --> 00:48:00,340 Then you say, okay, given that it, 958 00:48:00,340 --> 00:48:03,200 there's no four-digit numbers, they're three-digit numbers. 959 00:48:03,200 --> 00:48:06,390 Then what you probably did is you started looking for the first digit. 960 00:48:06,390 --> 00:48:08,170 You say, look, there's some ones. 961 00:48:08,170 --> 00:48:09,830 Is there any twos? 962 00:48:09,830 --> 00:48:12,930 Quickly, you decided there are no twos. 963 00:48:12,930 --> 00:48:15,580 So you knew that you had to look for the big blobs. 964 00:48:15,580 --> 00:48:18,460 And the second digit was probably the thing that mattered. 965 00:48:18,460 --> 00:48:22,410 Then you start getting to the nine, you say, okay, there's some nines. 966 00:48:22,410 --> 00:48:24,470 So that means it's, it's one, nine, something. 967 00:48:25,570 --> 00:48:27,270 Then that was the part that you probably had to go 968 00:48:27,270 --> 00:48:32,520 check to find the, oh, where the heck was the 190, aah! 969 00:48:34,360 --> 00:48:37,200 Oh, 198 right there, [LAUGH] yeah I color 970 00:48:37,200 --> 00:48:40,250 coded it, and I couldn't even see it, okay? 971 00:48:40,250 --> 00:48:43,850 But the point is, is, humans are great 972 00:48:43,850 --> 00:48:48,500 at eliminating sort of bad solutions, really fast. 973 00:48:48,500 --> 00:48:51,850 And you probably looked at how big, how much purple was on 974 00:48:51,850 --> 00:48:55,980 the screen, eliminating the areas that were less purple, because you knew that 975 00:48:55,980 --> 00:48:59,350 your brain quickly and instinctively knew that 976 00:48:59,350 --> 00:49:02,840 the more purple meant a larger number. 977 00:49:04,110 --> 00:49:08,610 Computers don't do any of that, they don't do any of that. 978 00:49:08,610 --> 00:49:15,229 So, in order to make you feel a little more like a computer, I have another test. 979 00:49:16,960 --> 00:49:21,430 And again, the goal is not just to find the largest number. 980 00:49:21,430 --> 00:49:28,620 But to monitor as you go, what your brain is thinking, while you're doing this, okay? 981 00:49:28,620 --> 00:49:30,450 Do you get it? 982 00:49:31,660 --> 00:49:34,160 How are you attacking the problem, what is 983 00:49:34,160 --> 00:49:36,470 your feeling as you're attacking the problem? 984 00:49:36,470 --> 00:49:38,970 Are you a computer or not? 985 00:49:38,970 --> 00:49:40,931 Here we go. 986 00:49:40,931 --> 00:49:42,347 I'm only going to give you a few seconds. 987 00:49:42,347 --> 00:49:50,142 [BLANK_AUDIO] 988 00:49:50,142 --> 00:49:53,153 [SOUND] 989 00:49:59,153 --> 00:50:02,606 So, what did 990 00:50:02,606 --> 00:50:05,860 you get? 991 00:50:08,230 --> 00:50:13,090 My guess is that most of you just said, I don't care. 992 00:50:14,110 --> 00:50:16,490 This is such a hard problem. 993 00:50:16,490 --> 00:50:17,870 It's a stupid problem. 994 00:50:17,870 --> 00:50:20,950 Or I'll try and turn my head upside down, or something. 995 00:50:20,950 --> 00:50:22,660 It's a really hard problem. 996 00:50:22,660 --> 00:50:24,550 The other one was kind of easy. 997 00:50:24,550 --> 00:50:26,130 Not that you, you might not have got it, but you 998 00:50:26,130 --> 00:50:29,910 had this natural instinct that allowed you to approach the problem. 999 00:50:29,910 --> 00:50:32,300 Okay, I'll show you what the right answer is. 1000 00:50:32,300 --> 00:50:33,400 The right answer is. 1001 00:50:36,810 --> 00:50:40,870 Right there is 197. 1002 00:50:40,870 --> 00:50:42,290 Yay. 1003 00:50:42,290 --> 00:50:44,790 Right. 1004 00:50:44,950 --> 00:50:45,780 I, you can't even, even if I tell you to 1005 00:50:45,780 --> 00:50:48,610 you know there you are you know, what is this? 1006 00:50:48,610 --> 00:50:55,120 Is this 500 or two zero zero. 1007 00:50:55,120 --> 00:50:57,780 Actually, the only way I can do this is I flip it to find it. 1008 00:50:57,780 --> 00:50:59,820 I mean it's just not what humans are good at. 1009 00:51:01,460 --> 00:51:04,530 This is a little bit more like how computers see the world. 1010 00:51:04,530 --> 00:51:06,910 But the fact that the data's frontwards or 1011 00:51:06,910 --> 00:51:10,060 backwards should sort of make no difference, right? 1012 00:51:10,060 --> 00:51:15,700 Computers need a strategy, we need to give them a strategy. 1013 00:51:15,700 --> 00:51:16,200 Okay. 1014 00:51:17,470 --> 00:51:18,075 So here we go. 1015 00:51:18,075 --> 00:51:26,727 [COUGH] One last experiment. 1016 00:51:26,727 --> 00:51:30,320 Now I'm going to show you numbers one at a time. 1017 00:51:30,320 --> 00:51:35,304 And at the end, I want you to tell me what the largest number that you saw was. 1018 00:51:35,304 --> 00:51:37,963 Ready? 1019 00:51:37,963 --> 00:51:39,823 Here we go. 1020 00:51:39,823 --> 00:51:41,890 First number. 1021 00:51:52,890 --> 00:51:54,469 What was the largest number? 1022 00:51:58,100 --> 00:51:58,600 As a matter of fact, 1023 00:52:02,290 --> 00:52:04,550 how did you solve that problem? 1024 00:52:05,820 --> 00:52:10,560 You solved that problem most likely because you didn't, you couldn't look 1025 00:52:10,560 --> 00:52:13,980 at all the numbers at the same time, so you probably created 1026 00:52:13,980 --> 00:52:16,280 a variable in your head without even knowing. 1027 00:52:17,430 --> 00:52:20,050 And you put into that variable. 1028 00:52:20,050 --> 00:52:23,390 You call the variable, the largest number I've seen so far. 1029 00:52:25,100 --> 00:52:27,520 And you haven't seen any, so let's say the 1030 00:52:27,520 --> 00:52:29,920 largest number you've seen so far is negative 1. 1031 00:52:29,920 --> 00:52:34,460 Then I show you 3 and you said to yourself. 1032 00:52:34,460 --> 00:52:34,960 Well. 1033 00:52:36,260 --> 00:52:39,160 Negative 1 is no longer the largest number I've seen. 1034 00:52:39,160 --> 00:52:40,900 So I'm going to keep that one, I'll keep 3. 1035 00:52:40,900 --> 00:52:42,370 That's the largest I've seen so far. 1036 00:52:42,370 --> 00:52:47,730 And now I see 41, 41 is larger than 3 so I will keep that. 1037 00:52:47,730 --> 00:52:54,090 And now I see 12, now 12 is crap because it's nowhere near as good as 41. 1038 00:52:54,090 --> 00:52:55,600 So I'm keeping 41. 1039 00:52:55,600 --> 00:52:56,475 74. Oh, 9. 1040 00:52:56,475 --> 00:53:03,160 9, not nearly as good as 41, so I'm gonna throw that one away. 1041 00:53:03,160 --> 00:53:04,570 74, better, better. 1042 00:53:04,570 --> 00:53:05,190 Keep it. 1043 00:53:05,190 --> 00:53:06,120 Keep that one. 1044 00:53:06,120 --> 00:53:07,130 So I'll keep 74. 1045 00:53:07,130 --> 00:53:09,310 And the last number is 15. 1046 00:53:09,310 --> 00:53:13,120 Don't even know it's the last number, but, we don't want to keep that one. 1047 00:53:13,120 --> 00:53:14,470 And so now, we're done. 1048 00:53:15,600 --> 00:53:21,970 So we know that at the end, what was during the loop the largest so far is 1049 00:53:21,970 --> 00:53:23,850 the actual largest of all the numbers, and 1050 00:53:23,850 --> 00:53:26,980 we don't remember exactly how many numbers there were. 1051 00:53:26,980 --> 00:53:29,380 So that's kind of like thinking like a 1052 00:53:29,380 --> 00:53:31,200 program, you know, this kind of sliding window. 1053 00:53:32,230 --> 00:53:35,580 Didn't matter if I gave you a billion numbers or five numbers. 1054 00:53:35,580 --> 00:53:37,920 I think there were five numbers, actually. 1055 00:53:37,920 --> 00:53:40,320 This notion of the largest so far is a powerful notion. 1056 00:53:40,320 --> 00:53:44,900 As a matter of fact, it's central to the program I've been showing you. 1057 00:53:44,900 --> 00:53:50,280 And I don't want you to try to understand this, but this part in the purple. 1058 00:53:52,470 --> 00:53:56,710 This part in the purple is really saying, 1059 00:53:56,710 --> 00:53:59,410 I'm going to loop through the counts of all the words. 1060 00:53:59,410 --> 00:54:05,310 So it's got a word like "the" is 15 times, and "clown" is four times. 1061 00:54:05,310 --> 00:54:09,590 And it's going to look through all the pairs of word-value combinations. 1062 00:54:09,590 --> 00:54:11,000 And it's going to basically say, I'm 1063 00:54:11,000 --> 00:54:13,860 going to go through the counts that I have. 1064 00:54:13,860 --> 00:54:15,590 And I'm going to check to see if the count I'm looking at 1065 00:54:15,590 --> 00:54:19,730 is bigger than the biggest count I've seen so far. 1066 00:54:19,730 --> 00:54:22,330 And if it is, I'm going to remember it. 1067 00:54:22,330 --> 00:54:23,490 Now, don't worry about this. 1068 00:54:23,490 --> 00:54:24,960 We haven't even covered any of this stuff. 1069 00:54:24,960 --> 00:54:27,700 That's what chapter two, three, four, five, six. 1070 00:54:27,700 --> 00:54:33,250 But this is an algorithm, a paragraph, a pattern. 1071 00:54:33,250 --> 00:54:36,430 That allows you to find the largest number, and we'll 1072 00:54:36,430 --> 00:54:40,270 look at this again in great detail in upcoming chapters. 1073 00:54:40,270 --> 00:54:42,680 So this is kind of thinking like 1074 00:54:42,680 --> 00:54:45,960 a computer, having a sliding window across the 1075 00:54:45,960 --> 00:54:48,970 long list of numbers, and coming up with 1076 00:54:48,970 --> 00:54:52,370 something that is the answer that you need. 1077 00:54:55,090 --> 00:54:58,570 Okay, so that's the end of this lecture. 1078 00:54:58,570 --> 00:55:02,080 Read Chapter One, write your Hello World program. 1079 00:55:02,080 --> 00:55:05,560 Make sure if you haven't, get Python installed. 1080 00:55:05,560 --> 00:55:08,470 As you read this chapter and even as you 1081 00:55:08,470 --> 00:55:11,930 install Python, and even as you write the first program, 1082 00:55:11,930 --> 00:55:14,240 don't get too stuck on the details. 1083 00:55:15,340 --> 00:55:18,410 I was confused for like eight weeks or six 1084 00:55:18,410 --> 00:55:22,490 weeks in my first programming class; you'll be confused too. 1085 00:55:22,490 --> 00:55:24,870 Just sort of wander through with me, keep at it. 1086 00:55:25,880 --> 00:55:28,690 It will start making sense at some point that's up 1087 00:55:28,690 --> 00:55:31,900 to you, I can't tell you when it's going to make sense. 1088 00:55:31,900 --> 00:55:35,430 So, if, don't sort of stare at everything until you get it. 1089 00:55:35,430 --> 00:55:40,090 Just kind of keep digging in, and keep understanding, and keep playing and 1090 00:55:40,090 --> 00:55:44,240 sooner or later this'll make a lot of sense to you, I promise you. 1091 00:55:44,240 --> 00:55:45,010 See you next lecture.