[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.16,0:00:04.53,Default,,0000,0000,0000,,Hello, and welcome to Chapter Eight:\NPython Lists. Dialogue: 0,0:00:04.53,0:00:08.40,Default,,0000,0000,0000,,So now we're sort of going to start taking\Ncare of business. Dialogue: 0,0:00:08.40,0:00:10.53,Default,,0000,0000,0000,,We are doing, make lists and Dialogue: 0,0:00:10.53,0:00:13.28,Default,,0000,0000,0000,,dictionaries and tuples and really start\Nmanipulating this data, Dialogue: 0,0:00:13.28,0:00:16.29,Default,,0000,0000,0000,,and doing real data analysis,\Nstarting the, Dialogue: 0,0:00:16.29,0:00:18.26,Default,,0000,0000,0000,,laying the proper work for real data\Nanalysis. Dialogue: 0,0:00:18.26,0:00:21.95,Default,,0000,0000,0000,,As always, these lectures, audio, video,\Nslides, Dialogue: 0,0:00:21.95,0:00:25.74,Default,,0000,0000,0000,,and even book are copyright Creative Commons\NAttribution. Dialogue: 0,0:00:25.74,0:00:31.03,Default,,0000,0000,0000,,So, lists, dictionaries, and tuples, the\Nnext real three big topics we're going to Dialogue: 0,0:00:31.03,0:00:36.27,Default,,0000,0000,0000,,talk about, are collections.\NAnd we've been doing lists already, right? Dialogue: 0,0:00:37.34,0:00:41.06,Default,,0000,0000,0000,,We've been doing lists when we were doing\Nfor loops. Dialogue: 0,0:00:41.06,0:00:44.00,Default,,0000,0000,0000,,A list in Python is something that has a\Nsquare braces. Dialogue: 0,0:00:44.00,0:00:45.42,Default,,0000,0000,0000,,This is a constant list. Dialogue: 0,0:00:46.55,0:00:48.41,Default,,0000,0000,0000,,Now, when I first talked to you Dialogue: 0,0:00:48.41,0:00:50.53,Default,,0000,0000,0000,,about variables, I sort of oversimplified\Nthings. Dialogue: 0,0:00:50.53,0:00:50.90,Default,,0000,0000,0000,,I said Dialogue: 0,0:00:50.90,0:00:54.16,Default,,0000,0000,0000,,if you put like x equals two, and then put Dialogue: 0,0:00:54.16,0:00:57.54,Default,,0000,0000,0000,,x equals four, the two and the four\Noverwrite each other. Dialogue: 0,0:00:57.54,0:01:01.89,Default,,0000,0000,0000,,A collection is where you can put a bunch\Nof things in the same variable. Dialogue: 0,0:01:01.89,0:01:04.13,Default,,0000,0000,0000,,Now, I have to have a way to find those\Nthings. Dialogue: 0,0:01:05.57,0:01:08.82,Default,,0000,0000,0000,,But it allows us to put multiple things in Dialogue: 0,0:01:08.82,0:01:11.81,Default,,0000,0000,0000,,more, more things, more than one thing in\Nthe variable. Dialogue: 0,0:01:11.81,0:01:15.33,Default,,0000,0000,0000,,So, here we have friends, that has three\Nstrings, Joseph, Glenn, and Sally. Dialogue: 0,0:01:15.33,0:01:15.97,Default,,0000,0000,0000,,And we have carryon Dialogue: 0,0:01:15.97,0:01:20.00,Default,,0000,0000,0000,,that has socks, shirt, and perfume.\NSo that's the basic idea. Dialogue: 0,0:01:20.00,0:01:21.68,Default,,0000,0000,0000,,So what's not a collection? Dialogue: 0,0:01:21.68,0:01:23.44,Default,,0000,0000,0000,,Well, simple variables. Dialogue: 0,0:01:23.44,0:01:26.61,Default,,0000,0000,0000,,Simple variables are not collections, just\Nlike this example. Dialogue: 0,0:01:26.61,0:01:30.19,Default,,0000,0000,0000,,I say x equals 2, x equals 4, and print x, Dialogue: 0,0:01:30.19,0:01:33.43,Default,,0000,0000,0000,,and the 4's in there and the 2 is somehow\Ngone. Dialogue: 0,0:01:33.43,0:01:35.57,Default,,0000,0000,0000,,It was there for a moment, and then it's\Ngone. Dialogue: 0,0:01:36.74,0:01:38.47,Default,,0000,0000,0000,,And so that's a normal variable. Dialogue: 0,0:01:38.47,0:01:41.49,Default,,0000,0000,0000,,They're not collections.\NYou can't put more than one thing in it. Dialogue: 0,0:01:41.49,0:01:44.22,Default,,0000,0000,0000,,But when you put more than one thing in\Nit, then you Dialogue: 0,0:01:44.22,0:01:46.53,Default,,0000,0000,0000,,have to have a way to find the things that\Nare in there. Dialogue: 0,0:01:46.53,0:01:47.32,Default,,0000,0000,0000,,We'll, we'll get to that. Dialogue: 0,0:01:49.26,0:01:51.88,Default,,0000,0000,0000,,So, we've been using list constants for\Nthe last couple Dialogue: 0,0:01:51.88,0:01:55.12,Default,,0000,0000,0000,,of chapters just because we have to use\Nlist constants. Dialogue: 0,0:01:55.12,0:01:59.04,Default,,0000,0000,0000,,You know, so we used, in the for loop\Nchapter, we did lists of numbers. Dialogue: 0,0:02:00.52,0:02:05.00,Default,,0000,0000,0000,,We have done lists of strings, that's\Nstrings, red, yellow, and blue. Dialogue: 0,0:02:06.46,0:02:11.23,Default,,0000,0000,0000,,And you don't have to necessarily, you\Ndon't necessarily Dialogue: 0,0:02:11.23,0:02:13.54,Default,,0000,0000,0000,,have to have things all of the same type. Dialogue: 0,0:02:13.54,0:02:17.68,Default,,0000,0000,0000,,This is a three-item list, that has\Na string red, Dialogue: 0,0:02:17.68,0:02:22.80,Default,,0000,0000,0000,,the number integer 24, and 98.6, which is\Na floating point number. Dialogue: 0,0:02:22.80,0:02:25.81,Default,,0000,0000,0000,,And here's an interesting thing, just as a\Nside note. Dialogue: 0,0:02:25.81,0:02:28.04,Default,,0000,0000,0000,,This shows that floating point numbers are Dialogue: 0,0:02:28.04,0:02:32.04,Default,,0000,0000,0000,,not always perfectly represented inside of\Nthe computer. Dialogue: 0,0:02:32.04,0:02:34.59,Default,,0000,0000,0000,,It's sort of an artifact of how they work. Dialogue: 0,0:02:34.59,0:02:36.88,Default,,0000,0000,0000,,And this is an example of 98.6 is really\N98 point Dialogue: 0,0:02:36.88,0:02:38.98,Default,,0000,0000,0000,,na, na, na, na, na. Dialogue: 0,0:02:38.98,0:02:41.26,Default,,0000,0000,0000,,So, but, don't, when you see something\Nlike that, don't freak out. Dialogue: 0,0:02:41.26,0:02:43.71,Default,,0000,0000,0000,,Floating point numbers are the ones that\Nshow this behavior. Dialogue: 0,0:02:44.76,0:02:48.34,Default,,0000,0000,0000,,So, interestingly, you can always,\Nalthough we won't put a lot of energy into Dialogue: 0,0:02:48.34,0:02:52.93,Default,,0000,0000,0000,,this, you can also have an element of a\Nlist be a list itself. Dialogue: 0,0:02:52.93,0:02:55.63,Default,,0000,0000,0000,,So this a outer list that's got three\Nelements. Dialogue: 0,0:02:55.63,0:02:57.71,Default,,0000,0000,0000,,1, 7, and then Dialogue: 0,0:02:57.71,0:02:59.86,Default,,0000,0000,0000,,a list that's 5 and 6. Dialogue: 0,0:02:59.86,0:03:04.47,Default,,0000,0000,0000,,So, if you look at the length of this,\Nthere is three things in it. Dialogue: 0,0:03:04.47,0:03:05.85,Default,,0000,0000,0000,,Not four, three. Dialogue: 0,0:03:05.85,0:03:08.52,Default,,0000,0000,0000,,Because the outer list has 1, 2, 3 things\Nin it. Dialogue: 0,0:03:08.52,0:03:12.48,Default,,0000,0000,0000,,And an empty list is bracket, bracket. Dialogue: 0,0:03:12.48,0:03:13.34,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:03:13.34,0:03:17.18,Default,,0000,0000,0000,,Like I said, we have been going through\Nlists all along. Dialogue: 0,0:03:17.18,0:03:19.66,Default,,0000,0000,0000,,We have iteration variables for i in. Dialogue: 0,0:03:19.66,0:03:22.20,Default,,0000,0000,0000,,This is a list.\NWe've been using it all along. Dialogue: 0,0:03:22.20,0:03:27.27,Default,,0000,0000,0000,,Similarly, we've been using lists in\Ndefinite loops, are a Dialogue: 0,0:03:27.27,0:03:30.34,Default,,0000,0000,0000,,great way to go through lists, for friend\Nin friends, there we have Dialogue: 0,0:03:30.34,0:03:34.40,Default,,0000,0000,0000,,goes through three times, out come\Nthree lines, with the Dialogue: 0,0:03:34.40,0:03:38.52,Default,,0000,0000,0000,,variable friend advancing through the\Nthree successive items in the list. Dialogue: 0,0:03:38.52,0:03:40.38,Default,,0000,0000,0000,,And away we go. Dialogue: 0,0:03:40.38,0:03:44.12,Default,,0000,0000,0000,,So, again, lists are not completely\Nforeign to us. Dialogue: 0,0:03:44.12,0:03:45.54,Default,,0000,0000,0000,,Now, Dialogue: 0,0:03:45.54,0:03:52.52,Default,,0000,0000,0000,,just like in a string, we can use the\Nindex operator, Dialogue: 0,0:03:52.52,0:03:56.99,Default,,0000,0000,0000,,the square bracket operator, and\Nwe can look up items in the list. Dialogue: 0,0:03:56.99,0:03:59.30,Default,,0000,0000,0000,,Sub one, friends, sub one. Dialogue: 0,0:04:00.33,0:04:03.78,Default,,0000,0000,0000,,Not surprisingly, using the European\Nelevator rule, Dialogue: 0,0:04:06.09,0:04:09.13,Default,,0000,0000,0000,,the first item in a list is sub zero,\Nthe second Dialogue: 0,0:04:09.13,0:04:11.57,Default,,0000,0000,0000,,item is sub one and the third one is sub\Ntwo. Dialogue: 0,0:04:11.57,0:04:15.15,Default,,0000,0000,0000,,So here when I print friends sub one I\Nget Glenn. Dialogue: 0,0:04:15.15,0:04:18.42,Default,,0000,0000,0000,,Which is the second element.\NJust like strings. Dialogue: 0,0:04:18.42,0:04:20.63,Default,,0000,0000,0000,,So once you kind of know it for strings,\Nlists Dialogue: 0,0:04:20.63,0:04:22.59,Default,,0000,0000,0000,,and the rest of these things make a lot\Nmore sense. Dialogue: 0,0:04:22.59,0:04:26.06,Default,,0000,0000,0000,,Just, remember that we're in Europe, and\Nthings start with zero. Dialogue: 0,0:04:27.76,0:04:31.81,Default,,0000,0000,0000,,Some things in these data items that we\Nwork with are not mutable. Dialogue: 0,0:04:31.81,0:04:34.42,Default,,0000,0000,0000,,So for example, strings, when we ask for a\Nlower case Dialogue: 0,0:04:34.42,0:04:37.25,Default,,0000,0000,0000,,version of a string, we're given a copy of\Nthat string. Dialogue: 0,0:04:37.25,0:04:41.55,Default,,0000,0000,0000,,And that's because strings are not\Nmutable, and we can see this Dialogue: 0,0:04:41.55,0:04:46.55,Default,,0000,0000,0000,,by doing something like saying fruit\Nsub 0 equals lowercase b. Dialogue: 0,0:04:46.55,0:04:49.62,Default,,0000,0000,0000,,Now you'd think that that would just\Nchange this Dialogue: 0,0:04:49.62,0:04:53.65,Default,,0000,0000,0000,,to be a lower case b, but it doesn't,\Nokay? Dialogue: 0,0:04:53.65,0:04:57.34,Default,,0000,0000,0000,,It says string object does not support\Nitem assignment Dialogue: 0,0:04:57.34,0:05:00.42,Default,,0000,0000,0000,,which means that you're not allowed to\Nreassign. Dialogue: 0,0:05:00.42,0:05:03.20,Default,,0000,0000,0000,,You can make a new string and put\Ndifferent things in Dialogue: 0,0:05:03.20,0:05:06.82,Default,,0000,0000,0000,,that new string, but once the strings are\Nmade, they're not changeable. Dialogue: 0,0:05:06.82,0:05:12.22,Default,,0000,0000,0000,,And that's why when we call fruit.lower, we\Nget a copy of it in lower case. Dialogue: 0,0:05:12.22,0:05:14.86,Default,,0000,0000,0000,,And so x is a copy of the original\Nstring, but Dialogue: 0,0:05:14.86,0:05:18.15,Default,,0000,0000,0000,,the original string, once we assign it\Ninto fruit, is unchanged. Dialogue: 0,0:05:18.15,0:05:19.08,Default,,0000,0000,0000,,It can't be changed. Dialogue: 0,0:05:20.34,0:05:22.38,Default,,0000,0000,0000,,Lists, on the other hand, can be changed,\Nand we Dialogue: 0,0:05:22.38,0:05:23.47,Default,,0000,0000,0000,,can change them in the middle. Dialogue: 0,0:05:23.47,0:05:26.23,Default,,0000,0000,0000,,This is one of the things we like about\Nthem. Dialogue: 0,0:05:26.23,0:05:29.32,Default,,0000,0000,0000,,So here we have a list: 2, 14, 26, 41, and\N63. Dialogue: 0,0:05:29.32,0:05:31.13,Default,,0000,0000,0000,,Then we say lotto sub two. Dialogue: 0,0:05:31.13,0:05:33.67,Default,,0000,0000,0000,,Of course, that's going to be the third\Nitem. Dialogue: 0,0:05:33.67,0:05:35.69,Default,,0000,0000,0000,,Lotto sub two is equal to 28. Dialogue: 0,0:05:35.69,0:05:38.38,Default,,0000,0000,0000,,Then we print it and we see the new number\Nthere. Dialogue: 0,0:05:38.38,0:05:41.19,Default,,0000,0000,0000,,So all this is saying is that we can\Nchange them, right? Dialogue: 0,0:05:41.19,0:05:44.64,Default,,0000,0000,0000,,Strings no, and lists yes. Dialogue: 0,0:05:44.64,0:05:47.54,Default,,0000,0000,0000,,You can change lists, but you can't change\Nstrings. Dialogue: 0,0:05:49.23,0:05:52.48,Default,,0000,0000,0000,,So the len function, we've used it for\Nseveral Dialogue: 0,0:05:52.48,0:05:55.54,Default,,0000,0000,0000,,things, we can say you know, use, len is Dialogue: 0,0:05:55.54,0:05:58.27,Default,,0000,0000,0000,,used for, for strings and it's used for\Nlists as well. Dialogue: 0,0:05:58.27,0:06:01.00,Default,,0000,0000,0000,,So the same function knows \Nwhen its Dialogue: 0,0:06:01.04,0:06:03.07,Default,,0000,0000,0000,,parameter is a string. And when its\Nparameter is a string, Dialogue: 0,0:06:03.07,0:06:05.03,Default,,0000,0000,0000,,it gives us the number of characters\Nin the string. Dialogue: 0,0:06:05.03,0:06:07.39,Default,,0000,0000,0000,,And when it is a list, it gives us Dialogue: 0,0:06:07.39,0:06:10.64,Default,,0000,0000,0000,,the number of elements in the list. Dialogue: 0,0:06:10.64,0:06:14.31,Default,,0000,0000,0000,,And just because one of them is a string,\Nit's still one element from the point Dialogue: 0,0:06:14.31,0:06:15.95,Default,,0000,0000,0000,,of view of this list. Dialogue: 0,0:06:15.95,0:06:20.92,Default,,0000,0000,0000,,So it has one, two, three, four - four\Nitems in the list, okay? Dialogue: 0,0:06:24.87,0:06:27.58,Default,,0000,0000,0000,,So, the range function is a special\Nfunction. Dialogue: 0,0:06:27.58,0:06:30.14,Default,,0000,0000,0000,,It's probably about time to talk about the\Nrange function. Dialogue: 0,0:06:31.35,0:06:34.35,Default,,0000,0000,0000,,The range function is a function that\Ngenerates a list, that Dialogue: 0,0:06:34.35,0:06:37.21,Default,,0000,0000,0000,,produces a list and gives it back to us. Dialogue: 0,0:06:37.21,0:06:38.87,Default,,0000,0000,0000,,And so you give the range function a Dialogue: 0,0:06:38.87,0:06:42.17,Default,,0000,0000,0000,,parameter, how many items you want, and\Nthe range Dialogue: 0,0:06:42.17,0:06:46.15,Default,,0000,0000,0000,,function creates and gives us back a list\Nthat Dialogue: 0,0:06:46.15,0:06:49.96,Default,,0000,0000,0000,,is four numbers starting at zero, which is\Nzero Dialogue: 0,0:06:49.96,0:06:53.97,Default,,0000,0000,0000,,up to, but not including three.\NSound familiar? Dialogue: 0,0:06:53.97,0:06:54.39,Default,,0000,0000,0000,,Yeah. Dialogue: 0,0:06:54.39,0:06:58.46,Default,,0000,0000,0000,,Zero up to but not, I mean zero up to, but\Nnot including four. Dialogue: 0,0:06:58.46,0:07:04.63,Default,,0000,0000,0000,,And, and so the same thing is true here.\NSo, we can combine the len and the range Dialogue: 0,0:07:04.63,0:07:10.07,Default,,0000,0000,0000,,to say, you know, to say okay, well len\Nfriends, that's three Dialogue: 0,0:07:10.07,0:07:15.40,Default,,0000,0000,0000,,items, and range len friends is 0, 1, 2.\NAnd it also Dialogue: 0,0:07:15.40,0:07:22.62,Default,,0000,0000,0000,,corresponds exactly to these items.\NSo we can actually use this Dialogue: 0,0:07:22.62,0:07:30.94,Default,,0000,0000,0000,,to construct loops to go through a list.\NWe already have a basic for loop, right? Dialogue: 0,0:07:30.94,0:07:34.29,Default,,0000,0000,0000,,We basically have a for loop that is our, Dialogue: 0,0:07:34.29,0:07:38.67,Default,,0000,0000,0000,,that, that said that for each friend in\Nfriends. Dialogue: 0,0:07:38.67,0:07:41.22,Default,,0000,0000,0000,,And out comes, Happy New Year, Glenn and\NJoseph. Dialogue: 0,0:07:41.22,0:07:45.07,Default,,0000,0000,0000,,If we also want to know where, what\Nposition we're at as Dialogue: 0,0:07:45.07,0:07:50.04,Default,,0000,0000,0000,,the loop progresses, we can rewrite the\Nexact same loop a different way. Dialogue: 0,0:07:50.04,0:07:52.95,Default,,0000,0000,0000,,And make i be our iteration variable. Dialogue: 0,0:07:52.95,0:07:59.25,Default,,0000,0000,0000,,And say i in range(len(friends)), that\Nturns this into zero, one, two. Dialogue: 0,0:07:59.25,0:08:01.53,Default,,0000,0000,0000,,And then i goes zero, one, two. Dialogue: 0,0:08:01.53,0:08:03.28,Default,,0000,0000,0000,,And then, we can in the loop, look up the Dialogue: 0,0:08:03.28,0:08:06.54,Default,,0000,0000,0000,,particular friend that is the particular\None we are interested in, Dialogue: 0,0:08:06.54,0:08:10.67,Default,,0000,0000,0000,,using the index operator, friend sub i. Dialogue: 0,0:08:10.67,0:08:12.28,Default,,0000,0000,0000,,And then print Happy New Year. Dialogue: 0,0:08:12.28,0:08:13.66,Default,,0000,0000,0000,,So these two loops, Dialogue: 0,0:08:15.83,0:08:20.34,Default,,0000,0000,0000,,these two loops are equivalent.\NThese, oop, not that one. Dialogue: 0,0:08:20.34,0:08:25.46,Default,,0000,0000,0000,,[SOUND] This loop and this loop.\NThis loop is Dialogue: 0,0:08:25.46,0:08:30.72,Default,,0000,0000,0000,,preferred, unless you happen to need this\Nvalue i, which tells you where you're at. Dialogue: 0,0:08:30.72,0:08:32.49,Default,,0000,0000,0000,,In case maybe you're going to change\Nsomething, you're Dialogue: 0,0:08:32.49,0:08:34.76,Default,,0000,0000,0000,,going to look through something and then\Nchange it. Dialogue: 0,0:08:34.76,0:08:39.07,Default,,0000,0000,0000,,So, but, but, for what I've written here,\Nthey're exactly equivalent. Dialogue: 0,0:08:39.07,0:08:41.07,Default,,0000,0000,0000,,Prefer the simpler one, unless you need Dialogue: 0,0:08:41.07,0:08:44.37,Default,,0000,0000,0000,,the more complex one.\NThey both produce the same kind of output. Dialogue: 0,0:08:46.17,0:08:50.09,Default,,0000,0000,0000,,We can concatenate lists, much like we\Nconcatenate strings, with plus. Dialogue: 0,0:08:53.30,0:08:59.56,Default,,0000,0000,0000,,And you can think of the Python operator's\Nlooking to its right and to its left and Dialogue: 0,0:08:59.56,0:09:02.27,Default,,0000,0000,0000,,saying oh, those are both lists, I know\Nwhat Dialogue: 0,0:09:02.27,0:09:04.56,Default,,0000,0000,0000,,to do with lists, I'm going to put those\Ntogether. Dialogue: 0,0:09:04.56,0:09:08.20,Default,,0000,0000,0000,,And so that produces a two, three-long\Nlists become a six-long Dialogue: 0,0:09:08.20,0:09:12.10,Default,,0000,0000,0000,,list with the first one followed by\Nthe second one concatenated. Dialogue: 0,0:09:12.10,0:09:15.71,Default,,0000,0000,0000,,It didn't hurt the original, a. c is a new\Nlist, basically. Dialogue: 0,0:09:19.04,0:09:22.53,Default,,0000,0000,0000,,We can also slice lists.\NFeels a lot like strings, right? Dialogue: 0,0:09:22.53,0:09:24.03,Default,,0000,0000,0000,,Everything's kind of like strings. Dialogue: 0,0:09:24.03,0:09:28.33,Default,,0000,0000,0000,,For loops like strings, concatenation like\Nstrings, and now slicing like strings. Dialogue: 0,0:09:28.33,0:09:30.02,Default,,0000,0000,0000,,And it is exactly the same. Dialogue: 0,0:09:32.30,0:09:37.81,Default,,0000,0000,0000,,So one up to, but not including.\NJust remember, up to, but not including. Dialogue: 0,0:09:37.81,0:09:41.83,Default,,0000,0000,0000,,the second parameter, is up to but not\Nincluding, so that starts at the sub one, Dialogue: 0,0:09:41.83,0:09:47.95,Default,,0000,0000,0000,,which is the second one up to but not\Nincluding 3, the third one, so. Dialogue: 0,0:09:47.95,0:09:50.91,Default,,0000,0000,0000,,This is 1, 2, and 3 so that's 41 comma 2. Dialogue: 0,0:09:50.91,0:09:55.32,Default,,0000,0000,0000,,Starting at the first one, up to but not\Nincluding the third one. Dialogue: 0,0:09:58.65,0:10:01.57,Default,,0000,0000,0000,,We can similarly eliminate the first one, Dialogue: 0,0:10:01.57,0:10:04.41,Default,,0000,0000,0000,,so that's up to but not including the fourth\None. Dialogue: 0,0:10:04.41,0:10:08.59,Default,,0000,0000,0000,,Starting at zero, one, two, three, but not\Nincluding four. Dialogue: 0,0:10:08.59,0:10:13.65,Default,,0000,0000,0000,,So that's this one.\NIf we go three to the end, and again, Dialogue: 0,0:10:13.65,0:10:21.02,Default,,0000,0000,0000,,remember that there, starting at 0, so 3\Nto the end is 0, 1, 2, 3 to the end. Dialogue: 0,0:10:21.02,0:10:23.54,Default,,0000,0000,0000,,The number 3 doesn't matter.\NSo that's 3, 74, 15. Dialogue: 0,0:10:23.54,0:10:24.29,Default,,0000,0000,0000,,And the Dialogue: 0,0:10:25.71,0:10:29.30,Default,,0000,0000,0000,,whole thing, that's the whole thing, so\Nthese two things are the same. Dialogue: 0,0:10:29.30,0:10:33.10,Default,,0000,0000,0000,,So slicing works like strings, starting\Nand up Dialogue: 0,0:10:33.10,0:10:34.76,Default,,0000,0000,0000,,to but not including is the second\Nparameter. Dialogue: 0,0:10:36.40,0:10:38.57,Default,,0000,0000,0000,,There are some methods, and you can Dialogue: 0,0:10:38.57,0:10:43.02,Default,,0000,0000,0000,,read about these online in the Python\Ndocumentation. Dialogue: 0,0:10:43.02,0:10:44.82,Default,,0000,0000,0000,,We can use the built-in function. Dialogue: 0,0:10:44.82,0:10:48.14,Default,,0000,0000,0000,,It doesn't have a lot of use in sort of how Dialogue: 0,0:10:48.14,0:10:50.59,Default,,0000,0000,0000,,we run, when we're running programs but\Nit's kind of of useful. Dialogue: 0,0:10:50.59,0:10:51.89,Default,,0000,0000,0000,,I like it when I'm typing Dialogue: 0,0:10:51.89,0:10:54.44,Default,,0000,0000,0000,,interactively. Like, what can this thing do? Dialogue: 0,0:10:54.44,0:10:58.12,Default,,0000,0000,0000,,So I make a list, list is a unique type, and Dialogue: 0,0:10:58.12,0:11:00.34,Default,,0000,0000,0000,,I say, with dir I say what can we do with it? Dialogue: 0,0:11:00.34,0:11:04.17,Default,,0000,0000,0000,,Well, we can append, we can count, extend,\Nindex, insert, pop, remove, reverse Dialogue: 0,0:11:04.17,0:11:08.30,Default,,0000,0000,0000,,and sort. And then you can sort of read up\Non all these things. Dialogue: 0,0:11:08.30,0:11:13.89,Default,,0000,0000,0000,,I'll show you just a couple.\NWe can build a list with the append. Dialogue: 0,0:11:14.90,0:11:16.10,Default,,0000,0000,0000,,So this syntax here, Dialogue: 0,0:11:16.10,0:11:19.27,Default,,0000,0000,0000,,stuff equals list, that's called a\Nconstructor Dialogue: 0,0:11:19.27,0:11:21.06,Default,,0000,0000,0000,,which says give me an empty list. Dialogue: 0,0:11:22.44,0:11:26.28,Default,,0000,0000,0000,,You could also say bracket, bracket for an\Nempty list. Dialogue: 0,0:11:26.28,0:11:30.06,Default,,0000,0000,0000,,Whatever, you gotta make an empty list and\Nthen you call the append. Dialogue: 0,0:11:30.06,0:11:33.21,Default,,0000,0000,0000,,Remember that lists are mutable, so it's\Nokay to change it. Dialogue: 0,0:11:33.21,0:11:35.53,Default,,0000,0000,0000,,So we're saying, okay, we started with an\Nempty list. Dialogue: 0,0:11:35.53,0:11:38.21,Default,,0000,0000,0000,,Now append to the end of that, the word\Nbook. Dialogue: 0,0:11:38.21,0:11:39.91,Default,,0000,0000,0000,,And then append to that, 99. Dialogue: 0,0:11:39.91,0:11:44.04,Default,,0000,0000,0000,,Wait a sec. Dialogue: 0,0:11:44.04,0:11:44.86,Default,,0000,0000,0000,,That's a mistake. Dialogue: 0,0:11:49.11,0:11:52.35,Default,,0000,0000,0000,,That's a mistake.\NSo I have to fix this mistake. Dialogue: 0,0:11:52.35,0:11:55.44,Default,,0000,0000,0000,,So watch me fix the mistake.\NPoof. Dialogue: 0,0:11:57.83,0:12:00.68,Default,,0000,0000,0000,,Now my thing is magically fixed.\NIsn't that amazing. Dialogue: 0,0:12:00.68,0:12:03.96,Default,,0000,0000,0000,,I have magic powers when it comes to slide\Nfixing. Dialogue: 0,0:12:03.96,0:12:07.37,Default,,0000,0000,0000,,I just snap my fingers and the slides are\Nfixed. Dialogue: 0,0:12:07.37,0:12:07.90,Default,,0000,0000,0000,,So here we go. Dialogue: 0,0:12:07.90,0:12:10.22,Default,,0000,0000,0000,,We append the 99, and we print it out. Dialogue: 0,0:12:10.22,0:12:13.92,Default,,0000,0000,0000,,And it's got book and 99, emphasizing the\Nfact that they don't Dialogue: 0,0:12:13.92,0:12:16.78,Default,,0000,0000,0000,,have to be the exact same kind of thing in\Na list. Dialogue: 0,0:12:16.78,0:12:20.45,Default,,0000,0000,0000,,Then later we append cookie and then it's\Nbook, 99, cookie. Dialogue: 0,0:12:20.45,0:12:22.91,Default,,0000,0000,0000,,Okay? So this append, we won't do it in line Dialogue: 0,0:12:22.91,0:12:25.73,Default,,0000,0000,0000,,like this so often, we'll tend to do it in\Na loop as we're building up a Dialogue: 0,0:12:25.73,0:12:27.37,Default,,0000,0000,0000,,list, but that's the way you start with Dialogue: 0,0:12:27.37,0:12:30.63,Default,,0000,0000,0000,,an empty list and then [SOUND]\Nprogrammatically grow it. Dialogue: 0,0:12:33.35,0:12:38.41,Default,,0000,0000,0000,,We can ask, much like we do in a string,\Nwe can ask if an item is in a list. Dialogue: 0,0:12:38.41,0:12:41.28,Default,,0000,0000,0000,,So here is a list called some, with these\Nnumbers in it. Dialogue: 0,0:12:41.28,0:12:42.91,Default,,0000,0000,0000,,It's got five numbers in it. Dialogue: 0,0:12:42.91,0:12:45.98,Default,,0000,0000,0000,,Is nine in some? True, yes it is. Dialogue: 0,0:12:45.98,0:12:48.78,Default,,0000,0000,0000,,Is 15 in some? False. Dialogue: 0,0:12:48.78,0:12:55.30,Default,,0000,0000,0000,,Is 20 not in, that's a leg, a legal\Nsyntax, that is legal syntax. Dialogue: 0,0:12:55.30,0:12:58.28,Default,,0000,0000,0000,,Is 20 not in some, yes it's not there,\Nokay? Dialogue: 0,0:12:58.28,0:13:02.91,Default,,0000,0000,0000,,They don't modify the list, don't modify\Nthe list, they're just asking questions. Dialogue: 0,0:13:02.91,0:13:06.26,Default,,0000,0000,0000,,These are logical operations often used in\Nif statements or Dialogue: 0,0:13:06.26,0:13:10.33,Default,,0000,0000,0000,,while, some kind of a logic that you might\Nbe building. Dialogue: 0,0:13:12.05,0:13:14.99,Default,,0000,0000,0000,,Okay, so lists have order. Dialogue: 0,0:13:14.99,0:13:17.13,Default,,0000,0000,0000,,So when we were appending them, the first\Nthing went Dialogue: 0,0:13:17.13,0:13:20.73,Default,,0000,0000,0000,,in first, the second thing went in second,\Net cetera, et cetera. Dialogue: 0,0:13:20.73,0:13:23.38,Default,,0000,0000,0000,,And we can also tell the list to sort\Nitself. Dialogue: 0,0:13:23.38,0:13:25.65,Default,,0000,0000,0000,,So one of the things that we can do with a\Nlist, Dialogue: 0,0:13:25.65,0:13:28.78,Default,,0000,0000,0000,,now we're starting to see some power here,\Nis say, sort yourself. Dialogue: 0,0:13:28.78,0:13:30.19,Default,,0000,0000,0000,,This is a list of strings. Dialogue: 0,0:13:30.19,0:13:33.10,Default,,0000,0000,0000,,It can sort numbers, it can sort lots of\Nthings. Dialogue: 0,0:13:33.10,0:13:38.55,Default,,0000,0000,0000,,friends.sort, that says hey there, dear\Nfriends, sort yourself. Dialogue: 0,0:13:38.55,0:13:40.08,Default,,0000,0000,0000,,This makes a change. Dialogue: 0,0:13:42.54,0:13:44.67,Default,,0000,0000,0000,,It alters the list, and puts it, in Dialogue: 0,0:13:44.67,0:13:48.01,Default,,0000,0000,0000,,this case, in alphabetical order, Glenn,\NJoseph, and Sally. Dialogue: 0,0:13:48.01,0:13:51.78,Default,,0000,0000,0000,,It is muted, it was, it's, it's been\Nmodified, and so Dialogue: 0,0:13:51.78,0:13:54.66,Default,,0000,0000,0000,,friend sub one is now Joseph because\Nthat's the second one. Dialogue: 0,0:13:54.66,0:13:55.85,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:13:55.85,0:14:00.00,Default,,0000,0000,0000,,So the sort method says sort yourself now, Dialogue: 0,0:14:00.00,0:14:03.68,Default,,0000,0000,0000,,sort yourself, and it sorts and then\Nit stays sorted. Dialogue: 0,0:14:06.72,0:14:10.59,Default,,0000,0000,0000,,So [COUGH] Dialogue: 0,0:14:10.59,0:14:13.26,Default,,0000,0000,0000,,you're going to be kind of ticked about\Nthis particular slide. Dialogue: 0,0:14:13.26,0:14:16.79,Default,,0000,0000,0000,,Because there's a whole bunch of built-in\Nfunctions that help with lists. Dialogue: 0,0:14:16.79,0:14:22.26,Default,,0000,0000,0000,,And, there's max, there's min, there's\Nlen, various things. Dialogue: 0,0:14:22.26,0:14:24.52,Default,,0000,0000,0000,,And so we could, all those loops that I\Ntold you how to Dialogue: 0,0:14:24.52,0:14:29.65,Default,,0000,0000,0000,,do, I was just showing you that stuff\Nbecause I thought it was important. Dialogue: 0,0:14:29.65,0:14:31.85,Default,,0000,0000,0000,,This the simplest way to go through and Dialogue: 0,0:14:31.85,0:14:35.23,Default,,0000,0000,0000,,find the largest, smallest, and sum,\Net cetera. Dialogue: 0,0:14:35.23,0:14:36.86,Default,,0000,0000,0000,,So here's a list of numbers. Dialogue: 0,0:14:38.15,0:14:39.56,Default,,0000,0000,0000,,We can say how many are there. Dialogue: 0,0:14:39.56,0:14:43.06,Default,,0000,0000,0000,,That's the count.\NWe can say what's the largest, it's 74. Dialogue: 0,0:14:43.06,0:14:45.96,Default,,0000,0000,0000,,What's the smallest, that'd be 3. Dialogue: 0,0:14:45.96,0:14:49.08,Default,,0000,0000,0000,,What is the sum of the running total of\Nthem all? 154. Dialogue: 0,0:14:49.08,0:14:52.31,Default,,0000,0000,0000,,If you remember from a few lectures\Nago, these are the same numbers. Dialogue: 0,0:14:52.31,0:14:56.88,Default,,0000,0000,0000,,And what is the average, which is, sum of\Nthem over the length of them, Dialogue: 0,0:14:56.88,0:14:58.12,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:14:58.12,0:15:00.96,Default,,0000,0000,0000,,So this makes a lot more sense and if you\Nhad a list of numbers Dialogue: 0,0:15:00.96,0:15:04.51,Default,,0000,0000,0000,,like this, you would simply say what's the\Nmax, you wouldn't write a max loop. Dialogue: 0,0:15:04.51,0:15:06.94,Default,,0000,0000,0000,,I just did that to kind of demonstrate how\Nloops work. Dialogue: 0,0:15:06.94,0:15:09.59,Default,,0000,0000,0000,,[COUGH] Demonstrate how loops work. Dialogue: 0,0:15:09.59,0:15:12.36,Default,,0000,0000,0000,,So here is a way that you can sort Dialogue: 0,0:15:12.36,0:15:16.58,Default,,0000,0000,0000,,of change those kind of programs that we\Nwrote. Dialogue: 0,0:15:16.58,0:15:19.78,Default,,0000,0000,0000,,So there's two ways to write a summing\Nprogram. Dialogue: 0,0:15:19.78,0:15:22.10,Default,,0000,0000,0000,,Let's just say instead of the data being Dialogue: 0,0:15:22.10,0:15:26.37,Default,,0000,0000,0000,,in a list, we're going to write a while\Nloop that's going to read a Dialogue: 0,0:15:26.37,0:15:31.25,Default,,0000,0000,0000,,set of numbers until we say done, and then\Ncompute the average of those numbers. Dialogue: 0,0:15:31.25,0:15:32.73,Default,,0000,0000,0000,,Okay, so let's say this is our problem. Dialogue: 0,0:15:32.73,0:15:38.22,Default,,0000,0000,0000,,Read a list of numbers, wait till the word\Ndone comes in, and then average them. Dialogue: 0,0:15:38.22,0:15:40.45,Default,,0000,0000,0000,,So here's a little program that does that. Dialogue: 0,0:15:40.45,0:15:43.25,Default,,0000,0000,0000,,We create total equals zero, count equals\Nzero. Dialogue: 0,0:15:43.25,0:15:46.12,Default,,0000,0000,0000,,Make a infinite loop with while True. Dialogue: 0,0:15:46.12,0:15:47.52,Default,,0000,0000,0000,,And then we ask Dialogue: 0,0:15:47.52,0:15:48.81,Default,,0000,0000,0000,,to enter a number. Dialogue: 0,0:15:48.81,0:15:51.75,Default,,0000,0000,0000,,We get a string back from this, remember\Nraw_input always Dialogue: 0,0:15:51.75,0:15:56.79,Default,,0000,0000,0000,,gives us strings back, and then if it's\Ndone, we're going to break. Dialogue: 0,0:15:56.79,0:15:59.77,Default,,0000,0000,0000,,This is the version of the if that does\Nnot require an indent. Dialogue: 0,0:15:59.77,0:16:01.57,Default,,0000,0000,0000,,We just put the break up there. Dialogue: 0,0:16:01.57,0:16:04.08,Default,,0000,0000,0000,,And so that gets us out of the loop when\Nthe time is right. Dialogue: 0,0:16:04.08,0:16:06.02,Default,,0000,0000,0000,,So when the time is right over here. Dialogue: 0,0:16:06.02,0:16:09.81,Default,,0000,0000,0000,,And then, we convert the value to float. Dialogue: 0,0:16:09.81,0:16:12.83,Default,,0000,0000,0000,,We use a float to convert the input to a\Nfloating point number. Dialogue: 0,0:16:12.83,0:16:15.13,Default,,0000,0000,0000,,And then we do our accumulation pattern, Dialogue: 0,0:16:15.13,0:16:18.11,Default,,0000,0000,0000,,total equals total plus value, count equals\Ncount plus one. Dialogue: 0,0:16:18.11,0:16:19.07,Default,,0000,0000,0000,,So this is going to run. Dialogue: 0,0:16:19.07,0:16:21.23,Default,,0000,0000,0000,,These numbers are going to go up and up\Nand up and up. Dialogue: 0,0:16:21.23,0:16:22.88,Default,,0000,0000,0000,,And then we're going to break out of it, Dialogue: 0,0:16:22.88,0:16:25.98,Default,,0000,0000,0000,,calculate the average, and then print the\Naverage. Dialogue: 0,0:16:25.98,0:16:29.85,Default,,0000,0000,0000,,Because that's a floating point number, so now\Nthe average is a floating point number. Dialogue: 0,0:16:29.85,0:16:31.07,Default,,0000,0000,0000,,So that's one way to do it. Dialogue: 0,0:16:31.07,0:16:31.39,Default,,0000,0000,0000,,Right? Dialogue: 0,0:16:31.39,0:16:34.57,Default,,0000,0000,0000,,That would be one way to write a program Dialogue: 0,0:16:34.57,0:16:37.99,Default,,0000,0000,0000,,that does an average, is keep a running\Naverage Dialogue: 0,0:16:37.99,0:16:38.100,Default,,0000,0000,0000,,as you're reading the numbers. Dialogue: 0,0:16:40.06,0:16:44.08,Default,,0000,0000,0000,,But there's another way to do it, that\Nwould exact, work exactly Dialogue: 0,0:16:44.08,0:16:47.51,Default,,0000,0000,0000,,the same way, and this is when you can\Nstart using lists. Dialogue: 0,0:16:47.51,0:16:51.56,Default,,0000,0000,0000,,So you come in, you say I'm going to\Nmake a list Dialogue: 0,0:16:51.56,0:16:56.81,Default,,0000,0000,0000,,of numbers, just a mnemonic name, numlist,\Nis an empty list. Dialogue: 0,0:16:56.81,0:17:02.07,Default,,0000,0000,0000,,Then I create another infinite loop\Nthat's going to read for enter a number. Dialogue: 0,0:17:02.07,0:17:03.46,Default,,0000,0000,0000,,And if it's done, break. Dialogue: 0,0:17:03.46,0:17:08.65,Default,,0000,0000,0000,,That gets us out of it.\NConvert the value to an int. Dialogue: 0,0:17:08.65,0:17:12.40,Default,,0000,0000,0000,,Convert the value to a float,\Nthe input value to a float. Dialogue: 0,0:17:12.40,0:17:14.44,Default,,0000,0000,0000,,And then append it to the list. Dialogue: 0,0:17:14.44,0:17:16.58,Default,,0000,0000,0000,,So now the list is going to grow, each\Ntime Dialogue: 0,0:17:16.58,0:17:18.82,Default,,0000,0000,0000,,we read a number the list is going to\Ngrow. Dialogue: 0,0:17:18.82,0:17:21.42,Default,,0000,0000,0000,,However many times we add the number is Dialogue: 0,0:17:21.42,0:17:23.41,Default,,0000,0000,0000,,how many things are going to be in the\Nlist. Dialogue: 0,0:17:23.41,0:17:25.73,Default,,0000,0000,0000,,So in this case, when we're at this point\Nand we Dialogue: 0,0:17:25.73,0:17:28.54,Default,,0000,0000,0000,,type done, there will be three numbers in\Nthe list, because we Dialogue: 0,0:17:28.54,0:17:32.56,Default,,0000,0000,0000,,will have run append three times.\NWe'll have appended 3, 9, and 5. Dialogue: 0,0:17:32.56,0:17:37.16,Default,,0000,0000,0000,,We'll have them sitting in a list.\NAnd we will have exited the loop. Dialogue: 0,0:17:37.16,0:17:39.36,Default,,0000,0000,0000,,So now you say, oh add up all the numbers\Nin Dialogue: 0,0:17:39.36,0:17:42.72,Default,,0000,0000,0000,,that list, and then divide it by the\Nlength of the list. Dialogue: 0,0:17:42.72,0:17:43.96,Default,,0000,0000,0000,,And print the average. Dialogue: 0,0:17:43.96,0:17:47.29,Default,,0000,0000,0000,,So these two programs are basically\Nequivalent. Dialogue: 0,0:17:47.29,0:17:48.62,Default,,0000,0000,0000,,The only time that they might not be Dialogue: 0,0:17:48.62,0:17:54.12,Default,,0000,0000,0000,,equivalent was if there was ten million\Nnumbers. Dialogue: 0,0:17:54.12,0:17:59.26,Default,,0000,0000,0000,,This would use up 40 megabytes of your\Nmemory, which Dialogue: 0,0:17:59.26,0:18:01.23,Default,,0000,0000,0000,,is actually not a lot of memory on some\Ncomputers. Dialogue: 0,0:18:01.23,0:18:05.18,Default,,0000,0000,0000,,But if memory mattered, this does store\Nall those numbers. Dialogue: 0,0:18:05.18,0:18:07.68,Default,,0000,0000,0000,,This one actually just runs the\Ncalculation. Dialogue: 0,0:18:07.68,0:18:11.66,Default,,0000,0000,0000,,So if there's a really large number of\Nnumbers, this would make a difference, Dialogue: 0,0:18:11.66,0:18:15.66,Default,,0000,0000,0000,,because the list is growing and keeping\Nthem all, summing them all at the end. Dialogue: 0,0:18:15.66,0:18:17.35,Default,,0000,0000,0000,,This is actually storing very little data. Dialogue: 0,0:18:18.43,0:18:20.60,Default,,0000,0000,0000,,But for reasonably sized numbers, Dialogue: 0,0:18:20.60,0:18:24.12,Default,,0000,0000,0000,,like thousands or even hundreds of thousands\Nof numbers, these Dialogue: 0,0:18:24.12,0:18:28.96,Default,,0000,0000,0000,,two approaches are kind of equivalent.\NAnd then sometimes you actually Dialogue: 0,0:18:28.96,0:18:32.07,Default,,0000,0000,0000,,want to accumulate something a little more\Ncomplex than this, you want to Dialogue: 0,0:18:32.07,0:18:35.32,Default,,0000,0000,0000,,sort them or look for the maximum and look\Nfor something else. Dialogue: 0,0:18:35.32,0:18:37.43,Default,,0000,0000,0000,,Who knows what, but the notion of make a Dialogue: 0,0:18:37.43,0:18:39.83,Default,,0000,0000,0000,,list and then append something to the list Dialogue: 0,0:18:39.83,0:18:42.38,Default,,0000,0000,0000,,each time through the iteration, and then do\Nsomething with Dialogue: 0,0:18:42.38,0:18:45.41,Default,,0000,0000,0000,,the list at the end is a rather powerful\Npattern. Dialogue: 0,0:18:45.41,0:18:48.72,Default,,0000,0000,0000,,So this is also a powerful pattern,\Nthis is accumulator Dialogue: 0,0:18:48.72,0:18:51.90,Default,,0000,0000,0000,,pattern where we just have the variables\Naccumulating in the loop. Dialogue: 0,0:18:51.90,0:18:55.04,Default,,0000,0000,0000,,This one is one where we accumulate the\Ndata in Dialogue: 0,0:18:55.04,0:18:58.17,Default,,0000,0000,0000,,the loop and then do the computations all\Nat the end. Dialogue: 0,0:18:58.17,0:19:02.05,Default,,0000,0000,0000,,The, certain situations will make use of\Nthese different techniques. Dialogue: 0,0:19:03.13,0:19:09.02,Default,,0000,0000,0000,,Okay.\NSo, connecting strings and lists. Dialogue: 0,0:19:09.02,0:19:11.83,Default,,0000,0000,0000,,So there's a method, a capability Dialogue: 0,0:19:11.83,0:19:16.19,Default,,0000,0000,0000,,of strings that is really powerful when it\Ncomes to tearing data apart. Dialogue: 0,0:19:18.88,0:19:23.11,Default,,0000,0000,0000,,It's called the split.\NSo here is a string Dialogue: 0,0:19:23.11,0:19:26.86,Default,,0000,0000,0000,,with three words and it has blanks in between\Nhere. Dialogue: 0,0:19:26.86,0:19:33.72,Default,,0000,0000,0000,,And abc.split says parse this string, Dialogue: 0,0:19:33.72,0:19:38.69,Default,,0000,0000,0000,,look for the blanks, break the string into\Npieces, and give me back a Dialogue: 0,0:19:38.69,0:19:43.92,Default,,0000,0000,0000,,list with one item for each of the words\Nin the list as Dialogue: 0,0:19:43.92,0:19:47.20,Default,,0000,0000,0000,,defined by the spaces. Okay? Dialogue: 0,0:19:47.20,0:19:53.15,Default,,0000,0000,0000,,So, it takes, breaks it into three pieces\Nand gives us that back in a list. Dialogue: 0,0:19:53.15,0:19:55.87,Default,,0000,0000,0000,,This is very powerful. Okay? Dialogue: 0,0:19:55.87,0:19:58.34,Default,,0000,0000,0000,,So we're going to split it and we get back\Na list. Dialogue: 0,0:19:58.34,0:20:04.18,Default,,0000,0000,0000,,There are three words, and the first word,\Nstuff sub zero, is With. Dialogue: 0,0:20:04.18,0:20:06.20,Default,,0000,0000,0000,,So there's a lot of parsing going on here. Dialogue: 0,0:20:06.20,0:20:09.18,Default,,0000,0000,0000,,We could do this with for loops and a lot\Nof other things. Dialogue: 0,0:20:09.18,0:20:11.24,Default,,0000,0000,0000,,There would be a lot of work in this\Nsplit. Dialogue: 0,0:20:11.24,0:20:14.18,Default,,0000,0000,0000,,Given that this is a really common task,\Nit's really Dialogue: 0,0:20:14.18,0:20:17.97,Default,,0000,0000,0000,,great that this has been put into Python\Nfor us. Dialogue: 0,0:20:17.97,0:20:19.35,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:20:19.35,0:20:22.85,Default,,0000,0000,0000,,So split breaks a string into parts and\Nproduces a list of strings. Dialogue: 0,0:20:22.85,0:20:25.63,Default,,0000,0000,0000,,We think of these as words, we can access a Dialogue: 0,0:20:25.63,0:20:28.04,Default,,0000,0000,0000,,particular word or we can loop through all\Nthe words. Dialogue: 0,0:20:28.04,0:20:31.05,Default,,0000,0000,0000,,So here we have stuff again and now we\Nhave a, a for loop Dialogue: 0,0:20:32.05,0:20:35.07,Default,,0000,0000,0000,,for each of the, that's going to go\Nthrough each of the three words. Dialogue: 0,0:20:35.07,0:20:36.37,Default,,0000,0000,0000,,And then it's going to run three times. Dialogue: 0,0:20:36.37,0:20:37.41,Default,,0000,0000,0000,,Now chances are good we're going to do Dialogue: 0,0:20:37.41,0:20:39.60,Default,,0000,0000,0000,,something different other than just print\Nthem out. Dialogue: 0,0:20:39.60,0:20:44.45,Default,,0000,0000,0000,,But you see how that you quickly can take\Na split followed by a for, and then write Dialogue: 0,0:20:44.45,0:20:45.72,Default,,0000,0000,0000,,a loop that's going to go through each of\Nthe Dialogue: 0,0:20:45.72,0:20:48.36,Default,,0000,0000,0000,,words, without working too hard to find\Nthe spaces. Dialogue: 0,0:20:48.36,0:20:52.57,Default,,0000,0000,0000,,You let Python do all the hard work of\Nfinding the spaces. Dialogue: 0,0:20:52.57,0:20:53.38,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:20:53.38,0:20:56.35,Default,,0000,0000,0000,,So let's take a look at a couple of\Nsamples. Dialogue: 0,0:20:58.13,0:21:00.48,Default,,0000,0000,0000,,Just a couple of things to teach you a\Nlittle more about split. Dialogue: 0,0:21:01.51,0:21:05.57,Default,,0000,0000,0000,,Split looks at many spaces as equal to one\Nspace. Dialogue: 0,0:21:07.50,0:21:10.81,Default,,0000,0000,0000,,So, if you split a lot blank, blank, blank\Nof spaces, it's Dialogue: 0,0:21:10.81,0:21:14.48,Default,,0000,0000,0000,,still just throws away all the spaces and\Ngives us four words. Dialogue: 0,0:21:15.75,0:21:20.48,Default,,0000,0000,0000,,One, two, three, four and throws away\Nall the spaces, Dialogue: 0,0:21:20.48,0:21:21.90,Default,,0000,0000,0000,,because it assumes that's what we\Nwant done. Dialogue: 0,0:21:21.90,0:21:22.54,Default,,0000,0000,0000,,So that's nice. Dialogue: 0,0:21:22.54,0:21:26.92,Default,,0000,0000,0000,,You can also have split, you can also have\Nsplit, Dialogue: 0,0:21:26.92,0:21:30.31,Default,,0000,0000,0000,,split on some other character. Sometimes\Nyou'll be getting data Dialogue: 0,0:21:30.31,0:21:33.09,Default,,0000,0000,0000,,and they'll have used a semicolon, or a\Ncomma, or Dialogue: 0,0:21:33.09,0:21:36.00,Default,,0000,0000,0000,,a colon, or a tab character, who knows\Nwhat they've Dialogue: 0,0:21:36.00,0:21:39.40,Default,,0000,0000,0000,,used, and your job is to dig that data\Nout. Dialogue: 0,0:21:39.40,0:21:42.90,Default,,0000,0000,0000,,So you can split, based on the different\Ncharacter. Dialogue: 0,0:21:42.90,0:21:47.07,Default,,0000,0000,0000,,Here, if we're splitting normally with,\Nwith this is a normal split. Dialogue: 0,0:21:47.07,0:21:49.80,Default,,0000,0000,0000,,It's not going to see the semicolons, it's\Nlooking for a space. Dialogue: 0,0:21:49.80,0:21:52.88,Default,,0000,0000,0000,,And so all we get back is one Dialogue: 0,0:21:52.88,0:21:55.22,Default,,0000,0000,0000,,item in the string, with the semicolons. Dialogue: 0,0:21:55.22,0:21:58.52,Default,,0000,0000,0000,,But, if we switch, and we pass semicolon Dialogue: 0,0:21:58.52,0:22:01.08,Default,,0000,0000,0000,,as a parameter, in as as parameter to\Nsplit, Dialogue: 0,0:22:01.08,0:22:03.09,Default,,0000,0000,0000,,then it will know to split it based on Dialogue: 0,0:22:03.09,0:22:06.45,Default,,0000,0000,0000,,semicolons, and gives us first, second, and\Nthird back. Dialogue: 0,0:22:07.52,0:22:07.82,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:22:07.82,0:22:09.94,Default,,0000,0000,0000,,And then it gives us three words. Dialogue: 0,0:22:09.94,0:22:13.64,Default,,0000,0000,0000,,So you can split either on spaces, or you Dialogue: 0,0:22:13.64,0:22:17.49,Default,,0000,0000,0000,,can split on a character other than a\Nspace. Dialogue: 0,0:22:17.49,0:22:18.04,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:22:18.04,0:22:20.40,Default,,0000,0000,0000,,[COUGH] Dialogue: 0,0:22:20.40,0:22:25.23,Default,,0000,0000,0000,,So, let's take a look at how we might turn\Nthis into some of our common assignments Dialogue: 0,0:22:25.23,0:22:32.42,Default,,0000,0000,0000,,that we have in this chapter, where we're\Ngoing to read some of the mailbox data. Okay? Dialogue: 0,0:22:33.42,0:22:36.72,Default,,0000,0000,0000,,So, here we go with a little program. Dialogue: 0,0:22:36.72,0:22:41.17,Default,,0000,0000,0000,,First three lines, we write these a lot.\NOpen the file. Dialogue: 0,0:22:41.17,0:22:43.09,Default,,0000,0000,0000,,Write a for loop to loop through each Dialogue: 0,0:22:43.09,0:22:44.87,Default,,0000,0000,0000,,line in the file. Dialogue: 0,0:22:44.87,0:22:48.10,Default,,0000,0000,0000,,Then we're going to strip off the white\Nspace at the end of the line. Dialogue: 0,0:22:48.10,0:22:50.99,Default,,0000,0000,0000,,One, two, three.\NDo those all the time. Dialogue: 0,0:22:50.99,0:22:54.99,Default,,0000,0000,0000,,And we're looking for lines, if you look\Nat the whole file, Dialogue: 0,0:22:54.99,0:22:58.17,Default,,0000,0000,0000,,we're looking for lines that start with\Nfrom, followed by a space. Dialogue: 0,0:22:58.17,0:23:00.42,Default,,0000,0000,0000,,So if the line does not start with from Dialogue: 0,0:23:00.42,0:23:03.70,Default,,0000,0000,0000,,followed by a space, that's a space right\Nthere, continue. Dialogue: 0,0:23:03.70,0:23:08.46,Default,,0000,0000,0000,,So that's a way to skip all the lines that\Ndon't look like this. Dialogue: 0,0:23:08.46,0:23:12.49,Default,,0000,0000,0000,,There're thousands of lines in this file\Nand just a few that look like this. Okay? Dialogue: 0,0:23:12.49,0:23:17.11,Default,,0000,0000,0000,,So we're going to look and we're\Ngoing to try Dialogue: 0,0:23:17.11,0:23:22.79,Default,,0000,0000,0000,,to find what day of the week this thing\Nhappened on. Dialogue: 0,0:23:22.79,0:23:27.70,Default,,0000,0000,0000,,So, so we're throwing away all the lines\Nwith this little bit of code. Dialogue: 0,0:23:27.70,0:23:32.82,Default,,0000,0000,0000,,Then what we do is we take the line, which\Nis all of this text, and then we split it. Dialogue: 0,0:23:34.11,0:23:38.27,Default,,0000,0000,0000,,And we know that the day of the week is\Nwords sub two. Dialogue: 0,0:23:38.27,0:23:43.08,Default,,0000,0000,0000,,So this is words sub zero, this is words sub\None, and this is words sub two. Dialogue: 0,0:23:43.08,0:23:46.48,Default,,0000,0000,0000,,So this is words sub zero, sub one, and sub\Ntwo. Dialogue: 0,0:23:46.48,0:23:48.55,Default,,0000,0000,0000,,And so, all we have to do is print out the\Nsub two Dialogue: 0,0:23:48.55,0:23:53.74,Default,,0000,0000,0000,,and we get, we throw away all the lines\Nexcept the from lines. Dialogue: 0,0:23:53.74,0:23:56.65,Default,,0000,0000,0000,,We split them and take the sec, uh, the, Dialogue: 0,0:23:56.65,0:23:59.33,Default,,0000,0000,0000,,the third word or words sub two and we Dialogue: 0,0:23:59.33,0:24:02.26,Default,,0000,0000,0000,,can quickly quickly create something\Nthat's extracting Dialogue: 0,0:24:02.26,0:24:04.06,Default,,0000,0000,0000,,the day of the week out of these. Dialogue: 0,0:24:06.03,0:24:07.40,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:24:07.40,0:24:11.89,Default,,0000,0000,0000,,So it's, it's, I mean, it's quick, because\Nsplit does the tricky work. Dialogue: 0,0:24:11.89,0:24:15.14,Default,,0000,0000,0000,,If you go back to the strings chapter, you\Nsaw that Dialogue: 0,0:24:15.14,0:24:16.91,Default,,0000,0000,0000,,we did a lot of work to get this to\Nhappen. Dialogue: 0,0:24:17.95,0:24:21.04,Default,,0000,0000,0000,,So here's even another tricky pattern. Dialogue: 0,0:24:21.04,0:24:26.51,Default,,0000,0000,0000,,So let's say we want to do what we did at\Nthe end of Chapter Six, Dialogue: 0,0:24:26.51,0:24:28.12,Default,,0000,0000,0000,,the string chapter. Dialogue: 0,0:24:28.12,0:24:30.87,Default,,0000,0000,0000,,Let's say we wanted to get back this little\Nbit of data. Dialogue: 0,0:24:32.13,0:24:33.33,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:24:33.33,0:24:37.31,Default,,0000,0000,0000,,So, can look at this and say, okay, let's\Nsplit this. Dialogue: 0,0:24:37.31,0:24:42.42,Default,,0000,0000,0000,,And this will be zero, one, and two, and\Nthree, and four, and five, and six. Dialogue: 0,0:24:42.42,0:24:44.53,Default,,0000,0000,0000,,We're splitting it based on spaces. Dialogue: 0,0:24:44.53,0:24:50.11,Default,,0000,0000,0000,,Then the email address is words sub one,\Nright? Dialogue: 0,0:24:51.11,0:24:54.67,Default,,0000,0000,0000,,So that email address is this little bit\Nof stuff Dialogue: 0,0:24:54.67,0:24:58.78,Default,,0000,0000,0000,,because it's in between spaces, right?\NSo that's what we pull out. Dialogue: 0,0:24:58.78,0:25:02.36,Default,,0000,0000,0000,,The email address is words sub one. Dialogue: 0,0:25:02.36,0:25:04.51,Default,,0000,0000,0000,,We've got that. Dialogue: 0,0:25:04.51,0:25:07.73,Default,,0000,0000,0000,,So that's sitting in this email address\Nvariable. Dialogue: 0,0:25:07.73,0:25:10.00,Default,,0000,0000,0000,,Then we really, all we want, we don't\Nreally want the whole thing, Dialogue: 0,0:25:10.00,0:25:11.96,Default,,0000,0000,0000,,we just want the part after the Dialogue: 0,0:25:11.96,0:25:14.47,Default,,0000,0000,0000,,at sign, and we can do a lookup for the, oop. Dialogue: 0,0:25:14.47,0:25:16.29,Default,,0000,0000,0000,,We can do a lookup of the at sign. Dialogue: 0,0:25:17.49,0:25:22.14,Default,,0000,0000,0000,,But you can also then do a second, come\Nback, come back. Dialogue: 0,0:25:22.14,0:25:25.30,Default,,0000,0000,0000,,[SOUND] There we come. Dialogue: 0,0:25:25.30,0:25:29.11,Default,,0000,0000,0000,,You can also do a second split.\NOkay? Dialogue: 0,0:25:29.11,0:25:31.26,Default,,0000,0000,0000,,So we're taking this variable here, email, Dialogue: 0,0:25:31.26,0:25:33.98,Default,,0000,0000,0000,,which is merely this little part right\Nhere. Dialogue: 0,0:25:33.98,0:25:36.84,Default,,0000,0000,0000,,And we are splitting it again, except this Dialogue: 0,0:25:36.84,0:25:38.40,Default,,0000,0000,0000,,time we're splitting it based on a at\Nsign. Dialogue: 0,0:25:38.40,0:25:42.64,Default,,0000,0000,0000,,Which means it's going to bust it right\Nhere, and find Dialogue: 0,0:25:42.64,0:25:44.14,Default,,0000,0000,0000,,us two pieces. Dialogue: 0,0:25:44.14,0:25:49.73,Default,,0000,0000,0000,,So pieces now is a list where the sub zero\Nitem is the Dialogue: 0,0:25:49.73,0:25:56.28,Default,,0000,0000,0000,,person's name and sub one item is the host\Nthat their mail address is held from. Dialogue: 0,0:25:56.28,0:26:00.54,Default,,0000,0000,0000,,Okay?\NAnd so then all we need to know is pieces Dialogue: 0,0:26:00.54,0:26:06.38,Default,,0000,0000,0000,,is sub one, and pieces sub one is this\Nguy right here. Dialogue: 0,0:26:07.90,0:26:10.75,Default,,0000,0000,0000,,So that's pieces sub one, and so we\Npulled it out. Dialogue: 0,0:26:10.75,0:26:13.47,Default,,0000,0000,0000,,So if you go back to how we did it before,\Nwe were Dialogue: 0,0:26:13.47,0:26:17.10,Default,,0000,0000,0000,,doing searching, we were searching some\Nmore, and then we were taking slices. Dialogue: 0,0:26:17.10,0:26:19.38,Default,,0000,0000,0000,,This is a little more elegant, okay? Dialogue: 0,0:26:19.38,0:26:21.11,Default,,0000,0000,0000,,Because really, we split it and then we\Nsplit it, Dialogue: 0,0:26:21.11,0:26:23.08,Default,,0000,0000,0000,,and we knew what piece we were looking at. Dialogue: 0,0:26:23.08,0:26:27.25,Default,,0000,0000,0000,,So this is what I call the Double Split\NPattern, where you split a string Dialogue: 0,0:26:27.25,0:26:30.63,Default,,0000,0000,0000,,into a list, then you take a thing out,\Nand then you split it again. Dialogue: 0,0:26:31.71,0:26:33.02,Default,,0000,0000,0000,,Depending on what data you're looking for. Dialogue: 0,0:26:33.02,0:26:35.38,Default,,0000,0000,0000,,This is just a technique, it's not the\Nonly technique. Dialogue: 0,0:26:35.38,0:26:40.48,Default,,0000,0000,0000,,Okay, so that's lists. Dialogue: 0,0:26:40.48,0:26:42.04,Default,,0000,0000,0000,,We talked about the concept of a Dialogue: 0,0:26:42.04,0:26:44.54,Default,,0000,0000,0000,,collection where lists have multiple\Nthings in it. Dialogue: 0,0:26:44.54,0:26:47.35,Default,,0000,0000,0000,,Definite loops, again, we've seen these\Nthings. Dialogue: 0,0:26:47.35,0:26:49.60,Default,,0000,0000,0000,,We're kind of, it looks a lot like strings Dialogue: 0,0:26:49.60,0:26:53.10,Default,,0000,0000,0000,,except the elements are more powerful and\Nthey're more mutable. Dialogue: 0,0:26:53.10,0:26:59.07,Default,,0000,0000,0000,,We still use the bracket operator and we\Nredid the max, min, and sum. Dialogue: 0,0:26:59.07,0:27:02.38,Default,,0000,0000,0000,,Except we did it in, like, one line rather\Nthan a whole loop. Dialogue: 0,0:27:02.38,0:27:06.11,Default,,0000,0000,0000,,And something we're going to play with a\Nlot is using split to parse strings, Dialogue: 0,0:27:06.11,0:27:08.63,Default,,0000,0000,0000,,the single split, and then the double\Nsplit Dialogue: 0,0:27:08.63,0:27:11.13,Default,,0000,0000,0000,,is the natural extension of the single\Nsplit. Dialogue: 0,0:27:11.13,0:27:14.78,Default,,0000,0000,0000,,So, see you in the next lecture, looking\Nforward to talking about dictionaries.