[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:03.76,0:00:08.24,Default,,0000,0000,0000,,As always, this lecture is copyright\NCreative Commons Attribution, including Dialogue: 0,0:00:08.24,0:00:12.68,Default,,0000,0000,0000,,the audio and video and, and the\Nslides, and the book even. Dialogue: 0,0:00:12.68,0:00:18.80,Default,,0000,0000,0000,,So, now we're getting to our\Nfourth basic pattern. Dialogue: 0,0:00:18.80,0:00:22.18,Default,,0000,0000,0000,,We've talked about sequential, where\Nsteps happen one after another. Dialogue: 0,0:00:22.18,0:00:25.27,Default,,0000,0000,0000,,We've talked about conditional, where\Nsteps may or may not happen. Dialogue: 0,0:00:25.27,0:00:27.58,Default,,0000,0000,0000,,In Chapter Four we talked about\Nthe store and retrieve pattern. Dialogue: 0,0:00:27.58,0:00:29.75,Default,,0000,0000,0000,,And now we're going to talk\Nabout the looping pattern. Dialogue: 0,0:00:29.75,0:00:33.82,Default,,0000,0000,0000,,And the looping pattern is the last\Nof our really foundational ones, and Dialogue: 0,0:00:33.82,0:00:38.84,Default,,0000,0000,0000,,it potentially is the most important one,\Nbecause it's the thing that allows us Dialogue: 0,0:00:38.84,0:00:41.54,Default,,0000,0000,0000,,to get computers to do lots of\Nthings that, say, Dialogue: 0,0:00:41.54,0:00:45.77,Default,,0000,0000,0000,,humans might get tired of,\Nbut computers don't tire of. Dialogue: 0,0:00:45.77,0:00:50.46,Default,,0000,0000,0000,,And so after this we'll start\Nsort of becoming a little more Dialogue: 0,0:00:50.46,0:00:52.71,Default,,0000,0000,0000,,skilled in the basic language\Ncapabilities. Dialogue: 0,0:00:52.71,0:00:56.12,Default,,0000,0000,0000,,We'll talk about strings and, and\Nthen start to talk about files. Dialogue: 0,0:00:56.12,0:01:00.83,Default,,0000,0000,0000,,Add start doing some real work after we\Nget done with this, so bear with us. Dialogue: 0,0:01:00.83,0:01:03.38,Default,,0000,0000,0000,,It's going to be a while,\Nbut we'll get there. Dialogue: 0,0:01:03.38,0:01:07.05,Default,,0000,0000,0000,,We'll get there.\NSo, welcome to repeated steps. Dialogue: 0,0:01:07.05,0:01:12.76,Default,,0000,0000,0000,,This is the example that I had in the\Nfirst, first lecture, Chapter 1. Dialogue: 0,0:01:12.76,0:01:15.31,Default,,0000,0000,0000,,And the basic idea, just to review, Dialogue: 0,0:01:15.31,0:01:18.15,Default,,0000,0000,0000,,is that you have this while keyword. Dialogue: 0,0:01:18.15,0:01:20.62,Default,,0000,0000,0000,,The while keyword sort of\Nfunctions like an if in that Dialogue: 0,0:01:20.62,0:01:23.89,Default,,0000,0000,0000,,it implicitly has a decision that\Nit's going to make. Dialogue: 0,0:01:23.89,0:01:26.89,Default,,0000,0000,0000,,And it's either going to do\Nthe code in the Dialogue: 0,0:01:26.89,0:01:29.86,Default,,0000,0000,0000,,indented block or not do it,\Nor skip it basically. Dialogue: 0,0:01:29.86,0:01:33.02,Default,,0000,0000,0000,,Alright?\NSo you either do it or you skip it. Dialogue: 0,0:01:33.02,0:01:36.28,Default,,0000,0000,0000,,The difference between the while\Nand the if is that it's going Dialogue: 0,0:01:36.28,0:01:40.78,Default,,0000,0000,0000,,to do it many times, as long as this\Nquestion that we have remains true. Dialogue: 0,0:01:40.78,0:01:42.50,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:01:42.50,0:01:48.10,Default,,0000,0000,0000,,And so, in this case, n is 5, while n\Ngreater than 0 functions like an if. Dialogue: 0,0:01:48.10,0:01:52.85,Default,,0000,0000,0000,,So yes, it's going to run it.\NPrints out 5, subtracts 1, so it's 4. Dialogue: 0,0:01:52.85,0:01:54.16,Default,,0000,0000,0000,,Goes back up. Dialogue: 0,0:01:54.16,0:01:58.10,Default,,0000,0000,0000,,Goes back up and asks the question again,\Nis n still greater than 0? Dialogue: 0,0:01:58.10,0:02:00.83,Default,,0000,0000,0000,,Well, since it's 4, yes,\Nwe'll continue on. Dialogue: 0,0:02:00.83,0:02:05.82,Default,,0000,0000,0000,,Out comes 4 then n gets subtracted.\N3, 2, 3, 2, and then Dialogue: 0,0:02:05.82,0:02:09.58,Default,,0000,0000,0000,,we come through, we print 1.\NPrint 1. Dialogue: 0,0:02:09.58,0:02:13.23,Default,,0000,0000,0000,,We subtract n to 0. We go up,\Nwe go back up. Dialogue: 0,0:02:13.23,0:02:16.94,Default,,0000,0000,0000,,n is now not greater than 0, so we come Dialogue: 0,0:02:16.94,0:02:20.73,Default,,0000,0000,0000,,up and we execute outside the loop,\Nwe leave the loop. Dialogue: 0,0:02:20.73,0:02:22.63,Default,,0000,0000,0000,,And that really means in the\NPython code we Dialogue: 0,0:02:22.63,0:02:26.36,Default,,0000,0000,0000,,skip to whatever is lined with\Nthe while statement. Dialogue: 0,0:02:26.36,0:02:29.29,Default,,0000,0000,0000,,The same indent level as\Nthe while statement. Dialogue: 0,0:02:29.29,0:02:31.26,Default,,0000,0000,0000,,And so that's how it works, and Dialogue: 0,0:02:31.26,0:02:36.05,Default,,0000,0000,0000,,I just print n at the end here to remind\Nourselves that n ended up at 0, not at 1, Dialogue: 0,0:02:36.05,0:02:38.93,Default,,0000,0000,0000,,the last thing we printed out\Nin the loop. The last thing we Dialogue: 0,0:02:38.93,0:02:42.80,Default,,0000,0000,0000,,printed out in the loop was the 1,\Nbut n ended up at 0 because Dialogue: 0,0:02:42.80,0:02:45.67,Default,,0000,0000,0000,,it was, this loop was going to run\Nas long as n was greater than 0, Dialogue: 0,0:02:45.67,0:02:49.83,Default,,0000,0000,0000,,so n had to sort of be not greater\Nthan 0 to get out of the loop. Dialogue: 0,0:02:49.83,0:02:50.38,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:02:50.38,0:02:52.61,Default,,0000,0000,0000,,So that's basically a \Nreview of what we've done. Dialogue: 0,0:02:54.00,0:02:56.17,Default,,0000,0000,0000,,Now, oh, wait, wait, wait, wait, Dialogue: 0,0:02:56.17,0:02:59.46,Default,,0000,0000,0000,,wait, something else.\NIteration variables. Dialogue: 0,0:03:00.74,0:03:04.44,Default,,0000,0000,0000,,Okay, so the key to this is \Nthese loops can't run forever. Dialogue: 0,0:03:04.44,0:03:06.01,Default,,0000,0000,0000,,We don't want them to run forever. Dialogue: 0,0:03:06.01,0:03:08.31,Default,,0000,0000,0000,,We want them to run until,\Nas long as we want them to run. Dialogue: 0,0:03:08.31,0:03:11.57,Default,,0000,0000,0000,,They may run a very long time,\Nbut not forever. Dialogue: 0,0:03:11.57,0:03:13.30,Default,,0000,0000,0000,,There's got to be a way to get\Nout of them, otherwise we Dialogue: 0,0:03:13.30,0:03:15.95,Default,,0000,0000,0000,,call them infinite loops, which\Nwe'll talk about in the next slide. Dialogue: 0,0:03:15.95,0:03:19.95,Default,,0000,0000,0000,,And so the iteration variable\Nis generally some variable Dialogue: 0,0:03:19.95,0:03:21.30,Default,,0000,0000,0000,,that is changing each Dialogue: 0,0:03:21.30,0:03:26.00,Default,,0000,0000,0000,,time through the loop, and we're changing\Nit by subtracting 1 to it, from it. Dialogue: 0,0:03:26.00,0:03:28.23,Default,,0000,0000,0000,,And so this thing is going to keep\Nrunning, and we Dialogue: 0,0:03:28.23,0:03:30.98,Default,,0000,0000,0000,,can pretty much see that Oh, this is\Ngoing to exit, right? Dialogue: 0,0:03:30.98,0:03:34.77,Default,,0000,0000,0000,,Whatever n is, it could be a large number,\Nbut eventually it's going to get to 0. Dialogue: 0,0:03:34.77,0:03:35.29,Default,,0000,0000,0000,,Right? Dialogue: 0,0:03:35.29,0:03:40.72,Default,,0000,0000,0000,,So the iteration variable controls how\Nmany times the loop runs. Dialogue: 0,0:03:40.72,0:03:43.54,Default,,0000,0000,0000,,And it also allows us to do something\Ndifferent inside the loop. Dialogue: 0,0:03:43.54,0:03:44.87,Default,,0000,0000,0000,,And, of course, this is like a trivial Dialogue: 0,0:03:44.87,0:03:47.42,Default,,0000,0000,0000,,loop, where we're just printing the\Niteration variable. Dialogue: 0,0:03:47.42,0:03:50.10,Default,,0000,0000,0000,,But it just means that this\Nloop is going to run Dialogue: 0,0:03:50.10,0:03:53.73,Default,,0000,0000,0000,,five times and it's going to do something\Npotentially different each time. Dialogue: 0,0:03:53.73,0:03:55.69,Default,,0000,0000,0000,,If you just ran the loop that\Ndid the same thing over Dialogue: 0,0:03:55.69,0:03:58.40,Default,,0000,0000,0000,,and over and over again, with no data\Nchanging, that's kind of dull and pointless. Dialogue: 0,0:03:58.40,0:04:01.15,Default,,0000,0000,0000,,So just because you have an\Niteration variable Dialogue: 0,0:04:01.15,0:04:05.51,Default,,0000,0000,0000,,doesn't mean that you've properly\Nconstructed your loop. Dialogue: 0,0:04:05.51,0:04:09.25,Default,,0000,0000,0000,,It's a, it's a common problem or\Nsomething we want to avoid, Dialogue: 0,0:04:09.25,0:04:13.01,Default,,0000,0000,0000,,is an infinite loop. And here is a, a\Ncarefully constructed loop. Dialogue: 0,0:04:13.01,0:04:15.54,Default,,0000,0000,0000,,We start n at 5 at the beginning. Dialogue: 0,0:04:15.54,0:04:18.77,Default,,0000,0000,0000,,We have a good question at the end,\Nwhile n greater than 0. Dialogue: 0,0:04:18.77,0:04:20.12,Default,,0000,0000,0000,,It's going to run this Dialogue: 0,0:04:20.12,0:04:22.04,Default,,0000,0000,0000,,as long as n is greater than 0. Dialogue: 0,0:04:23.10,0:04:25.74,Default,,0000,0000,0000,,But the problem is, is we don't\Nchange in the little block. Dialogue: 0,0:04:25.74,0:04:27.44,Default,,0000,0000,0000,,We don't change the n. Dialogue: 0,0:04:27.44,0:04:29.81,Default,,0000,0000,0000,,Which means it's going to come back\Nand n is going to be 5. Dialogue: 0,0:04:29.81,0:04:31.84,Default,,0000,0000,0000,,And it's going to run this and\Nn is going to be 5. Dialogue: 0,0:04:31.84,0:04:34.10,Default,,0000,0000,0000,,And it's going to run this and\Nn is going to be 5. Dialogue: 0,0:04:34.10,0:04:36.93,Default,,0000,0000,0000,,And so this is an infinite loop, which\Nmeans this loop will never exit. Dialogue: 0,0:04:36.93,0:04:38.53,Default,,0000,0000,0000,,It will never get out, Dialogue: 0,0:04:39.60,0:04:43.31,Default,,0000,0000,0000,,it's just going to run forever in here,\Nbecause n is not changing. Dialogue: 0,0:04:43.31,0:04:45.25,Default,,0000,0000,0000,,Neither of these statements change n. Dialogue: 0,0:04:45.25,0:04:49.58,Default,,0000,0000,0000,,So part of the iteration variable is there\Nneeds to be something that changes Dialogue: 0,0:04:49.58,0:04:51.87,Default,,0000,0000,0000,,so that the loop will ultimately\Nmake progress to Dialogue: 0,0:04:51.87,0:04:54.21,Default,,0000,0000,0000,,accomplish what it is and\Nknow when to stop. Dialogue: 0,0:04:54.21,0:04:56.50,Default,,0000,0000,0000,,So this is an infinite loop\Nand, of course, Dialogue: 0,0:04:56.50,0:05:01.47,Default,,0000,0000,0000,,Lather, Rinse, Repeat is commonly put on\Nshampoo and conditioner. Dialogue: 0,0:05:01.47,0:05:03.52,Default,,0000,0000,0000,,And so you know, you can,\Nnext time you are Dialogue: 0,0:05:03.52,0:05:04.76,Default,,0000,0000,0000,,in the shower, take a look at\Nyour shampoo and Dialogue: 0,0:05:04.76,0:05:07.90,Default,,0000,0000,0000,,conditioner and find the\Ninfinite loop that's, Dialogue: 0,0:05:07.90,0:05:12.12,Default,,0000,0000,0000,,that's on most bottles of\Nshampoo and conditioner. Dialogue: 0,0:05:14.35,0:05:15.39,Default,,0000,0000,0000,,Now here is another loop, Dialogue: 0,0:05:17.97,0:05:20.46,Default,,0000,0000,0000,,just to emphasize that it's\Npossible to structure Dialogue: 0,0:05:20.46,0:05:22.81,Default,,0000,0000,0000,,these loops in a way that they never run. Dialogue: 0,0:05:22.81,0:05:24.99,Default,,0000,0000,0000,,So this function is, is an if. Dialogue: 0,0:05:24.99,0:05:27.51,Default,,0000,0000,0000,,The while functions as an if. Dialogue: 0,0:05:27.51,0:05:31.72,Default,,0000,0000,0000,,And so when n is set to 0 and\Nwe ask the question, Dialogue: 0,0:05:31.72,0:05:34.59,Default,,0000,0000,0000,,it is literally going to make the\Ndecision based on n greater than 0. Dialogue: 0,0:05:34.59,0:05:38.78,Default,,0000,0000,0000,,Well it is not greater than 0, so it's\Ngoing to go right by it, over Dialogue: 0,0:05:38.78,0:05:43.27,Default,,0000,0000,0000,,here it's going to come in here and go\Nright to there and never run these Dialogue: 0,0:05:43.27,0:05:46.85,Default,,0000,0000,0000,,lines of code. And that's, we\Ncall this a zero trip loop. Dialogue: 0,0:05:48.99,0:05:51.85,Default,,0000,0000,0000,,And that's okay.\NI mean, this is a silly one, of course. Dialogue: 0,0:05:53.57,0:05:57.50,Default,,0000,0000,0000,,It just shows that the test, the\Nquestion that's being asked is Dialogue: 0,0:05:57.50,0:06:00.66,Default,,0000,0000,0000,,above the lines that make up\Nthe body of the loop. Dialogue: 0,0:06:00.66,0:06:03.27,Default,,0000,0000,0000,,And if it's false, the loop never runs. Dialogue: 0,0:06:03.27,0:06:06.65,Default,,0000,0000,0000,,So it's possible that these loops\Nhave zero trips. Dialogue: 0,0:06:06.65,0:06:07.69,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:06:07.69,0:06:09.10,Default,,0000,0000,0000,,So, that's a loop. Dialogue: 0,0:06:09.10,0:06:14.31,Default,,0000,0000,0000,,Now, there are more than one way\Nto sort of control the flow Dialogue: 0,0:06:14.31,0:06:15.68,Default,,0000,0000,0000,,of a loop. Dialogue: 0,0:06:15.68,0:06:17.54,Default,,0000,0000,0000,,The basic flow of the loop is\Nwhen it gets to the Dialogue: 0,0:06:17.54,0:06:20.57,Default,,0000,0000,0000,,bottom, it goes back up to the while,\Nand does the check. Dialogue: 0,0:06:20.57,0:06:22.24,Default,,0000,0000,0000,,This is a different way of getting out of Dialogue: 0,0:06:22.24,0:06:24.69,Default,,0000,0000,0000,,a loop or controlling the execution\Nof a loop. Dialogue: 0,0:06:24.69,0:06:28.06,Default,,0000,0000,0000,,There is a keyword or a part of\Nthe Python language called... Dialogue: 0,0:06:29.63,0:06:31.25,Default,,0000,0000,0000,,What color do I got? Dialogue: 0,0:06:31.25,0:06:34.46,Default,,0000,0000,0000,,No, green's over here.\NCalled break. Dialogue: 0,0:06:34.46,0:06:38.51,Default,,0000,0000,0000,,If you look back at the reserved words,\Nbreak was one of the reserved words. Dialogue: 0,0:06:38.51,0:06:39.45,Default,,0000,0000,0000,,Break says, Dialogue: 0,0:06:39.45,0:06:42.92,Default,,0000,0000,0000,,hey, if I'm in a loop, stop the loop.\NAll right? Dialogue: 0,0:06:42.92,0:06:43.74,Default,,0000,0000,0000,,Get out of this loop. Dialogue: 0,0:06:43.74,0:06:45.55,Default,,0000,0000,0000,,I'm done with this loop. Dialogue: 0,0:06:45.55,0:06:47.56,Default,,0000,0000,0000,,And so here's this loop. Dialogue: 0,0:06:47.56,0:06:49.42,Default,,0000,0000,0000,,Now the interesting thing we've\Ndone is I've Dialogue: 0,0:06:49.42,0:06:51.11,Default,,0000,0000,0000,,just got done talking to you about\Ninfinite loops. Dialogue: 0,0:06:51.11,0:06:54.76,Default,,0000,0000,0000,,We have just constructed an\N"infinite loop", because the Dialogue: 0,0:06:54.76,0:06:58.18,Default,,0000,0000,0000,,question is right there, and\Nthe answer is yes. Dialogue: 0,0:06:58.18,0:06:58.93,Default,,0000,0000,0000,,True. Dialogue: 0,0:06:58.93,0:06:59.28,Default,,0000,0000,0000,,True. Dialogue: 0,0:06:59.28,0:07:01.50,Default,,0000,0000,0000,,And that's a way to construct\Nan infinite loop. Dialogue: 0,0:07:01.50,0:07:03.14,Default,,0000,0000,0000,,We've done this because we have a\Ndifferent Dialogue: 0,0:07:03.14,0:07:04.49,Default,,0000,0000,0000,,way of getting out of the loop, so we've Dialogue: 0,0:07:04.49,0:07:07.06,Default,,0000,0000,0000,,constructed a loop that,\Njust on the face of it, Dialogue: 0,0:07:07.06,0:07:10.05,Default,,0000,0000,0000,,just looking at that line, looks\Nlike an infinite loop. Dialogue: 0,0:07:10.05,0:07:13.35,Default,,0000,0000,0000,,So what this loop does is it\Nreads a line of input, Dialogue: 0,0:07:13.35,0:07:17.32,Default,,0000,0000,0000,,checks to see if the string that\Nwe've entered is done. Dialogue: 0,0:07:17.32,0:07:20.74,Default,,0000,0000,0000,,And if it is, we're going to\Nskip out with this break Dialogue: 0,0:07:20.74,0:07:23.17,Default,,0000,0000,0000,,and get out of the loop, otherwise\Nwe're going to print it. Dialogue: 0,0:07:23.17,0:07:24.95,Default,,0000,0000,0000,,So at a high level, what this loop is Dialogue: 0,0:07:24.95,0:07:28.51,Default,,0000,0000,0000,,going to do is prompt for\Nstrings of characters Dialogue: 0,0:07:28.51,0:07:30.00,Default,,0000,0000,0000,,until we enter done. Dialogue: 0,0:07:30.00,0:07:31.14,Default,,0000,0000,0000,,And that's exactly what it does. Dialogue: 0,0:07:31.14,0:07:33.10,Default,,0000,0000,0000,,It prompts, we say hello there,\Nit prints that out. Dialogue: 0,0:07:33.10,0:07:35.29,Default,,0000,0000,0000,,We say, we say finished, it\Nprints that out. Dialogue: 0,0:07:35.29,0:07:37.03,Default,,0000,0000,0000,,We say done, and it's done. Dialogue: 0,0:07:37.03,0:07:39.70,Default,,0000,0000,0000,,So it, when we say done, it comes out and Dialogue: 0,0:07:39.70,0:07:42.76,Default,,0000,0000,0000,,finishes the loop, and, and that's\Nthe end of the program. Dialogue: 0,0:07:42.76,0:07:43.54,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:07:43.54,0:07:46.15,Default,,0000,0000,0000,,So, to look at this in some more detail. Dialogue: 0,0:07:48.20,0:07:51.78,Default,,0000,0000,0000,,The first time, it comes in,\Ndoes the raw input. Dialogue: 0,0:07:51.78,0:07:54.04,Default,,0000,0000,0000,,Because True is true, so it's\Ngoing to run it. Dialogue: 0,0:07:54.04,0:07:57.60,Default,,0000,0000,0000,,And then we enter hello there. Dialogue: 0,0:07:57.60,0:07:59.95,Default,,0000,0000,0000,,It checks to see if what we entered is\Nequal to the string done. Dialogue: 0,0:07:59.95,0:08:02.48,Default,,0000,0000,0000,,It is not, so it skips and it\Ndoes the print. Dialogue: 0,0:08:03.60,0:08:06.59,Default,,0000,0000,0000,,And we do this one more time,\Nwe type finished. Dialogue: 0,0:08:06.59,0:08:10.02,Default,,0000,0000,0000,,And then the line is not done,\Nthat variable Dialogue: 0,0:08:10.02,0:08:11.86,Default,,0000,0000,0000,,line does not have the value done in it. Dialogue: 0,0:08:11.86,0:08:13.30,Default,,0000,0000,0000,,So we print that. We come in Dialogue: 0,0:08:13.30,0:08:16.70,Default,,0000,0000,0000,,one more time, but this time\Nthis is true, so it Dialogue: 0,0:08:16.70,0:08:21.18,Default,,0000,0000,0000,,goes in and executes the break\Nand then it escapes the loop. Dialogue: 0,0:08:21.18,0:08:22.29,Default,,0000,0000,0000,,And so you can think of... Dialogue: 0,0:08:23.87,0:08:24.24,Default,,0000,0000,0000,,Right? Dialogue: 0,0:08:24.24,0:08:26.30,Default,,0000,0000,0000,,Here is the body of this loop, Dialogue: 0,0:08:26.30,0:08:29.70,Default,,0000,0000,0000,,because that's where the indentation\Nstarts and ends. Dialogue: 0,0:08:29.70,0:08:34.73,Default,,0000,0000,0000,,The break says, break me out of the\Ncurrent loop that I am in Dialogue: 0,0:08:34.73,0:08:38.21,Default,,0000,0000,0000,,and get to that next line that has the\Nsame indent as the while. Dialogue: 0,0:08:38.21,0:08:42.05,Default,,0000,0000,0000,,So, whatever it is, break says, we are\Ndone with this loop. Dialogue: 0,0:08:42.05,0:08:45.31,Default,,0000,0000,0000,,When that statement executes, we are\Ndone with the loop. Dialogue: 0,0:08:45.31,0:08:46.72,Default,,0000,0000,0000,,We're finished with the loop. Dialogue: 0,0:08:46.72,0:08:50.97,Default,,0000,0000,0000,,It'll run until that executes\Nbecause we got this set to be while True. Dialogue: 0,0:08:53.18,0:08:57.16,Default,,0000,0000,0000,,Okay, so there's a simpler, I mean this is\Nsort of a simple way to draw this. Dialogue: 0,0:08:57.16,0:09:01.10,Default,,0000,0000,0000,,Break is sort of a jump to the statement\Nimmediately following the loop. Dialogue: 0,0:09:02.53,0:09:04.42,Default,,0000,0000,0000,,If you really want to picture this, I think Dialogue: 0,0:09:04.42,0:09:06.83,Default,,0000,0000,0000,,of this as a kind of like a Star Trek\Ntransporter. Dialogue: 0,0:09:06.83,0:09:11.59,Default,,0000,0000,0000,,Where you kind of come into break and\Nthen [SOUND] your molecules are sent Dialogue: 0,0:09:11.59,0:09:15.52,Default,,0000,0000,0000,,to the four corners of the universe, and\Nyou reassemble outside of the loop. Dialogue: 0,0:09:15.52,0:09:18.59,Default,,0000,0000,0000,,And so if we look at this in sort of\Nmy little road map Dialogue: 0,0:09:18.59,0:09:20.54,Default,,0000,0000,0000,,version of these things, right? Dialogue: 0,0:09:20.54,0:09:23.64,Default,,0000,0000,0000,,The while loop is going to run for \Na while, yada, yada. Dialogue: 0,0:09:23.64,0:09:25.83,Default,,0000,0000,0000,,There can actually be more\Nthan one break Dialogue: 0,0:09:25.83,0:09:29.88,Default,,0000,0000,0000,,as long as they only get this. But the\Nmoment that somehow, some if or Dialogue: 0,0:09:29.88,0:09:36.11,Default,,0000,0000,0000,,whatever hits the break, then it gets out\Ncompletely and so it escapes the loop. Dialogue: 0,0:09:36.11,0:09:40.74,Default,,0000,0000,0000,,And so it sort of like you, you,\Nyou're zoom, zoom, Dialogue: 0,0:09:40.74,0:09:43.52,Default,,0000,0000,0000,,zoom, zoom, zoom, zoom, you\Ncome in here and then you are, Dialogue: 0,0:09:43.52,0:09:48.42,Default,,0000,0000,0000,,you are re-materialized outside the loop.\NThat's what the break does, okay? Dialogue: 0,0:09:48.42,0:09:52.57,Default,,0000,0000,0000,,So, break is one way to control the\Nexecution of loops. Dialogue: 0,0:09:54.53,0:09:57.09,Default,,0000,0000,0000,,Now, another way to control\Nthe execution of Dialogue: 0,0:09:57.09,0:10:00.00,Default,,0000,0000,0000,,loops, that doesn't actually exit the\Nloop, is called continue. Dialogue: 0,0:10:01.00,0:10:06.63,Default,,0000,0000,0000,,Continue basically says, hey, I'm done\Nwith this iteration of the loop. Dialogue: 0,0:10:06.63,0:10:09.62,Default,,0000,0000,0000,,Now each time through the loop, we\Ncall that an iteration. Dialogue: 0,0:10:09.62,0:10:13.30,Default,,0000,0000,0000,,Continue says, I don't want to\Nstop the loop, but I Dialogue: 0,0:10:13.30,0:10:17.59,Default,,0000,0000,0000,,want to stop this iteration and\Nadvance to the next iteration. Dialogue: 0,0:10:17.59,0:10:20.10,Default,,0000,0000,0000,,And so what we have here is\Nwe have the same basic loop, Dialogue: 0,0:10:20.10,0:10:23.04,Default,,0000,0000,0000,,a while True which kind of\Nmakes us an infinite loop. Dialogue: 0,0:10:24.57,0:10:27.42,Default,,0000,0000,0000,,We're going to read a line prompting\Nwith a less than sign. Dialogue: 0,0:10:28.80,0:10:30.69,Default,,0000,0000,0000,,And if it's done, we are going to\Nbreak, that code is Dialogue: 0,0:10:30.69,0:10:32.99,Default,,0000,0000,0000,,down here, and we are going to\Nprint it if we fall through. Dialogue: 0,0:10:32.99,0:10:34.92,Default,,0000,0000,0000,,So normally we'll be reading and Dialogue: 0,0:10:34.92,0:10:38.30,Default,,0000,0000,0000,,printing, and if the line is done,\Nwe are going to break out. Dialogue: 0,0:10:38.30,0:10:40.70,Default,,0000,0000,0000,,That's we just got done doing.\NBut the new part is right here. Dialogue: 0,0:10:41.96,0:10:44.12,Default,,0000,0000,0000,,And this is, we'll learn this\Nin the next chapter. Dialogue: 0,0:10:44.12,0:10:46.64,Default,,0000,0000,0000,,If line sub 0, if the first character of Dialogue: 0,0:10:46.64,0:10:50.65,Default,,0000,0000,0000,,the line, is a pound sign, we're\Ngoing to continue. Dialogue: 0,0:10:50.65,0:10:54.30,Default,,0000,0000,0000,,And what continue says is it doesn't\Nactually get us out of the loop. Dialogue: 0,0:10:54.30,0:10:56.23,Default,,0000,0000,0000,,It jumps back up to the top of the loop. Dialogue: 0,0:10:56.23,0:11:00.06,Default,,0000,0000,0000,,Which means that it ignores,\Nfor that iteration, Dialogue: 0,0:11:00.06,0:11:01.86,Default,,0000,0000,0000,,the rest of the loop. Right? Dialogue: 0,0:11:01.86,0:11:04.23,Default,,0000,0000,0000,,So if execution comes in here. Dialogue: 0,0:11:04.23,0:11:05.74,Default,,0000,0000,0000,,I'm going to clear that. Dialogue: 0,0:11:06.88,0:11:12.81,Default,,0000,0000,0000,,If execution comes in here and hits this\Nline, it goes back up to the while. Dialogue: 0,0:11:12.81,0:11:13.70,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:11:13.70,0:11:17.19,Default,,0000,0000,0000,,Which means it, whatever this is, it's not\Ncoming out of this if. Dialogue: 0,0:11:17.19,0:11:19.10,Default,,0000,0000,0000,,It's going back up to the while. Dialogue: 0,0:11:19.10,0:11:19.71,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:11:19.71,0:11:22.52,Default,,0000,0000,0000,,So continue ends the current\Niteration and jumps to Dialogue: 0,0:11:22.52,0:11:24.97,Default,,0000,0000,0000,,the top of the loop, and starts\Nthe next iteration. Dialogue: 0,0:11:26.30,0:11:28.38,Default,,0000,0000,0000,,And so if we look at how the code runs, Dialogue: 0,0:11:28.38,0:11:32.30,Default,,0000,0000,0000,,hello there prints, pound sign with the\Nfirst character Dialogue: 0,0:11:32.30,0:11:35.20,Default,,0000,0000,0000,,doesn't print, so there is\Nno printout right here. Dialogue: 0,0:11:35.20,0:11:39.34,Default,,0000,0000,0000,,Print this is not done. And we enter done,\Nand then the loop ends. Dialogue: 0,0:11:39.34,0:11:41.46,Default,,0000,0000,0000,,Now another way to sort of draw this is Dialogue: 0,0:11:41.46,0:11:44.44,Default,,0000,0000,0000,,the continued jumps to the top\Nof the loop. Dialogue: 0,0:11:44.44,0:11:49.60,Default,,0000,0000,0000,,It, it does run the question, right?\NIt does check the question. Dialogue: 0,0:11:49.60,0:11:51.48,Default,,0000,0000,0000,,So here is another way to, to draw Dialogue: 0,0:11:51.48,0:11:55.83,Default,,0000,0000,0000,,that picture. And so here again we have a\Nloop and it's happily running. Dialogue: 0,0:11:55.83,0:11:57.50,Default,,0000,0000,0000,,And there can be breaks in there\Nand there could Dialogue: 0,0:11:57.50,0:12:00.11,Default,,0000,0000,0000,,be continues in there, and as\Nlong as we don't Dialogue: 0,0:12:00.11,0:12:01.76,Default,,0000,0000,0000,,hit a break or continue, the\Nloop just sort of Dialogue: 0,0:12:01.76,0:12:04.96,Default,,0000,0000,0000,,runs and goes up to the top.\NAnd at some point, some if, Dialogue: 0,0:12:04.96,0:12:09.02,Default,,0000,0000,0000,,we hit the continue. And\Nlike a transporter, instead of Dialogue: 0,0:12:09.02,0:12:11.32,Default,,0000,0000,0000,,going out of the loop, we go to\Nthe top of the loop. Dialogue: 0,0:12:11.32,0:12:15.60,Default,,0000,0000,0000,,But it's important that we go and\Nwe check the question, right? Dialogue: 0,0:12:15.60,0:12:16.50,Default,,0000,0000,0000,,So the continue is Dialogue: 0,0:12:16.50,0:12:20.02,Default,,0000,0000,0000,,not likely to exit the loop\Nunless the question has become false. Dialogue: 0,0:12:20.02,0:12:22.67,Default,,0000,0000,0000,,So the continue is likely to\Ncome up here, Dialogue: 0,0:12:22.67,0:12:25.54,Default,,0000,0000,0000,,run some more, then hit the continue.\NIt comes up here. Dialogue: 0,0:12:25.54,0:12:26.64,Default,,0000,0000,0000,,Oops!\NOops, it did that backwards. Dialogue: 0,0:12:26.64,0:12:27.83,Default,,0000,0000,0000,,Run some more. Dialogue: 0,0:12:27.83,0:12:29.47,Default,,0000,0000,0000,,Clear this out. Dialogue: 0,0:12:29.47,0:12:32.10,Default,,0000,0000,0000,,So, the continue could run\Nmany times, right? Dialogue: 0,0:12:32.10,0:12:33.11,Default,,0000,0000,0000,,So we have the loop. Dialogue: 0,0:12:33.11,0:12:36.62,Default,,0000,0000,0000,,Loop runs a bunch of times, then\Nfinally we hit the continue. Dialogue: 0,0:12:36.62,0:12:37.90,Default,,0000,0000,0000,,Continue goes up to the top. Dialogue: 0,0:12:37.90,0:12:40.69,Default,,0000,0000,0000,,If it's still true, we'll run the \Nloop some more. Dialogue: 0,0:12:40.69,0:12:42.14,Default,,0000,0000,0000,,Then you might hit the continue. Dialogue: 0,0:12:42.14,0:12:43.61,Default,,0000,0000,0000,,Then you might go up to the top, come Dialogue: 0,0:12:43.61,0:12:46.93,Default,,0000,0000,0000,,down, round and round and round and round,\Nhit the continue again. Dialogue: 0,0:12:46.93,0:12:49.24,Default,,0000,0000,0000,,Go up to the top, yada, yada. Dialogue: 0,0:12:49.24,0:12:52.61,Default,,0000,0000,0000,,Now in this, in this particular loop,\Nthis break eventually Dialogue: 0,0:12:52.61,0:12:55.24,Default,,0000,0000,0000,,is down here and that's how we \Nget out, okay? Dialogue: 0,0:12:55.24,0:12:58.04,Default,,0000,0000,0000,,So the continue goes back up\Nto the top of the loop. Dialogue: 0,0:13:00.02,0:13:01.94,Default,,0000,0000,0000,,So these loops that we construct with the Dialogue: 0,0:13:01.94,0:13:05.34,Default,,0000,0000,0000,,while keyword are what we call\Nindefinite loops. Dialogue: 0,0:13:05.34,0:13:07.42,Default,,0000,0000,0000,,I mean, looking at the ones that we've Dialogue: 0,0:13:07.42,0:13:09.63,Default,,0000,0000,0000,,written, which are two lines or six lines, Dialogue: 0,0:13:09.63,0:13:12.80,Default,,0000,0000,0000,,we can kind of inspect them and\Nunderstand when they are going Dialogue: 0,0:13:12.80,0:13:16.78,Default,,0000,0000,0000,,to stop, and we are going to know that\Nthey're possible to stop them. Dialogue: 0,0:13:16.78,0:13:20.13,Default,,0000,0000,0000,,A loop that won't stop is an infinite loop. Dialogue: 0,0:13:21.50,0:13:24.86,Default,,0000,0000,0000,,Sometimes these loops can be rather [COUGH]\Ncomplex and you may not actually be able to Dialogue: 0,0:13:24.86,0:13:29.91,Default,,0000,0000,0000,,look at them, because there are many lines\Nand, and so we don't know. Dialogue: 0,0:13:29.91,0:13:32.46,Default,,0000,0000,0000,,And so, so if you, you have to be\Nreal careful when you Dialogue: 0,0:13:32.46,0:13:37.20,Default,,0000,0000,0000,,construct these to make sure that they\Nstop as, as things get more complicated. Dialogue: 0,0:13:37.20,0:13:42.38,Default,,0000,0000,0000,,Now the cousin to indefinite loops are\Ndefinite loops. Dialogue: 0,0:13:42.38,0:13:46.71,Default,,0000,0000,0000,,And definite loops is something where we\Nhave a list of things or a set Dialogue: 0,0:13:46.71,0:13:49.90,Default,,0000,0000,0000,,of things that are kind of a known set of\Nthings, a finite set of things. Dialogue: 0,0:13:51.05,0:13:53.27,Default,,0000,0000,0000,,And we are going to write a loop that's\Ngoing to go through that set of things Dialogue: 0,0:13:53.27,0:13:57.46,Default,,0000,0000,0000,,and do something to each thing\Nin that set of things. Dialogue: 0,0:13:57.46,0:13:59.63,Default,,0000,0000,0000,,And the keyword that we use\Nfor this is the for. Dialogue: 0,0:13:59.63,0:14:05.13,Default,,0000,0000,0000,,So we use the Python for keyword that\Nsays we are going to write a loop, but Dialogue: 0,0:14:05.13,0:14:07.64,Default,,0000,0000,0000,,instead of it just running until some\Ncondition Dialogue: 0,0:14:07.64,0:14:09.32,Default,,0000,0000,0000,,becomes true or false or we hit a break, Dialogue: 0,0:14:10.32,0:14:13.72,Default,,0000,0000,0000,,we're actually going to know how many\Ntimes this is going to run. Dialogue: 0,0:14:13.72,0:14:16.64,Default,,0000,0000,0000,,Now you can actually use break and\Ncontinue in for loops. Dialogue: 0,0:14:16.64,0:14:19.82,Default,,0000,0000,0000,,We call these definite loops because\Nthe, how long Dialogue: 0,0:14:19.82,0:14:23.12,Default,,0000,0000,0000,,they're going to run is kind of\Nwell known, basically. Dialogue: 0,0:14:23.12,0:14:26.63,Default,,0000,0000,0000,,So here's a simple definite loop, and it's\Nkind of like that while loop Dialogue: 0,0:14:26.63,0:14:28.34,Default,,0000,0000,0000,,that we just got done looking at Dialogue: 0,0:14:28.34,0:14:31.40,Default,,0000,0000,0000,,where it's counting down and then\Nsaying blastoff. Dialogue: 0,0:14:31.40,0:14:35.29,Default,,0000,0000,0000,,And so the way we construct this loop is\Nwe have the for keyword, Dialogue: 0,0:14:35.29,0:14:37.58,Default,,0000,0000,0000,,it's part of Python language, Dialogue: 0,0:14:37.58,0:14:40.95,Default,,0000,0000,0000,,the in keyword, and then we \Nhave an iteration variable. Dialogue: 0,0:14:40.95,0:14:43.70,Default,,0000,0000,0000,,I've chosen i as my iteration variable. Dialogue: 0,0:14:43.70,0:14:46.59,Default,,0000,0000,0000,,And basically what we're saying is,\Ndear Python, Dialogue: 0,0:14:48.56,0:14:52.97,Default,,0000,0000,0000,,run this indented block, and there's only\None line in the indented block. Dialogue: 0,0:14:52.97,0:14:56.92,Default,,0000,0000,0000,,Run it once for each of the values\Nin this little list. Dialogue: 0,0:14:56.92,0:15:00.59,Default,,0000,0000,0000,,This is a Python list, square brackets\Nmake Python lists. Dialogue: 0,0:15:00.59,0:15:02.25,Default,,0000,0000,0000,,Comma-separated values. Dialogue: 0,0:15:02.25,0:15:05.21,Default,,0000,0000,0000,,So it says, I would like i to be 5,\Nthen run this code. Dialogue: 0,0:15:05.21,0:15:07.23,Default,,0000,0000,0000,,Then I would like i to be 4,\Nthen run this code. Dialogue: 0,0:15:07.23,0:15:09.43,Default,,0000,0000,0000,,Then I would like i to be 3,\Nthen run this code. Dialogue: 0,0:15:09.43,0:15:13.54,Default,,0000,0000,0000,,i should be 2, then run this code.\NAnd i should be 1, then run this code. Dialogue: 0,0:15:13.54,0:15:16.45,Default,,0000,0000,0000,,And so this is pretty clear,\Nand I like this word in. Dialogue: 0,0:15:17.81,0:15:22.57,Default,,0000,0000,0000,,It says you know, doop, doop, doop, \Ndoop, doop, and then run this each time. Dialogue: 0,0:15:22.57,0:15:27.81,Default,,0000,0000,0000,,And so out of that comes 5, 4, 3, 2, 1,\Nand then the loop is done. Dialogue: 0,0:15:27.81,0:15:29.92,Default,,0000,0000,0000,,Python is doing all the tricky bits Dialogue: 0,0:15:29.92,0:15:32.64,Default,,0000,0000,0000,,here, Python's figuring all these\Nthings out for us Dialogue: 0,0:15:34.88,0:15:38.57,Default,,0000,0000,0000,,and handling all this, and then we're done.\NAnd so it's, it's, if you look at it, Dialogue: 0,0:15:38.57,0:15:41.32,Default,,0000,0000,0000,,we have an iteration variable, but we\Ndidn't have to increment it, Dialogue: 0,0:15:41.32,0:15:44.92,Default,,0000,0000,0000,,we didn't have to do anything. Python took\Ncare of a lot of things for us. Dialogue: 0,0:15:44.92,0:15:47.85,Default,,0000,0000,0000,,And so when we're looping through\Na known list of things, Dialogue: 0,0:15:47.85,0:15:49.71,Default,,0000,0000,0000,,or later when we read a file, Dialogue: 0,0:15:49.71,0:15:51.48,Default,,0000,0000,0000,,we're going to loop through\Nthe lines in the file. Dialogue: 0,0:15:51.48,0:15:54.46,Default,,0000,0000,0000,,And so the for loop is a really nice\Npowerful. Dialogue: 0,0:15:54.46,0:15:58.10,Default,,0000,0000,0000,,And it's syntactically cleaner, it's\Nreally quite nice. Dialogue: 0,0:15:58.10,0:15:59.92,Default,,0000,0000,0000,,Now it's important to realize that you Dialogue: 0,0:15:59.92,0:16:03.74,Default,,0000,0000,0000,,don't have to just loop through numbers.\NI did that one with the set of Dialogue: 0,0:16:03.74,0:16:05.33,Default,,0000,0000,0000,,descending numbers, so that\Nit was equivalent to the Dialogue: 0,0:16:05.33,0:16:07.68,Default,,0000,0000,0000,,while loop that I started at the\Nbeginning. Dialogue: 0,0:16:07.68,0:16:11.51,Default,,0000,0000,0000,,But this is a loop where what it's\Ngoing to loop through is a list. Dialogue: 0,0:16:11.51,0:16:14.97,Default,,0000,0000,0000,,Closed square brackets are a list in Python. Dialogue: 0,0:16:14.97,0:16:18.99,Default,,0000,0000,0000,,This is a list of three strings, Joseph,\NGlenn, and Sally. Dialogue: 0,0:16:18.99,0:16:23.11,Default,,0000,0000,0000,,They're string constants, and then commas\Nare how we make lists. Dialogue: 0,0:16:24.37,0:16:25.82,Default,,0000,0000,0000,,And so friends Dialogue: 0,0:16:25.82,0:16:27.30,Default,,0000,0000,0000,,is a mnemonic variable. Dialogue: 0,0:16:27.30,0:16:29.55,Default,,0000,0000,0000,,Python doesn't know anything about friends\Nin particular, Dialogue: 0,0:16:29.55,0:16:31.96,Default,,0000,0000,0000,,but I've chosen this variable name\Nto be friends. Dialogue: 0,0:16:31.96,0:16:35.11,Default,,0000,0000,0000,,And it's a list of three people, Joseph,\NGlenn, and Sally. Dialogue: 0,0:16:35.11,0:16:37.90,Default,,0000,0000,0000,,And so I have an iteration variable\Ncalled friend. Dialogue: 0,0:16:37.90,0:16:40.15,Default,,0000,0000,0000,,And I'm going to loop through the\Nset of friends. Dialogue: 0,0:16:40.15,0:16:42.41,Default,,0000,0000,0000,,Now Python doesn't know anything about\Nsingular. Dialogue: 0,0:16:42.41,0:16:44.26,Default,,0000,0000,0000,,Python doesn't know anything about plural. Dialogue: 0,0:16:44.26,0:16:47.32,Default,,0000,0000,0000,,I'm just choosing these variable names\Nbecause it makes a lot of sense. Dialogue: 0,0:16:48.35,0:16:50.89,Default,,0000,0000,0000,,This is a set of friends.\NBecause it has three of them in it. Dialogue: 0,0:16:50.89,0:16:54.03,Default,,0000,0000,0000,,And this is a single friend. Dialogue: 0,0:16:54.03,0:16:57.62,Default,,0000,0000,0000,,What it's really going to do is friend is\Ngoing to take on the successive Dialogue: 0,0:16:57.62,0:17:00.60,Default,,0000,0000,0000,,of values Joseph, Glenn, and Sally,\Nand this little block of code Dialogue: 0,0:17:00.60,0:17:05.16,Default,,0000,0000,0000,,is going to once for each of\Nthose three items in the set, and Dialogue: 0,0:17:05.16,0:17:10.20,Default,,0000,0000,0000,,the variable friend is going to take on\Nthe successive values of that set. Dialogue: 0,0:17:10.20,0:17:13.36,Default,,0000,0000,0000,,So out of this comes\Nthree lines of printout. Dialogue: 0,0:17:13.36,0:17:15.98,Default,,0000,0000,0000,,Happy New Year Joseph, Happy New Year\NGlenn, Happy New Year Sally. Dialogue: 0,0:17:15.98,0:17:19.94,Default,,0000,0000,0000,,Of course this is the i bit\Nright down here, but we just Dialogue: 0,0:17:19.94,0:17:23.33,Default,,0000,0000,0000,,made it so, hey Python, look, how ever\Nmany friends they are, run this Dialogue: 0,0:17:23.33,0:17:26.82,Default,,0000,0000,0000,,code one time for each one, change this\Nvariable friend to be each Dialogue: 0,0:17:26.82,0:17:30.40,Default,,0000,0000,0000,,of the successive ones in order,\Nand then we print that we're done. Dialogue: 0,0:17:30.40,0:17:30.89,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:17:30.89,0:17:32.86,Default,,0000,0000,0000,,So the for loop. Dialogue: 0,0:17:32.86,0:17:34.86,Default,,0000,0000,0000,,Sort of if we go and try to make a\Npicture of the for loop. Dialogue: 0,0:17:34.86,0:17:39.42,Default,,0000,0000,0000,,The for loop is kind of a powerful thing.\NIt's does, it does two things. Dialogue: 0,0:17:39.42,0:17:41.08,Default,,0000,0000,0000,,It decides if we're done or not. Dialogue: 0,0:17:42.47,0:17:44.06,Default,,0000,0000,0000,,Do we keep going in the loop? Dialogue: 0,0:17:44.06,0:17:46.21,Default,,0000,0000,0000,,Or, well I mean as long as we keep Dialogue: 0,0:17:46.21,0:17:49.28,Default,,0000,0000,0000,,going, we're going to advance\Nthe i value, the iteration variable. Dialogue: 0,0:17:49.28,0:17:52.66,Default,,0000,0000,0000,,It takes care of the responsibility of\Nchanging the iteration variable. Dialogue: 0,0:17:52.66,0:17:57.05,Default,,0000,0000,0000,,We do not have to add lines of code in\Nthat change the iteration variable, okay? Dialogue: 0,0:17:57.05,0:18:00.91,Default,,0000,0000,0000,,And so if we take a look, you know,\Nwe come in. Dialogue: 0,0:18:00.91,0:18:02.28,Default,,0000,0000,0000,,Are we done? We're not done. Dialogue: 0,0:18:02.28,0:18:04.31,Default,,0000,0000,0000,,Set i to the right thing, then print it. Dialogue: 0,0:18:04.31,0:18:07.19,Default,,0000,0000,0000,,Out comes 5. Advance i, Dialogue: 0,0:18:07.19,0:18:09.37,Default,,0000,0000,0000,,advance i, print it. Advance it. Dialogue: 0,0:18:09.37,0:18:10.17,Default,,0000,0000,0000,,Print it. Advance it. Print it. Dialogue: 0,0:18:10.17,0:18:12.50,Default,,0000,0000,0000,,Oh, now we're done. Dialogue: 0,0:18:12.50,0:18:13.35,Default,,0000,0000,0000,,Right? Dialogue: 0,0:18:13.35,0:18:16.51,Default,,0000,0000,0000,,i was not the thing that decided\Nwhen we were done. Dialogue: 0,0:18:16.51,0:18:19.66,Default,,0000,0000,0000,,The for loop just keeps track\Ninternally as i moves Dialogue: 0,0:18:19.66,0:18:22.79,Default,,0000,0000,0000,,through these things and it\Ngoes like oh, I'm all done. Dialogue: 0,0:18:22.79,0:18:25.26,Default,,0000,0000,0000,,I'll take care of that, you finished. Dialogue: 0,0:18:25.26,0:18:29.15,Default,,0000,0000,0000,,So it doesn't, there's no if in here,\Nso it's not like like if i equals 1, stop. Dialogue: 0,0:18:29.15,0:18:31.78,Default,,0000,0000,0000,,No, no, no, it just says you told me\Nto do five things. Dialogue: 0,0:18:31.78,0:18:32.36,Default,,0000,0000,0000,,I'm going to do five things Dialogue: 0,0:18:32.36,0:18:33.71,Default,,0000,0000,0000,,and then we're going to stop. Dialogue: 0,0:18:33.71,0:18:40.22,Default,,0000,0000,0000,,And so again the for loop, the for loop\Nhere has got sort of two functions. Dialogue: 0,0:18:40.22,0:18:43.91,Default,,0000,0000,0000,,It decides how long the loop is going to\Nrun, and changes the Dialogue: 0,0:18:43.91,0:18:47.57,Default,,0000,0000,0000,,iteration variable based on what you've\Ntold it to, in this in clause. Dialogue: 0,0:18:48.69,0:18:49.19,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:18:50.31,0:18:53.58,Default,,0000,0000,0000,,So I think in is a real elegant\Nconstruct. Dialogue: 0,0:18:53.58,0:18:58.29,Default,,0000,0000,0000,,It's just a keyword, but it's \Nsort of, if you think about math, Dialogue: 0,0:18:58.29,0:18:59.68,Default,,0000,0000,0000,,if you're familiar with sets, Dialogue: 0,0:18:59.68,0:19:02.12,Default,,0000,0000,0000,,it's like something inside of \Na set of something. Dialogue: 0,0:19:02.12,0:19:03.98,Default,,0000,0000,0000,,I think it's a real pretty way\Nto think about it. Dialogue: 0,0:19:05.08,0:19:07.35,Default,,0000,0000,0000,,And you can kind of think of it\Na little more abstractly. Dialogue: 0,0:19:07.35,0:19:10.64,Default,,0000,0000,0000,,That you say, well here's a little\Nindented block of code. Dialogue: 0,0:19:10.64,0:19:11.08,Default,,0000,0000,0000,,Right? Dialogue: 0,0:19:11.08,0:19:14.99,Default,,0000,0000,0000,,And I want it to run some number\Nof times for each Dialogue: 0,0:19:14.99,0:19:18.66,Default,,0000,0000,0000,,of the i values in the set 5, 4, 3, 2, 1. Dialogue: 0,0:19:18.66,0:19:19.95,Default,,0000,0000,0000,,That's how I kind of think of it. Dialogue: 0,0:19:19.95,0:19:23.17,Default,,0000,0000,0000,,So I think that this is a \Nreal pretty syntax. Dialogue: 0,0:19:23.17,0:19:25.49,Default,,0000,0000,0000,,Different languages have different\Nlooping syntax. Dialogue: 0,0:19:25.49,0:19:28.93,Default,,0000,0000,0000,,I think this is really a very\Nexpressive, very pretty one. Dialogue: 0,0:19:32.72,0:19:33.92,Default,,0000,0000,0000,,Yeah. Dialogue: 0,0:19:33.92,0:19:36.87,Default,,0000,0000,0000,,So another way to, so, so, so one way to Dialogue: 0,0:19:36.87,0:19:40.44,Default,,0000,0000,0000,,think about this picture is that, you\Nknow, the for loop Dialogue: 0,0:19:40.44,0:19:42.76,Default,,0000,0000,0000,,causes sort of repeated execution\Nin that we're Dialogue: 0,0:19:42.76,0:19:45.41,Default,,0000,0000,0000,,driving in the circle and then we\Nstop, right? Dialogue: 0,0:19:45.41,0:19:47.89,Default,,0000,0000,0000,,The other way to think about is to, to Dialogue: 0,0:19:47.89,0:19:50.59,Default,,0000,0000,0000,,not, to think about a little more\Nabstractly, right? Dialogue: 0,0:19:50.59,0:19:53.44,Default,,0000,0000,0000,,To say, hmm, you know, at the\Nend of the day, all I'm Dialogue: 0,0:19:53.44,0:19:58.13,Default,,0000,0000,0000,,really telling Python is, I want to\Nexecute this block of code five times, Dialogue: 0,0:19:58.13,0:20:02.19,Default,,0000,0000,0000,,then I want the variable i to\Nchange from, to these three values. Dialogue: 0,0:20:02.19,0:20:04.03,Default,,0000,0000,0000,,So in a way, you could think of \Nthis as expanded Dialogue: 0,0:20:04.03,0:20:06.96,Default,,0000,0000,0000,,is the for loop sets it to 5,\Nthen runs your code. Dialogue: 0,0:20:06.96,0:20:09.29,Default,,0000,0000,0000,,The for loop then sets it to 4,\Nruns your code. Dialogue: 0,0:20:09.29,0:20:11.52,Default,,0000,0000,0000,,The for loop sets it to 3, runs your code. Dialogue: 0,0:20:11.52,0:20:15.83,Default,,0000,0000,0000,,For loop sets it to 2, runs your code.\NSets it to 1, runs your code. Dialogue: 0,0:20:15.83,0:20:18.74,Default,,0000,0000,0000,,These two ways of looking at it\Nare the same Dialogue: 0,0:20:19.91,0:20:23.39,Default,,0000,0000,0000,,from your perspective, because\Nyou're just asking Python Dialogue: 0,0:20:23.39,0:20:24.59,Default,,0000,0000,0000,,to do something. Dialogue: 0,0:20:24.59,0:20:28.03,Default,,0000,0000,0000,,Whether it does it this way, or\Nwhether it does it this way, Dialogue: 0,0:20:28.03,0:20:32.29,Default,,0000,0000,0000,,you hardly can tell the difference.\NIt's probably going to do it this way. Dialogue: 0,0:20:32.29,0:20:37.15,Default,,0000,0000,0000,,But logically it's not that different.\NIt's not different from doing it this way. Dialogue: 0,0:20:37.15,0:20:41.44,Default,,0000,0000,0000,,You're saying, run this block of code,\Nchange i in the following way. Dialogue: 0,0:20:44.86,0:20:48.60,Default,,0000,0000,0000,,Cool.\NIt's like, we don't have to worry. Dialogue: 0,0:20:48.60,0:20:51.64,Default,,0000,0000,0000,,I mean, we can use, mentally,\Neither model of what's Dialogue: 0,0:20:51.64,0:20:54.52,Default,,0000,0000,0000,,going on inside, because it doesn't\Nmatter, because they're the same. Dialogue: 0,0:20:57.38,0:21:00.33,Default,,0000,0000,0000,,Okay, so these definite loops\Nare really cool. Dialogue: 0,0:21:00.33,0:21:02.96,Default,,0000,0000,0000,,Starting in a couple of chapters we'll\Nmostly use definite Dialogue: 0,0:21:02.96,0:21:07.20,Default,,0000,0000,0000,,loops to go through lists or\Ndictionaries or tuples or files. Dialogue: 0,0:21:08.29,0:21:09.97,Default,,0000,0000,0000,,And so that's a finite set of things. Dialogue: 0,0:21:09.97,0:21:12.10,Default,,0000,0000,0000,,It can be a large set of things, but\Nit's a finite set of things. Dialogue: 0,0:21:13.68,0:21:17.04,Default,,0000,0000,0000,,Okay. So now I want to talk about\Nloop idioms. Dialogue: 0,0:21:19.02,0:21:24.20,Default,,0000,0000,0000,,Loop idioms are how we construct loops. Dialogue: 0,0:21:24.20,0:21:28.08,Default,,0000,0000,0000,,And we're going to, the loops kind of\Nhave some kind of a goal in mind. Dialogue: 0,0:21:28.08,0:21:29.97,Default,,0000,0000,0000,,Finding the largest, we played with that. Dialogue: 0,0:21:29.97,0:21:33.36,Default,,0000,0000,0000,,Finding the smallest, counting the \Nnumber of things, Dialogue: 0,0:21:33.36,0:21:36.23,Default,,0000,0000,0000,,looking for lines that start with pound\Nsigns, something like that. Dialogue: 0,0:21:36.23,0:21:39.63,Default,,0000,0000,0000,,They, they have a kind of a high-level\Nview of what they're supposed to do, Dialogue: 0,0:21:39.63,0:21:43.83,Default,,0000,0000,0000,,and then we have to kind of build a\Nloop to accomplish that. Dialogue: 0,0:21:43.83,0:21:47.60,Default,,0000,0000,0000,,And and this goes back to how we have to\Nthink like a computer, right? Dialogue: 0,0:21:47.60,0:21:50.00,Default,,0000,0000,0000,,We have to say, hey computer, Dialogue: 0,0:21:50.00,0:21:52.08,Default,,0000,0000,0000,,do this over and over and over again,\Nand then I'll Dialogue: 0,0:21:52.08,0:21:54.30,Default,,0000,0000,0000,,get what I want once you've done that\Nover and over again. Dialogue: 0,0:21:54.30,0:21:55.76,Default,,0000,0000,0000,,You have to do something a million times. Dialogue: 0,0:21:55.76,0:21:57.10,Default,,0000,0000,0000,,I'm not going to sit here and wait. Dialogue: 0,0:21:57.10,0:21:59.03,Default,,0000,0000,0000,,At the end, I'll get what I want. Dialogue: 0,0:21:59.03,0:22:00.77,Default,,0000,0000,0000,,So I call these kind of "smart loops", Dialogue: 0,0:22:00.77,0:22:03.46,Default,,0000,0000,0000,,or how to kind of build\Nintelligence into loops. Dialogue: 0,0:22:06.17,0:22:08.64,Default,,0000,0000,0000,,So, for example, we want the largest\Nnumber. Dialogue: 0,0:22:08.64,0:22:09.40,Default,,0000,0000,0000,,Right? Dialogue: 0,0:22:09.40,0:22:11.86,Default,,0000,0000,0000,,But we have to construct a loop that will Dialogue: 0,0:22:11.86,0:22:15.02,Default,,0000,0000,0000,,get us the largest number thinking\Nlike a computer. Dialogue: 0,0:22:15.02,0:22:17.20,Default,,0000,0000,0000,,Okay? Thinking computationally. Dialogue: 0,0:22:17.20,0:22:18.92,Default,,0000,0000,0000,,Thinking like a computer. Dialogue: 0,0:22:18.92,0:22:21.55,Default,,0000,0000,0000,,So the idea is that we have some kind Dialogue: 0,0:22:21.55,0:22:24.32,Default,,0000,0000,0000,,of a loop and we're going to\Ngo through this list, Dialogue: 0,0:22:24.32,0:22:28.17,Default,,0000,0000,0000,,some list of things, and this is\Ngoing to run a bunch of times. Dialogue: 0,0:22:28.17,0:22:29.51,Default,,0000,0000,0000,,And, but the way we're going to do it is Dialogue: 0,0:22:29.51,0:22:32.13,Default,,0000,0000,0000,,we're going to set something up\Nbefore the loop starts. Dialogue: 0,0:22:32.13,0:22:35.72,Default,,0000,0000,0000,,We're going to do something to each of the\Nthings that's being looked at, Dialogue: 0,0:22:35.72,0:22:39.80,Default,,0000,0000,0000,,and at the end, we're going to get the\Nresult we're looking for. Dialogue: 0,0:22:39.80,0:22:40.56,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:22:40.56,0:22:43.31,Default,,0000,0000,0000,,And so in the middle it's kind of like\Nworking. Dialogue: 0,0:22:43.31,0:22:45.58,Default,,0000,0000,0000,,It's in the middle working,\Nda, da, da, da, da. Dialogue: 0,0:22:45.58,0:22:47.27,Default,,0000,0000,0000,,And then here is the payoff. Dialogue: 0,0:22:47.27,0:22:52.29,Default,,0000,0000,0000,,The payoff is at the end when we get the\Ninformation that we're interested in. Dialogue: 0,0:22:52.29,0:22:55.88,Default,,0000,0000,0000,,So I will sort of use in the\Nnext few examples Dialogue: 0,0:22:55.88,0:22:57.88,Default,,0000,0000,0000,,this simple loop, Dialogue: 0,0:22:57.88,0:22:59.87,Default,,0000,0000,0000,,and right now it doesn't do much. Dialogue: 0,0:22:59.87,0:23:02.14,Default,,0000,0000,0000,,It does a print Before and it\Nhas this variable Dialogue: 0,0:23:02.14,0:23:06.18,Default,,0000,0000,0000,,thing that goes through the successive\Nvalues of these numbers. Dialogue: 0,0:23:06.18,0:23:08.95,Default,,0000,0000,0000,,And it prints it out, right?\NSo that middle part says Dialogue: 0,0:23:08.95,0:23:12.50,Default,,0000,0000,0000,,run this six times, once for\Neach of those values. Dialogue: 0,0:23:12.50,0:23:13.33,Default,,0000,0000,0000,,And then After. Okay? Dialogue: 0,0:23:13.33,0:23:16.71,Default,,0000,0000,0000,,And so we will add some intelligence\Nat the beginning, we'll add Dialogue: 0,0:23:16.71,0:23:19.91,Default,,0000,0000,0000,,some intelligence in the middle, and we'll\Nadd some intelligence at the end. Dialogue: 0,0:23:19.91,0:23:22.79,Default,,0000,0000,0000,,And then the whole thing will\Naccomplish what we want. Dialogue: 0,0:23:22.79,0:23:26.17,Default,,0000,0000,0000,,Right now this is kind of not that\Nintelligent. Dialogue: 0,0:23:26.17,0:23:30.58,Default,,0000,0000,0000,,So now what I want to do, is I want to\Nreview the thing we did, and I want you Dialogue: 0,0:23:30.58,0:23:32.90,Default,,0000,0000,0000,,to remember what the largest number is,\Nand I'm going to Dialogue: 0,0:23:32.90,0:23:35.76,Default,,0000,0000,0000,,show you a sequence of numbers in order. Dialogue: 0,0:23:35.76,0:23:36.59,Default,,0000,0000,0000,,Ready? Dialogue: 0,0:23:36.59,0:23:38.94,Default,,0000,0000,0000,,One, I'll do it kind of quickly, because\Nyou've seen this before. Dialogue: 0,0:23:38.94,0:23:41.63,Default,,0000,0000,0000,,So, I'm only showing you one number\Nat a time. Dialogue: 0,0:23:41.63,0:23:44.11,Default,,0000,0000,0000,,So you want to tell me what the\Nlargest number is. Dialogue: 0,0:23:44.11,0:23:48.24,Default,,0000,0000,0000,,So here we go.\NThe first number is 9. Dialogue: 0,0:23:48.24,0:23:52.12,Default,,0000,0000,0000,,The second number is 41. Dialogue: 0,0:23:52.12,0:23:57.07,Default,,0000,0000,0000,,The third number is 12.\NThe fourth number is 3. Dialogue: 0,0:23:57.07,0:24:03.70,Default,,0000,0000,0000,,The fifth number is 74.\NAnd the sixth number is 15. Dialogue: 0,0:24:03.70,0:24:05.79,Default,,0000,0000,0000,,So what was the largest number? Dialogue: 0,0:24:09.04,0:24:12.74,Default,,0000,0000,0000,,Did you have to go back?\NOr did you remember how to do it? Dialogue: 0,0:24:12.74,0:24:17.98,Default,,0000,0000,0000,,Okay, well, I will give you a clue.\NIt was 74. Dialogue: 0,0:24:17.98,0:24:20.44,Default,,0000,0000,0000,,Okay? That's because I know. Dialogue: 0,0:24:20.44,0:24:24.45,Default,,0000,0000,0000,,Okay, now if you did that and you had to\Ndo that for 20 or 30 numbers, Dialogue: 0,0:24:24.45,0:24:26.68,Default,,0000,0000,0000,,you'd have to create a mental algorithm in Dialogue: 0,0:24:26.68,0:24:31.15,Default,,0000,0000,0000,,your head to approach it and stay\Nconcentrated, focused. Dialogue: 0,0:24:31.15,0:24:33.53,Default,,0000,0000,0000,,So, you would've created a\Nvariable in your head Dialogue: 0,0:24:33.53,0:24:35.17,Default,,0000,0000,0000,,called largest_so_far. Dialogue: 0,0:24:36.61,0:24:40.02,Default,,0000,0000,0000,,I would show you the first number, which\Nwould be 9, and you would go hmm. Dialogue: 0,0:24:40.02,0:24:43.12,Default,,0000,0000,0000,,Well, 9 is larger than 1, negative 1. Dialogue: 0,0:24:43.12,0:24:46.43,Default,,0000,0000,0000,,So I will keep that. That's the new\Nlargest I've seen so far. Dialogue: 0,0:24:46.43,0:24:50.06,Default,,0000,0000,0000,,That's pretty awesome, because it's way\Nbetter than negative 1. Dialogue: 0,0:24:50.06,0:24:54.86,Default,,0000,0000,0000,,41? I thought 9 was good.\NBut 41, that is a lot better. Dialogue: 0,0:24:54.86,0:24:55.82,Default,,0000,0000,0000,,So I'm going to keep that one. Dialogue: 0,0:24:55.82,0:24:58.83,Default,,0000,0000,0000,,That's the, that's the best.\NIt's the largest we've seen so far. Dialogue: 0,0:24:58.83,0:24:59.78,Default,,0000,0000,0000,,We've only seen two numbers. Dialogue: 0,0:25:00.89,0:25:03.46,Default,,0000,0000,0000,,But the best we've so far is 41. Dialogue: 0,0:25:03.46,0:25:08.52,Default,,0000,0000,0000,,So 12 is not larger. Who, who\Ncares about that? Dialogue: 0,0:25:08.52,0:25:12.06,Default,,0000,0000,0000,,It's not as big as 41 so we'll just go\Nright on to the next, on to the next. Dialogue: 0,0:25:12.06,0:25:16.22,Default,,0000,0000,0000,,3, that's lame when we are\Nlooking for large numbers. Dialogue: 0,0:25:16.22,0:25:19.64,Default,,0000,0000,0000,,So we skip, oh, 74. 74, that's a\Nrockingly large number. Dialogue: 0,0:25:19.64,0:25:20.87,Default,,0000,0000,0000,,So we're going to, that's a lot. Dialogue: 0,0:25:20.87,0:25:23.18,Default,,0000,0000,0000,,That's actually the largest we've\Nseen so far Dialogue: 0,0:25:23.18,0:25:26.21,Default,,0000,0000,0000,,because it's bigger than 41,\Nand 41 was the former champion Dialogue: 0,0:25:26.21,0:25:31.41,Default,,0000,0000,0000,,largest we've seen so far.\NAnd there's 74, so we keep that one. Dialogue: 0,0:25:31.41,0:25:33.90,Default,,0000,0000,0000,,I don't know how many letters, of these\Nthings you're going to see, right? Dialogue: 0,0:25:33.90,0:25:37.93,Default,,0000,0000,0000,,We could see lots of them.\NBut the next one we see 15. Dialogue: 0,0:25:37.93,0:25:39.55,Default,,0000,0000,0000,,Well, that's no good. Dialogue: 0,0:25:39.55,0:25:43.01,Default,,0000,0000,0000,,We've got 74 already.\N74's like totally awesome. Dialogue: 0,0:25:43.01,0:25:43.62,Default,,0000,0000,0000,,Right? Dialogue: 0,0:25:43.62,0:25:45.26,Default,,0000,0000,0000,,So now, oh, we're done. Dialogue: 0,0:25:45.26,0:25:49.55,Default,,0000,0000,0000,,So, hey, we're done and so 74\Nis the champion. Dialogue: 0,0:25:50.66,0:25:51.67,Default,,0000,0000,0000,,That is the largest. Dialogue: 0,0:25:51.67,0:25:57.16,Default,,0000,0000,0000,,It's not even the largest so far\Nany more, it's actually the largest. Dialogue: 0,0:25:57.16,0:25:58.56,Default,,0000,0000,0000,,It's the largest. Dialogue: 0,0:25:58.56,0:26:01.71,Default,,0000,0000,0000,,So again, we had this thing\Nat the top, we had this loop in Dialogue: 0,0:26:01.71,0:26:04.19,Default,,0000,0000,0000,,the middle, and at the bottom is \Nwhere you kind of get the payoff. Dialogue: 0,0:26:04.19,0:26:05.48,Default,,0000,0000,0000,,And the payoff is Dialogue: 0,0:26:05.48,0:26:07.09,Default,,0000,0000,0000,,not in the middle. Dialogue: 0,0:26:07.09,0:26:09.22,Default,,0000,0000,0000,,while we're largest so far, largest so\Nfar, largest so far, Dialogue: 0,0:26:09.22,0:26:10.69,Default,,0000,0000,0000,,but at the end it turned out Dialogue: 0,0:26:10.69,0:26:12.27,Default,,0000,0000,0000,,once you've looked at all the variables, Dialogue: 0,0:26:12.27,0:26:16.33,Default,,0000,0000,0000,,all the values, the largest so far\Nis indeed the largest. Dialogue: 0,0:26:16.33,0:26:16.94,Default,,0000,0000,0000,,Okay. Dialogue: 0,0:26:16.94,0:26:19.18,Default,,0000,0000,0000,,So here's the algorithm for this. Dialogue: 0,0:26:19.18,0:26:20.96,Default,,0000,0000,0000,,I have some variables, and remember Dialogue: 0,0:26:20.96,0:26:24.82,Default,,0000,0000,0000,,that underscores are valid characters in\Nvariables. Dialogue: 0,0:26:24.82,0:26:28.65,Default,,0000,0000,0000,,Now [COUGH] I'm being a little\Nover-explicit in this. Dialogue: 0,0:26:28.65,0:26:31.54,Default,,0000,0000,0000,,So I have a variable called\Nlargest_so_far. Dialogue: 0,0:26:31.54,0:26:37.00,Default,,0000,0000,0000,,Then what I do is I set it to 1,\Nnegative 1. Dialogue: 0,0:26:37.00,0:26:41.88,Default,,0000,0000,0000,,Then I print Before so we can see that\Nlargest_so_far is negative 1. Dialogue: 0,0:26:41.88,0:26:46.13,Default,,0000,0000,0000,,Then we have a for loop and my variable\Niteration variable is the_num. Dialogue: 0,0:26:46.13,0:26:50.95,Default,,0000,0000,0000,,So that's going to take on the successive\Nvalues: 9, 41, 12, 3, 74, 15, Dialogue: 0,0:26:50.95,0:26:54.23,Default,,0000,0000,0000,,and run this indented loop of code, okay? Dialogue: 0,0:26:54.23,0:26:59.12,Default,,0000,0000,0000,,And so the_num will be 9,\Nfirst time through. Dialogue: 0,0:26:59.12,0:27:02.17,Default,,0000,0000,0000,,If the_num, 9, is greater than Dialogue: 0,0:27:02.17,0:27:07.42,Default,,0000,0000,0000,,largest_so_far, then grab the_num\Nand assign Dialogue: 0,0:27:07.42,0:27:13.36,Default,,0000,0000,0000,,it into largest_so_far.\NThen print at the end of each loop, Dialogue: 0,0:27:13.36,0:27:18.99,Default,,0000,0000,0000,,largest_so_far and the_num.\NSo, so, in effect, the_num is 9. Dialogue: 0,0:27:18.99,0:27:22.84,Default,,0000,0000,0000,,We compare it to negative 1, and\Nnegative 1, 9 is higher. Dialogue: 0,0:27:22.84,0:27:25.26,Default,,0000,0000,0000,,So we make largest_so_far be 9. Dialogue: 0,0:27:27.37,0:27:32.71,Default,,0000,0000,0000,,Next time through the loop, next time\Nthrough the loop, num is 41. Dialogue: 0,0:27:32.71,0:27:38.18,Default,,0000,0000,0000,,So we compare largest_so_far with 41, and\Nwe like it, so we store it. Dialogue: 0,0:27:38.18,0:27:42.08,Default,,0000,0000,0000,,So we like it, we run it, and we print\Nour 41 is the largest we've seen so far. Dialogue: 0,0:27:43.33,0:27:44.70,Default,,0000,0000,0000,,And we run again. Dialogue: 0,0:27:44.70,0:27:49.82,Default,,0000,0000,0000,,We come in, the_num now points to 12.\Nthe_num, 12, is Dialogue: 0,0:27:49.82,0:27:55.15,Default,,0000,0000,0000,,not greater than 41, and so we skip.\NSo, the largest Dialogue: 0,0:27:55.15,0:28:03.30,Default,,0000,0000,0000,,so far stays 41, and we see 12.\NSimilarly, the_num advances to 3. Dialogue: 0,0:28:03.30,0:28:04.58,Default,,0000,0000,0000,,We skip. Dialogue: 0,0:28:04.58,0:28:08.06,Default,,0000,0000,0000,,So we saw 3, but the largest\Nso far is still 41. Dialogue: 0,0:28:08.06,0:28:13.90,Default,,0000,0000,0000,,Continuing, the_num is now 74.\NIt runs, 74 is Dialogue: 0,0:28:13.90,0:28:20.31,Default,,0000,0000,0000,,greater than 41, and so we run\Nthis code. And so we say 74 Dialogue: 0,0:28:20.31,0:28:24.52,Default,,0000,0000,0000,,is stuck in largest_so_far,\Nand indeed, then we print Dialogue: 0,0:28:24.52,0:28:27.38,Default,,0000,0000,0000,,it out, and largest so far is now 74. Dialogue: 0,0:28:27.38,0:28:29.14,Default,,0000,0000,0000,,We continue on. Dialogue: 0,0:28:29.14,0:28:30.51,Default,,0000,0000,0000,,We go up one more time. Dialogue: 0,0:28:30.51,0:28:35.54,Default,,0000,0000,0000,,The_num points to 15, but 15\Nis not larger than 74. Dialogue: 0,0:28:35.54,0:28:36.76,Default,,0000,0000,0000,,And so we skip. Dialogue: 0,0:28:36.76,0:28:40.84,Default,,0000,0000,0000,,We print out 15 and 74, and then\Nwe come out and Dialogue: 0,0:28:40.84,0:28:44.70,Default,,0000,0000,0000,,at the end, at the end, we get\Nthe largest so far. Dialogue: 0,0:28:44.70,0:28:46.40,Default,,0000,0000,0000,,It, the name no matter, no longer, Dialogue: 0,0:28:46.40,0:28:48.33,Default,,0000,0000,0000,,I mean it's kind of the largest Dialogue: 0,0:28:48.33,0:28:51.54,Default,,0000,0000,0000,,so far at the end is the largest,\Nbut the variable name. Dialogue: 0,0:28:51.54,0:28:53.32,Default,,0000,0000,0000,,Okay? Got it? Dialogue: 0,0:28:55.59,0:28:59.23,Default,,0000,0000,0000,,That's one idiom.\NSo let's just switch to another idiom. Dialogue: 0,0:29:00.57,0:29:02.51,Default,,0000,0000,0000,,Now counting, how many things are we Dialogue: 0,0:29:02.51,0:29:04.17,Default,,0000,0000,0000,,going to, how many times is loop\Ngoing to execute? Dialogue: 0,0:29:04.17,0:29:06.38,Default,,0000,0000,0000,,How things are we going to find\Nin the loop? Dialogue: 0,0:29:06.38,0:29:08.02,Default,,0000,0000,0000,,It's all kind of the same notion. Dialogue: 0,0:29:08.02,0:29:11.71,Default,,0000,0000,0000,,And the pattern is really simple.\NWe start some variable zork. Dialogue: 0,0:29:11.71,0:29:13.13,Default,,0000,0000,0000,,A better name for this would be count. Dialogue: 0,0:29:13.13,0:29:15.87,Default,,0000,0000,0000,,But I want to call it zork. Dialogue: 0,0:29:15.87,0:29:20.44,Default,,0000,0000,0000,,And then we have a loop, and then in the\Nloop we just add 1 to zork, Dialogue: 0,0:29:20.44,0:29:22.08,Default,,0000,0000,0000,,and at the end, zork. Dialogue: 0,0:29:22.08,0:29:26.88,Default,,0000,0000,0000,,That should be light blue right there.\Nzork should be the total count. Dialogue: 0,0:29:26.88,0:29:29.53,Default,,0000,0000,0000,,Now of course we can look at it and say\Nit's going to be 6, but Dialogue: 0,0:29:29.53,0:29:31.78,Default,,0000,0000,0000,,assume this loop is looping through a\Nmillion Dialogue: 0,0:29:31.78,0:29:33.34,Default,,0000,0000,0000,,lines in the file or something like that. Dialogue: 0,0:29:33.34,0:29:38.60,Default,,0000,0000,0000,,So it's [SOUND], so it's cheating to\Nkind of look at it and say, ooh, it's 6, Dialogue: 0,0:29:38.60,0:29:41.52,Default,,0000,0000,0000,,because we want to actually compute it.\NSo it's really simple. Dialogue: 0,0:29:41.52,0:29:45.89,Default,,0000,0000,0000,,You know, zork starts at 0.\NIt's going to run zork is 1 now, and Dialogue: 0,0:29:45.89,0:29:52.29,Default,,0000,0000,0000,,2, 3, 4, 5, 6, and then we've run out of\Nstuff and then we print out 6. Dialogue: 0,0:29:52.29,0:29:53.00,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:29:53.00,0:29:54.26,Default,,0000,0000,0000,,So that's kind of the idiom, right? Dialogue: 0,0:29:54.26,0:29:59.16,Default,,0000,0000,0000,,Before, during, and after, right?\NWe do something before, we do Dialogue: 0,0:29:59.16,0:30:05.09,Default,,0000,0000,0000,,something during, and, and in a sense this\Nzork here is the number we've seen so far. Dialogue: 0,0:30:05.09,0:30:07.26,Default,,0000,0000,0000,,And at the end it becomes kind of the\Ntotal number. Dialogue: 0,0:30:10.09,0:30:12.36,Default,,0000,0000,0000,,Summing in a loop, very similar. Dialogue: 0,0:30:12.36,0:30:16.41,Default,,0000,0000,0000,,Again, you have to think of this is, there\Nis a whole bunch of variables here. Dialogue: 0,0:30:16.41,0:30:18.86,Default,,0000,0000,0000,,We start a variable at 0. Dialogue: 0,0:30:18.86,0:30:22.33,Default,,0000,0000,0000,,Each time through the loop, we add\Nwhatever it is that we're seeing. Dialogue: 0,0:30:22.33,0:30:27.62,Default,,0000,0000,0000,,Instead of adding 1 we're\Nadding 9, 41, 12, 3, 74, 15. Dialogue: 0,0:30:27.62,0:30:34.29,Default,,0000,0000,0000,,And zork would be best thought of as\Nrunning total. Dialogue: 0,0:30:34.29,0:30:35.91,Default,,0000,0000,0000,,So, zork is the running total. Dialogue: 0,0:30:35.91,0:30:38.61,Default,,0000,0000,0000,,And so if we look at the numbers 9, Dialogue: 0,0:30:38.61,0:30:43.50,Default,,0000,0000,0000,,running total's 9, running total's 50, \Nrunning total's 62, 65, 139, 154. Dialogue: 0,0:30:43.50,0:30:47.79,Default,,0000,0000,0000,,And then we skip out and at the end, the\Nrunning total becomes the total. Dialogue: 0,0:30:49.15,0:30:50.33,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:30:50.33,0:30:53.35,Default,,0000,0000,0000,,So, that's another of these patterns\Nthat, sort of, Dialogue: 0,0:30:53.35,0:30:54.73,Default,,0000,0000,0000,,we do something at the beginning, we Dialogue: 0,0:30:54.73,0:30:57.88,Default,,0000,0000,0000,,do something in the middle, and we have Dialogue: 0,0:30:57.88,0:31:00.92,Default,,0000,0000,0000,,something very nice for ourselves\Nat the end. Dialogue: 0,0:31:04.80,0:31:07.32,Default,,0000,0000,0000,,Finding the average of a sequence of\Nvalues Dialogue: 0,0:31:07.32,0:31:10.74,Default,,0000,0000,0000,,is the combination of the\Ntwo previous patterns. Dialogue: 0,0:31:10.74,0:31:14.19,Default,,0000,0000,0000,,This time I am going to use\Nmore mnemonic variables. Dialogue: 0,0:31:14.19,0:31:15.85,Default,,0000,0000,0000,,A variable called count. Dialogue: 0,0:31:15.85,0:31:17.56,Default,,0000,0000,0000,,Everyone calls this count. Dialogue: 0,0:31:17.56,0:31:20.98,Default,,0000,0000,0000,,Sum, now total would maybe be\Na better word for this. Dialogue: 0,0:31:20.98,0:31:25.18,Default,,0000,0000,0000,,The running total. And then, so the count\Nand the sum start out at 0, and Dialogue: 0,0:31:25.18,0:31:30.33,Default,,0000,0000,0000,,then each time through the loop, count equals\Ncount plus 1, so we're adding 1 to count. Dialogue: 0,0:31:30.33,0:31:33.62,Default,,0000,0000,0000,,Sum equal sum plus value, so we're\Nadding 1 to to sum. Dialogue: 0,0:31:33.62,0:31:35.12,Default,,0000,0000,0000,,I mean adding the value. Dialogue: 0,0:31:35.12,0:31:39.31,Default,,0000,0000,0000,,Value of course being\N9, 41, 12, 3, 74, 15. Dialogue: 0,0:31:39.31,0:31:42.39,Default,,0000,0000,0000,,And then at the very end we can\Nprint out the number. Dialogue: 0,0:31:42.39,0:31:47.19,Default,,0000,0000,0000,,We have six things with a total of 154,\Nand then we calculate the average. Dialogue: 0,0:31:47.19,0:31:51.80,Default,,0000,0000,0000,,Of course these are integer numbers, and\Nso this is a truncating division. Dialogue: 0,0:31:51.80,0:31:55.72,Default,,0000,0000,0000,,So 154 over 6 equals 25 and Dialogue: 0,0:31:55.72,0:31:58.83,Default,,0000,0000,0000,,not 25 point something. Dialogue: 0,0:31:58.83,0:32:01.91,Default,,0000,0000,0000,,In Python 3000, Python 3, it'd be better. Dialogue: 0,0:32:01.91,0:32:08.00,Default,,0000,0000,0000,,But so the average, the integer average is\Nof the numbers we just looked at, is 25. Dialogue: 0,0:32:08.00,0:32:11.61,Default,,0000,0000,0000,,So sometimes we're searching, like\Nfor a needle in a haystack. Dialogue: 0,0:32:11.61,0:32:15.46,Default,,0000,0000,0000,,Looking for something and again\Nyou have to think of like Dialogue: 0,0:32:15.46,0:32:18.61,Default,,0000,0000,0000,,you're handed some amount of data and you\Ngotta hunt for something. Dialogue: 0,0:32:18.61,0:32:20.98,Default,,0000,0000,0000,,And there might be a million things and\Nyou might only want five of them. Dialogue: 0,0:32:20.98,0:32:22.45,Default,,0000,0000,0000,,And you can either look by hand\Nor you can write Dialogue: 0,0:32:22.45,0:32:25.43,Default,,0000,0000,0000,,a loop that's got an if statement that\Nsays found it. Dialogue: 0,0:32:25.43,0:32:28.30,Default,,0000,0000,0000,,Maybe I found it at line seven\Nor found it wherever. Dialogue: 0,0:32:28.30,0:32:31.62,Default,,0000,0000,0000,,So this is filtering or finding or\Nsearching, looking Dialogue: 0,0:32:31.62,0:32:33.83,Default,,0000,0000,0000,,for a needle in a haystack, in a loop. Dialogue: 0,0:32:34.85,0:32:38.21,Default,,0000,0000,0000,,And so the, the idea basically is\Nthat we have this loop, Dialogue: 0,0:32:38.21,0:32:42.86,Default,,0000,0000,0000,,it's going to go through all the\Nvalues, 9, 41, 12, 3, 74. Dialogue: 0,0:32:42.86,0:32:46.29,Default,,0000,0000,0000,,But we put in the loop, we embed\Nan if statement. Dialogue: 0,0:32:46.29,0:32:49.30,Default,,0000,0000,0000,,If the value we're looking at is greater\Nthan 20, print I found it. Dialogue: 0,0:32:50.39,0:32:57.76,Default,,0000,0000,0000,,So when value is 9, this is going to do\Nnothing and just go and make value be 41. Dialogue: 0,0:32:57.76,0:33:02.50,Default,,0000,0000,0000,,And then value 41, oop, yep, there we go,\Nprint Large number, so off this comes. Dialogue: 0,0:33:03.84,0:33:07.75,Default,,0000,0000,0000,,Then value becomes 12, nothing happens,\Nvalue becomes 3, nothing happens, Dialogue: 0,0:33:07.75,0:33:11.58,Default,,0000,0000,0000,,value becomes 74, oops, this\Ntime it's going to happen. Dialogue: 0,0:33:11.58,0:33:13.92,Default,,0000,0000,0000,,So out comes Large number 74. Dialogue: 0,0:33:13.92,0:33:18.55,Default,,0000,0000,0000,,Then value becomes 15, nothing happens,\Nand then value is all done, Dialogue: 0,0:33:18.55,0:33:20.61,Default,,0000,0000,0000,,and so it comes and finishes. Dialogue: 0,0:33:20.61,0:33:26.26,Default,,0000,0000,0000,,So this is the searching or\Nfiltering or catching or, or whatever. Dialogue: 0,0:33:26.26,0:33:28.47,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:33:29.47,0:33:31.50,Default,,0000,0000,0000,,We can also sort of, if we don't just want Dialogue: 0,0:33:31.50,0:33:34.45,Default,,0000,0000,0000,,to print everything out, we want to\Nsay is something in there. Dialogue: 0,0:33:34.45,0:33:36.15,Default,,0000,0000,0000,,Go look through this million things and Dialogue: 0,0:33:36.15,0:33:38.83,Default,,0000,0000,0000,,tell me if blah exists. Dialogue: 0,0:33:38.83,0:33:42.15,Default,,0000,0000,0000,,And in this we're going to introduce the\Nnotion of Boolean variable. Dialogue: 0,0:33:42.15,0:33:43.82,Default,,0000,0000,0000,,A Boolean is a true-false. Dialogue: 0,0:33:43.82,0:33:47.20,Default,,0000,0000,0000,,It only has two values and we've\Nalready used it in the while True. Dialogue: 0,0:33:47.20,0:33:56.54,Default,,0000,0000,0000,,So that capital True, that is a constant,\Njust like 7 or 42 or 99, or "Sam". Dialogue: 0,0:33:56.54,0:33:58.41,Default,,0000,0000,0000,,And so we're going to make this\Nvariable called found. Dialogue: 0,0:33:58.41,0:34:02.51,Default,,0000,0000,0000,,Now found is a mnemonic value, variable.\NIt's just a name I picked. Dialogue: 0,0:34:02.51,0:34:04.22,Default,,0000,0000,0000,,So found equals False. Dialogue: 0,0:34:04.22,0:34:06.38,Default,,0000,0000,0000,,This is going to be false,\Nuntil we find what Dialogue: 0,0:34:06.38,0:34:08.88,Default,,0000,0000,0000,,we're looking for and then it's\Ngoing to switch to true. Dialogue: 0,0:34:08.88,0:34:11.10,Default,,0000,0000,0000,,So it starts out and it's false. Dialogue: 0,0:34:11.10,0:34:13.87,Default,,0000,0000,0000,,Then we're going to run this\Nbit of code three times. Dialogue: 0,0:34:15.76,0:34:18.54,Default,,0000,0000,0000,,And if the value that we are looking at\Nis 3, then we're going Dialogue: 0,0:34:18.54,0:34:23.43,Default,,0000,0000,0000,,to set found to be true, and we'll print\Nfound value each time through. Dialogue: 0,0:34:23.43,0:34:27.80,Default,,0000,0000,0000,,So value's going to take on 9, 41, 12, 3,\N74, so we get a line Dialogue: 0,0:34:27.80,0:34:30.90,Default,,0000,0000,0000,,of output for each one. And the\Nfirst time through Dialogue: 0,0:34:30.90,0:34:33.80,Default,,0000,0000,0000,,it's not yet found, because we're\Nlooking at a 9. Dialogue: 0,0:34:33.80,0:34:35.27,Default,,0000,0000,0000,,Second time, it's not yet found. Dialogue: 0,0:34:35.27,0:34:38.83,Default,,0000,0000,0000,,We looked at 41, still false. So it could\Nstay false for long time. Dialogue: 0,0:34:39.94,0:34:41.18,Default,,0000,0000,0000,,Oh, we found a true. Dialogue: 0,0:34:41.18,0:34:43.36,Default,,0000,0000,0000,,And then that means that this code is\Ngoing to run once. Dialogue: 0,0:34:43.36,0:34:46.31,Default,,0000,0000,0000,,And so you can kind of think of this\Nfound variable as sticky. Dialogue: 0,0:34:46.31,0:34:49.34,Default,,0000,0000,0000,,It's going to stay false, and then\Nthe rest of the loop Dialogue: 0,0:34:49.34,0:34:52.59,Default,,0000,0000,0000,,it's going to stay true, and at\Nthe end it is true. Dialogue: 0,0:34:52.59,0:34:53.86,Default,,0000,0000,0000,,Now the way we usually do these\Nkinds of things Dialogue: 0,0:34:53.86,0:34:54.75,Default,,0000,0000,0000,,is we don't bother with this\Nprint statement, Dialogue: 0,0:34:54.75,0:34:57.97,Default,,0000,0000,0000,,so we wouldn't see all this stuff. Dialogue: 0,0:34:57.97,0:35:00.35,Default,,0000,0000,0000,,All we'd see is Before False, After True. Dialogue: 0,0:35:00.35,0:35:02.80,Default,,0000,0000,0000,,And After would just tell us \Nthat yeah, we found it. Dialogue: 0,0:35:02.80,0:35:06.13,Default,,0000,0000,0000,,There was a 3 somewhere in\Nthat long list of numbers. Dialogue: 0,0:35:06.13,0:35:08.15,Default,,0000,0000,0000,,Okay? I am just adding this print\Nstatement so we can Dialogue: 0,0:35:08.15,0:35:13.53,Default,,0000,0000,0000,,kind of trace it. But basically this\Nloop, sort of from here to here, is asking Dialogue: 0,0:35:13.53,0:35:18.13,Default,,0000,0000,0000,,the question, is there the number 3 in the\Nlist that we're about to go through? Dialogue: 0,0:35:19.26,0:35:20.36,Default,,0000,0000,0000,,Okay?\NNow... Dialogue: 0,0:35:21.87,0:35:25.86,Default,,0000,0000,0000,,How could, I'll just give you a second and\Nask you a quick question. Dialogue: 0,0:35:25.86,0:35:27.00,Default,,0000,0000,0000,,You can pause if you want. Dialogue: 0,0:35:29.13,0:35:32.36,Default,,0000,0000,0000,,How could you improve this loop\Nusing the break? Dialogue: 0,0:35:32.36,0:35:34.84,Default,,0000,0000,0000,,Where might you put a break\Nto make this loop smarter? Dialogue: 0,0:35:34.84,0:35:41.23,Default,,0000,0000,0000,,[SOUND]. Dialogue: 0,0:35:41.23,0:35:44.38,Default,,0000,0000,0000,,It's okay if you didn't, if it\Ndoesn't jump out at you. Dialogue: 0,0:35:44.38,0:35:46.88,Default,,0000,0000,0000,,So, if you think about it. Dialogue: 0,0:35:46.88,0:35:51.31,Default,,0000,0000,0000,,Once you hit true, there's really little\Npoint in looking at the rest of them. Dialogue: 0,0:35:51.31,0:35:55.31,Default,,0000,0000,0000,,There just is no point.\NSo we could put a break right here. Dialogue: 0,0:35:56.58,0:36:01.19,Default,,0000,0000,0000,,Inside this block.\NYou'd say, look, I'm looking for a 3. Dialogue: 0,0:36:01.19,0:36:03.24,Default,,0000,0000,0000,,All I care is whether I found it or not. Dialogue: 0,0:36:03.24,0:36:08.44,Default,,0000,0000,0000,,If I find it, I mark it to true that I\Nfound it, and I get out of the loop. Dialogue: 0,0:36:08.44,0:36:09.53,Default,,0000,0000,0000,,Why bother? Dialogue: 0,0:36:09.53,0:36:10.82,Default,,0000,0000,0000,,Why do all these things? Dialogue: 0,0:36:10.82,0:36:13.10,Default,,0000,0000,0000,,Right, just get out.\NOkay? Dialogue: 0,0:36:13.10,0:36:14.99,Default,,0000,0000,0000,,So don't worry about it. Dialogue: 0,0:36:14.99,0:36:18.49,Default,,0000,0000,0000,,I'm just pointing that out, that's one of\Nthe places where break could be used. Dialogue: 0,0:36:18.49,0:36:21.66,Default,,0000,0000,0000,,The loop functions either way it just, it\Njust loops through all the rest Dialogue: 0,0:36:21.66,0:36:22.25,Default,,0000,0000,0000,,of them as well. Dialogue: 0,0:36:24.61,0:36:26.54,Default,,0000,0000,0000,,Okay. So. Dialogue: 0,0:36:27.73,0:36:33.59,Default,,0000,0000,0000,,Here's this largest value one I've used\Nbefore and you know, away we go. Dialogue: 0,0:36:33.59,0:36:36.51,Default,,0000,0000,0000,,We, you know, we have\Nlargest_so_far, we check to see Dialogue: 0,0:36:36.51,0:36:38.31,Default,,0000,0000,0000,,if the one we're looking at is better Dialogue: 0,0:36:38.31,0:36:44.61,Default,,0000,0000,0000,,and if it is we keep it and then away we\Ngo and we find that the largest is 17. Dialogue: 0,0:36:44.61,0:36:45.73,Default,,0000,0000,0000,,What if... Dialogue: 0,0:36:46.84,0:36:49.96,Default,,0000,0000,0000,,What would you have to change in this Dialogue: 0,0:36:49.96,0:36:53.74,Default,,0000,0000,0000,,code to make this search for the\Nsmallest of all the values? Dialogue: 0,0:36:56.21,0:36:58.19,Default,,0000,0000,0000,,Like point, point where in the screen. Dialogue: 0,0:36:59.67,0:37:02.87,Default,,0000,0000,0000,,Where, what would you have to change to Dialogue: 0,0:37:02.87,0:37:05.44,Default,,0000,0000,0000,,make this look for the smallest\Nin the list of values? Dialogue: 0,0:37:07.21,0:37:11.90,Default,,0000,0000,0000,,What is the nature of what's making this\Nabout being largest? Dialogue: 0,0:37:11.90,0:37:14.25,Default,,0000,0000,0000,,What would you change?\NOkay. Dialogue: 0,0:37:16.26,0:37:16.98,Default,,0000,0000,0000,,Pause if you like. Dialogue: 0,0:37:19.49,0:37:24.14,Default,,0000,0000,0000,,So here is some things that you might do\Nto make it work about the smallest. Dialogue: 0,0:37:24.14,0:37:25.67,Default,,0000,0000,0000,,So, hey, one thing we would do. Dialogue: 0,0:37:25.67,0:37:29.69,Default,,0000,0000,0000,,Let's change the name of the variable.\NWe had a variable named largest_so_far. Dialogue: 0,0:37:29.69,0:37:32.24,Default,,0000,0000,0000,,And now we'll change it to be called\Nsmallest_so_far. Dialogue: 0,0:37:33.84,0:37:36.50,Default,,0000,0000,0000,,Changing the variable name doesn't\Nchange the program at all. Dialogue: 0,0:37:36.50,0:37:38.48,Default,,0000,0000,0000,,But it makes the program easier to read. Dialogue: 0,0:37:38.48,0:37:40.23,Default,,0000,0000,0000,,If the program works. Dialogue: 0,0:37:40.23,0:37:41.94,Default,,0000,0000,0000,,So, it's like smallest so far. Dialogue: 0,0:37:41.94,0:37:44.46,Default,,0000,0000,0000,,Okay, but that didn't make it\Nabout being small. Dialogue: 0,0:37:44.46,0:37:47.32,Default,,0000,0000,0000,,The thing that made it about being small Dialogue: 0,0:37:47.32,0:37:50.03,Default,,0000,0000,0000,,is change this greater than to a less than. Dialogue: 0,0:37:51.25,0:37:54.58,Default,,0000,0000,0000,,Because we're kind of thinking when\Nwe're doing largest so far, if the Dialogue: 0,0:37:54.58,0:37:59.02,Default,,0000,0000,0000,,number we're looking at is bigger than\Nthe largest so far, we keep it. Dialogue: 0,0:37:59.02,0:38:01.21,Default,,0000,0000,0000,,If the number we're looking at in\Nthe smallest is Dialogue: 0,0:38:01.21,0:38:03.44,Default,,0000,0000,0000,,smaller than the smallest so far,\Nthen we want to keep it. Dialogue: 0,0:38:03.44,0:38:04.08,Default,,0000,0000,0000,,So this is like, Dialogue: 0,0:38:06.31,0:38:09.79,Default,,0000,0000,0000,,this line here is the keeping line\Nand this is the when line. Dialogue: 0,0:38:09.79,0:38:13.75,Default,,0000,0000,0000,,When to keep. Dialogue: 0,0:38:13.75,0:38:15.37,Default,,0000,0000,0000,,We'll keep it if it's smaller. Dialogue: 0,0:38:16.42,0:38:17.72,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:38:17.72,0:38:20.90,Default,,0000,0000,0000,,So that's the key, and so yeah,\Nso I name it smallest_so_far, Dialogue: 0,0:38:20.90,0:38:24.68,Default,,0000,0000,0000,,whoop de do. That's good.\NBut the real thing that Dialogue: 0,0:38:24.68,0:38:27.93,Default,,0000,0000,0000,,had this being about largeness and\Nsmallness was whether this, this less Dialogue: 0,0:38:27.93,0:38:31.49,Default,,0000,0000,0000,,than and greater than and this was the\Nrepeated code that got re-checked Dialogue: 0,0:38:31.49,0:38:35.59,Default,,0000,0000,0000,,over and over again.\NSo, but this still has a bug in it. Dialogue: 0,0:38:36.75,0:38:39.32,Default,,0000,0000,0000,,So let's run this visually. Dialogue: 0,0:38:41.59,0:38:42.33,Default,,0000,0000,0000,,Okay. Dialogue: 0,0:38:42.33,0:38:44.61,Default,,0000,0000,0000,,So now we've got a variable called\Nsmallest so far. Dialogue: 0,0:38:45.90,0:38:48.23,Default,,0000,0000,0000,,We are going to check to see if\Na series of numbers that Dialogue: 0,0:38:48.23,0:38:51.00,Default,,0000,0000,0000,,I'm about to show you are smaller\Nthan the smallest so far. Dialogue: 0,0:38:53.13,0:38:57.38,Default,,0000,0000,0000,,So the first number is 9.\NIs that smaller than negative 1? Dialogue: 0,0:38:59.24,0:39:01.76,Default,,0000,0000,0000,,No, it's not.\NNegative 1 is smaller. Dialogue: 0,0:39:01.76,0:39:06.10,Default,,0000,0000,0000,,The second number is 41.\NIs that smaller than negative 1? Dialogue: 0,0:39:06.10,0:39:07.69,Default,,0000,0000,0000,,No, it is not. Dialogue: 0,0:39:07.69,0:39:10.41,Default,,0000,0000,0000,,The next number is 12.\NIs that smaller than negative 1? Dialogue: 0,0:39:10.41,0:39:11.31,Default,,0000,0000,0000,,No. Dialogue: 0,0:39:11.31,0:39:14.18,Default,,0000,0000,0000,,Negative 1 is smaller than 12. Dialogue: 0,0:39:14.18,0:39:17.29,Default,,0000,0000,0000,,3? No, not smaller. Dialogue: 0,0:39:17.29,0:39:20.67,Default,,0000,0000,0000,,74? No, not smaller. Dialogue: 0,0:39:20.67,0:39:23.16,Default,,0000,0000,0000,,15? Not smaller. Dialogue: 0,0:39:23.16,0:39:25.89,Default,,0000,0000,0000,,So, we're all done. Dialogue: 0,0:39:25.89,0:39:29.59,Default,,0000,0000,0000,,Yay, and the smallest number we saw\Nin the list is... Dialogue: 0,0:39:32.00,0:39:35.70,Default,,0000,0000,0000,,Negative 1?\NNegative 1 wasn't even in the list. Dialogue: 0,0:39:35.70,0:39:38.19,Default,,0000,0000,0000,,So that's not a very good program. Dialogue: 0,0:39:41.74,0:39:44.88,Default,,0000,0000,0000,,So let's take a look at what went wrong\Nwith this program. Dialogue: 0,0:39:44.88,0:39:46.99,Default,,0000,0000,0000,,So we fixed it, we fixed it as\Nbest we could. Right? Dialogue: 0,0:39:48.05,0:39:52.59,Default,,0000,0000,0000,,We made it, we changed the words\Nlargest to smallest and yay, that'll fix. Dialogue: 0,0:39:52.59,0:39:54.92,Default,,0000,0000,0000,,Just makes it more readable, doesn't\Nactually change the program. Dialogue: 0,0:39:54.92,0:39:58.53,Default,,0000,0000,0000,,And we made this less than, so now what\Nhappens is it comes in, Dialogue: 0,0:39:58.53,0:40:03.25,Default,,0000,0000,0000,,if 3 is less than negative 1, smallest so\Nfar of course is negative 1. Dialogue: 0,0:40:03.25,0:40:05.51,Default,,0000,0000,0000,,It, this just never runs. Dialogue: 0,0:40:05.51,0:40:07.01,Default,,0000,0000,0000,,This never runs. And so Dialogue: 0,0:40:07.01,0:40:12.09,Default,,0000,0000,0000,,as we print, smallest_so_far stays\Nnegative 1 and oops, that Dialogue: 0,0:40:12.09,0:40:18.42,Default,,0000,0000,0000,,should be negative 1, right there.\N[LAUGH] I'm sorry, I forgot to fix that. Dialogue: 0,0:40:18.42,0:40:20.52,Default,,0000,0000,0000,,Here, let me magically fix that. Dialogue: 0,0:40:21.95,0:40:23.52,Default,,0000,0000,0000,,Boom. [SNAP] Dialogue: 0,0:40:23.52,0:40:27.34,Default,,0000,0000,0000,,So let's take a look at what\Nwent wrong with this. Dialogue: 0,0:40:27.34,0:40:29.94,Default,,0000,0000,0000,,So here we have the code.\Nsmallest_so_far is negative 1. Dialogue: 0,0:40:29.94,0:40:32.38,Default,,0000,0000,0000,,We have it fixed so we're checking,\Nlooking for smaller Dialogue: 0,0:40:32.38,0:40:36.06,Default,,0000,0000,0000,,numbers rather than larger numbers, by\Nturning this to less than. Dialogue: 0,0:40:36.06,0:40:43.11,Default,,0000,0000,0000,,But the first time through, \Nsmallest_so_far is negative 1 and the_num is 3. Dialogue: 0,0:40:43.11,0:40:48.17,Default,,0000,0000,0000,,3 is not less than negative 1, so we skip\Nthrough and the printout of the Dialogue: 0,0:40:48.17,0:40:50.30,Default,,0000,0000,0000,,first line is negative 1 3. Dialogue: 0,0:40:50.30,0:40:54.32,Default,,0000,0000,0000,,And it doesn't take long to realize that\Nit's just going to keep doing this. Dialogue: 0,0:40:54.32,0:40:57.38,Default,,0000,0000,0000,,smallest_so_far is going to stay\Nnegative 1, Dialogue: 0,0:40:57.38,0:40:59.62,Default,,0000,0000,0000,,no matter what we look at on this side. Dialogue: 0,0:40:59.62,0:41:01.09,Default,,0000,0000,0000,,And then we're going to come out\Nat the end. Dialogue: 0,0:41:02.24,0:41:07.22,Default,,0000,0000,0000,,And we end up with negative 1 as the\Nanswer. Not very good. Dialogue: 0,0:41:07.22,0:41:09.91,Default,,0000,0000,0000,,[SIGH]. Dialogue: 0,0:41:09.91,0:41:10.81,Default,,0000,0000,0000,,So the question is... Dialogue: 0,0:41:14.09,0:41:18.22,Default,,0000,0000,0000,,What should we make this value be?\NNegative 1? Dialogue: 0,0:41:18.22,0:41:21.21,Default,,0000,0000,0000,,It barely worked in the largest because we\Nwere working with positive Dialogue: 0,0:41:21.21,0:41:24.32,Default,,0000,0000,0000,,numbers and so starting with negative 1\Nas the largest so far Dialogue: 0,0:41:24.32,0:41:27.29,Default,,0000,0000,0000,,was a reasonable assumption as\Nlong as the numbers were all positive. Dialogue: 0,0:41:27.29,0:41:29.56,Default,,0000,0000,0000,,[SOUND]. Dialogue: 0,0:41:29.56,0:41:32.84,Default,,0000,0000,0000,,But what would be the number\Nto choose here? Dialogue: 0,0:41:32.84,0:41:34.00,Default,,0000,0000,0000,,Think about that for a second. Dialogue: 0,0:41:34.00,0:41:35.35,Default,,0000,0000,0000,,Pause if you have to. Dialogue: 0,0:41:35.35,0:41:38.57,Default,,0000,0000,0000,,Let me clear it.\NLet me make it real clear. Dialogue: 0,0:41:38.57,0:41:41.70,Default,,0000,0000,0000,,[SOUND].\NWhat's the right thing Dialogue: 0,0:41:41.70,0:41:46.39,Default,,0000,0000,0000,,to put here? Okay. Dialogue: 0,0:41:46.39,0:41:52.81,Default,,0000,0000,0000,,So, what? A million? Dialogue: 0,0:41:52.81,0:41:58.52,Default,,0000,0000,0000,,That might work, a million might work.\NBut what if this number, you know, Dialogue: 0,0:41:58.52,0:42:04.21,Default,,0000,0000,0000,,was, you know, what if, what if all\Nthese numbers were larger Dialogue: 0,0:42:04.21,0:42:07.02,Default,,0000,0000,0000,,than a million, okay?\NThen, then that wouldn't work. Dialogue: 0,0:42:07.02,0:42:12.20,Default,,0000,0000,0000,,So the problem is there's no real good\Nvalue, unless you could make this be Dialogue: 0,0:42:12.20,0:42:17.10,Default,,0000,0000,0000,,somehow infinity, okay?\NYou could make this be infinity. Dialogue: 0,0:42:18.31,0:42:24.01,Default,,0000,0000,0000,,But there is a way to do this in Python.\NAnd it's a really kind of cool technique. Dialogue: 0,0:42:24.01,0:42:26.73,Default,,0000,0000,0000,,It's sort of a way we signal ourselves, Dialogue: 0,0:42:26.73,0:42:29.55,Default,,0000,0000,0000,,and that is we're going to use\Na special value. Dialogue: 0,0:42:29.55,0:42:30.32,Default,,0000,0000,0000,,Not negative 1. Dialogue: 0,0:42:30.32,0:42:34.34,Default,,0000,0000,0000,,It's not a number. And the special value\Nwe're going to use is None. Dialogue: 0,0:42:35.89,0:42:41.16,Default,,0000,0000,0000,,It's a different type. It's not a number,\Nit's itself its own type. Dialogue: 0,0:42:41.16,0:42:43.75,Default,,0000,0000,0000,,So what we're going to do is mark\Nsmallest as "None". Dialogue: 0,0:42:43.75,0:42:45.90,Default,,0000,0000,0000,,And, and, and sort of at a high level what Dialogue: 0,0:42:45.90,0:42:49.99,Default,,0000,0000,0000,,we're really saying is we haven't seen\Nanything so far. Dialogue: 0,0:42:49.99,0:42:52.94,Default,,0000,0000,0000,,The smallest we've seen so far is none. Dialogue: 0,0:42:52.94,0:42:55.14,Default,,0000,0000,0000,,We've not seen anything so far. Dialogue: 0,0:42:55.14,0:42:58.02,Default,,0000,0000,0000,,Now we have to change our loop,\Nour little if inside the loop. Dialogue: 0,0:42:58.02,0:43:00.48,Default,,0000,0000,0000,,This is this intelligence in the middle. Dialogue: 0,0:43:00.48,0:43:02.58,Default,,0000,0000,0000,,First we say if smallest is None. Dialogue: 0,0:43:02.58,0:43:06.30,Default,,0000,0000,0000,,is is an operator, part of the\NPython language. Dialogue: 0,0:43:06.30,0:43:10.19,Default,,0000,0000,0000,,If smallest is None, exactly the\Nsame as None, Dialogue: 0,0:43:10.19,0:43:12.43,Default,,0000,0000,0000,,then the smallest we've seen so far\Nis the value. Dialogue: 0,0:43:12.43,0:43:17.13,Default,,0000,0000,0000,,Now this is going to happen the first time\N[SOUND]. Dialogue: 0,0:43:17.13,0:43:20.27,Default,,0000,0000,0000,,Because smallest starts out None and then\Nas soon as we set smallest Dialogue: 0,0:43:20.27,0:43:23.81,Default,,0000,0000,0000,,to the value, it's going to be that first\Nnumber, so it's going to be 9. Dialogue: 0,0:43:23.81,0:43:26.95,Default,,0000,0000,0000,,Okay? So the smallest is quickly\Ngoing to become 9. Dialogue: 0,0:43:26.95,0:43:33.01,Default,,0000,0000,0000,,Then we print out the new, the smallest\Nis 9 after we've seen the 9. Dialogue: 0,0:43:33.01,0:43:37.01,Default,,0000,0000,0000,,Then we go up to the top and we say, is\Nsmallest None? Dialogue: 0,0:43:37.01,0:43:41.22,Default,,0000,0000,0000,,And the answer is no it is not,\Nbecause smallest is now 9. Dialogue: 0,0:43:41.22,0:43:46.08,Default,,0000,0000,0000,,Then this else-if is going to ask, is the\Nvalue we're looking at, which is 41, Dialogue: 0,0:43:46.08,0:43:50.65,Default,,0000,0000,0000,,is the value less than smallest?\NWell, no, it is not. Dialogue: 0,0:43:50.65,0:43:52.36,Default,,0000,0000,0000,,9 is smaller than 41. Dialogue: 0,0:43:52.36,0:43:56.09,Default,,0000,0000,0000,,And, so in a sense, after the\Nfirst time it's executed, Dialogue: 0,0:43:56.09,0:43:58.05,Default,,0000,0000,0000,,after the first time the statement\Nis executed, Dialogue: 0,0:43:58.05,0:43:59.80,Default,,0000,0000,0000,,this is going to always be false, right? Dialogue: 0,0:43:59.80,0:44:02.20,Default,,0000,0000,0000,,Because smallest is no longer None\Nand this is going to be Dialogue: 0,0:44:02.20,0:44:04.85,Default,,0000,0000,0000,,the thing that really is operating. Dialogue: 0,0:44:04.85,0:44:06.27,Default,,0000,0000,0000,,And then it's going to work. Dialogue: 0,0:44:06.27,0:44:09.38,Default,,0000,0000,0000,,And when we, you know, smallest will\Nbecome 9. Dialogue: 0,0:44:09.38,0:44:11.27,Default,,0000,0000,0000,,The smallest so far is 9, but then we see Dialogue: 0,0:44:11.27,0:44:14.96,Default,,0000,0000,0000,,the 3 finally, and the value of\Nthe 3 is less than 9. Dialogue: 0,0:44:14.96,0:44:17.52,Default,,0000,0000,0000,,And so then we take 3 and we stick it into Dialogue: 0,0:44:17.52,0:44:21.56,Default,,0000,0000,0000,,smallest and we end up with this,\Nand then the loop Dialogue: 0,0:44:21.56,0:44:24.32,Default,,0000,0000,0000,,runs some more times, and when\Nwe're all done we have 3. Dialogue: 0,0:44:25.51,0:44:29.86,Default,,0000,0000,0000,,So the trick here is we put this\NNone in, and we have a Dialogue: 0,0:44:29.86,0:44:33.51,Default,,0000,0000,0000,,little more if code to check to see\Nif we haven't seen anything so far. Dialogue: 0,0:44:33.51,0:44:35.58,Default,,0000,0000,0000,,This is what, you can think of this Dialogue: 0,0:44:35.58,0:44:41.20,Default,,0000,0000,0000,,as a way to trigger on the first,\Nfirst iteration. Dialogue: 0,0:44:41.20,0:44:43.56,Default,,0000,0000,0000,,Special code that's really going to, it\Ncould, it looks at it Dialogue: 0,0:44:43.56,0:44:46.50,Default,,0000,0000,0000,,on each iteration, but it's never true\Nafter the first iteration. Dialogue: 0,0:44:47.53,0:44:49.41,Default,,0000,0000,0000,,Okay? So that's just a technique. Dialogue: 0,0:44:52.15,0:44:54.84,Default,,0000,0000,0000,,So this is and the is not operator,\NI think it's a real elegant thing. Dialogue: 0,0:44:54.84,0:45:01.76,Default,,0000,0000,0000,,Don't start overusing it. It's,\Nat a low level Dialogue: 0,0:45:01.76,0:45:05.78,Default,,0000,0000,0000,,its real meaning is exactly the\Nsame as in type and value. Dialogue: 0,0:45:07.57,0:45:11.04,Default,,0000,0000,0000,,There's an is and there's an\Nis not, but don't, like, Dialogue: 0,0:45:11.04,0:45:15.53,Default,,0000,0000,0000,,say, like, if, don't do things like\Nsaying if i equals. Dialogue: 0,0:45:15.53,0:45:20.07,Default,,0000,0000,0000,,Oops.\NI won't even let myself type the bad code. Dialogue: 0,0:45:20.07,0:45:22.85,Default,,0000,0000,0000,,if i is 4. Dialogue: 0,0:45:22.85,0:45:26.03,Default,,0000,0000,0000,,Don't say that, okay? Don't say that. Dialogue: 0,0:45:26.03,0:45:27.68,Default,,0000,0000,0000,,Don't do if i is 4. Dialogue: 0,0:45:30.49,0:45:34.08,Default,,0000,0000,0000,,It may work in certain situations.\NIt's really best used in very limited Dialogue: 0,0:45:34.08,0:45:36.56,Default,,0000,0000,0000,,situations where you're checking\Nfor some of these Dialogue: 0,0:45:36.56,0:45:40.74,Default,,0000,0000,0000,,special values like None and False. Dialogue: 0,0:45:40.74,0:45:41.57,Default,,0000,0000,0000,,Okay. Dialogue: 0,0:45:41.57,0:45:45.81,Default,,0000,0000,0000,,The problem is if you use equality here,\Nit tries to kind of Dialogue: 0,0:45:45.81,0:45:50.59,Default,,0000,0000,0000,,convert values and it may end up\Ngiving you a false yes. Dialogue: 0,0:45:50.59,0:45:54.45,Default,,0000,0000,0000,,And so is is a stronger equality than\Nsimple equals. Dialogue: 0,0:45:56.19,0:46:02.71,Default,,0000,0000,0000,,Equals is same value, same numeric value,\Nwhereas is is exactly the same thing. Dialogue: 0,0:46:02.71,0:46:05.98,Default,,0000,0000,0000,,But don't, don't overuse is. Use\Ndouble equals Dialogue: 0,0:46:05.98,0:46:09.14,Default,,0000,0000,0000,,95% of the time and use is when you're Dialogue: 0,0:46:09.14,0:46:12.53,Default,,0000,0000,0000,,checking if it's one of these special\Nconstants like True or False. Dialogue: 0,0:46:13.89,0:46:14.39,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:46:16.02,0:46:19.34,Default,,0000,0000,0000,,Okay.\NSo this is a iterations. Dialogue: 0,0:46:19.34,0:46:21.19,Default,,0000,0000,0000,,I mean our loops are going to get more\Nsophisticated and Dialogue: 0,0:46:21.19,0:46:23.57,Default,,0000,0000,0000,,we have more interesting things\Nto do, but we, Dialogue: 0,0:46:23.57,0:46:24.94,Default,,0000,0000,0000,,you know, we talked about some Dialogue: 0,0:46:24.94,0:46:28.37,Default,,0000,0000,0000,,indefinite loops, definite loops,\Niteration variables. Dialogue: 0,0:46:28.37,0:46:33.13,Default,,0000,0000,0000,,Some patterns like maximum, minimum,\Nsumming, averaging, you know. Dialogue: 0,0:46:33.13,0:46:37.49,Default,,0000,0000,0000,,We introduced the concept of None, you\Nknow, and, and so this is. Dialogue: 0,0:46:37.49,0:46:40.32,Default,,0000,0000,0000,,We're getting there, we've got a couple\Nmore chapters before we really Dialogue: 0,0:46:40.32,0:46:43.47,Default,,0000,0000,0000,,start hitting the data analysis, so see\Nyou in the next lecture.