[MUSIC] Welcome, I'm Professor Seth Frey, and we're going to learn about code flow. So up to now, if you wrote some code and ran it, you sort of ran all the code, we've run from top to bottom like a waterfall, but codes flexible. We can control the flow of execution of the code and produce patterns that do increasingly complicated stuff. That's what we're going to learn about today in this lesson. The great way to do that is using the list you've already learned about. We're going to learn how to sort of run or loop through list. We're going to write one piece of code, and we're going to execute it on every item of a list. That's our first introduction to non-linear code flow is these loops. They're also called for loops. You'll learn why. Our goals today are to learn how to loop over every element of the list, learn how to build a list by looping through the same amount of code over and over and sort of building it one by one, adding elements to it. And we're going to learn how to think in code and follow the flow of code, which is going to start to mean sort of being able to run code in our heads. The first application is going to be how to make a list from other lists. So let's say you're given a list of the names of the days of the week, but what you want is a list of the abbreviated days of the week. So let's run this code here. We're going to produce the list days. This should be familiar to you. If we print out days, we'll get just what we expect. Print days, we'll get a list of the days of the week, wonderful. Now we're going to see some new syntax, so this should be partly recognizable. You recognize the square brackets, square brackets mean it's a list, but something funny is happening in here. We no longer have elements separated by commas. We've got some syntax. What's happening here is we're saying d, square brackets colon 3. If d was a string, that would be, I guess the first three elements of that string if you remember your slicing notation. So whatever d is the first three elements of it, for d in days. Now d and days. So what this means is that days we know, days we recognize days is a handle and you can always go to the end of this kind of syntax. The very last thing for your grounding in reality, days we know, days is a list. What this syntax is doing is it's defining a temporary variable called d. And d is going to be our sort of placeholder name or placeholder variable as we loop through the list. So the for syntax for something in something else, for needle, in haystack, for element in list. We're going to run the code up front, this code here on every element of that list. So what's going to happen is first, Sunday is going to be assigned to d, and we'll get the first three letters of Sunday, Sun. When once that codes run, then Monday is going to be assigned to d, and then Tuesday and Wednesday and Thursday and Friday and Saturday until these assigned Saturday, we get the first three elements of Saturday are Sat. And we'll sort of end up with a list that was this sort of the same bit of code right here. Just these five characters run on every element of the list. So if we've run it, that's what we're going to get, is the first three characters of every element of that list. Now, I could have named that placeholder variable, anything I called it d, I could have called it var. This code is going to do exactly the same thing. And suddenly all our variable name rules are kind of coming back to us that we should have informative variable names. So here, day of the week, that's kind of more typing, but that code does exactly the same thing Or day names. So all of these examples, they'll do the exact same thing. I'm just showing you the flexibility of these placeholders, these variable names. At the end of the day, what was important was sort of here, is whatever the placeholder is called, what are we doing to it? For all of these examples, we're extracting the first three characters, and that's how we get our list of abbreviated names of the days of the week. Just to really loop you through manually, in the very first code variable d got assigned the value of Sunday. Sunday got abbreviated to Sun, then we did the same thing for Monday and Tuesday on and on and on. So all of this here is just the sort of verbal explanation of what happened. If that looks repetitive, you're right, that's why we have this very elegant, concise syntax. That's the beauty of code, once you have something repetitive, you're actually in a great spot. because you can just define a loop that runs the same code over and over, rather than you having to type that code out seven times for each day of the week or a million times for each tweet in your giant Twitter data set, or any other type of long list or example that you can come up with. Now, just like any other list or output, we can assign it to a variable. That's what's occurring here. Days is our full day names. Days abbr is our abbreviated day names and we're going to print both out, we'll get the abbreviation. So days abbr is a variable that we assign the list to. This syntax, let's forget a really ugly name, if you ever have to google it, it's called the list comprehension, I think that's an awful name. But regardless, it produces a list that you can assign this stuff just like anything else in Python. It's a cheaper way of building one list from another list, which is the point of this. And everything between the open square bracket and the for is our playground, we can do whatever we want to a list. So there's an upper function. What you do is you take a string, and you put dot upper at the end of it, and you will uppercase that string. So we could uppercase all the days of the week. And at the end of the day, days is still the normal sentence case, it's still the original list, days didn't change. From days we created a new list that has all upper case. Another thing we could do is return the last character every day of the week. That might answer the question, do all days of the week end in y or end in day? Let's run it and find out. You might remember indexing the minus one gives you the last element of a list or a string. Yeah, it looks like they all end in y, and our playground, our space between the square bracket and the for, we can sort of just do nothing. What would happen if we just for every element in days, we just return that element? We would essentially make a copy of the list. So here, we're defining a variable called days copy, which is an exact copy of days. We run it and these two should be identical, which they are. So for another example of iterating through list of creating a new list from an existing list, we can take a group of ages and we can sort of figure out who's over 21. So each of these elements in the list corresponds to a person, there's person one, there's person two, there's person three, there's person four, and maybe what we want is for every position in that list is sort of a true or false. Is that person over 21 or are they not? So if we run this code, this first line, I'm just sort of giving it to you as an example, just to walk you through this code is just copying the ages list into a new variable called over 21. For every element in ages, placeholder called age, we're just going to return that element. This code doesn't really do anything, but we're going to elaborate it by replacing age with this test, this Boolean, if the number age is greater than 21, it will return true, if it's not, it'll return false. And that's what we should get as our final output here. So if we run it, we've got that the first person is not over 21. The next two are, the next bunch are not, all the way to the last person who is. So all those people are not. For every element in the list, we ran this test and we got a bunch of true falses, we get a new list whose entire elements of the return values of this code here, either true or false. And we could have run the exact same code with a different placeholder variable, maybe a more informative one. I may be over 21, true or false. We get the same answer. This list is identical to this list. They have the same values. To keep going on the value the power of iterating through lists to create new list, we can do something maybe kind of applied, let's say you are trying to spam company or spam a bunch of friends. What you have is a bunch of people's names all the way from Marcellinus to Xiaolin Li, and you happen to know that the company they work for always uses the same format of email. It uses the first name.second name @ whatever the company is, or first initial.last name or no dot, just first initial maybe middle initial and then last name @ the company. You know the format, and you want to generate their emails automatically so that you can do a big mail merge or a big spam attack. All right, so we're going to start with this list. What's that going to look like? First, we're going to meet the replace function, replace is handy. It takes two arguments. The first one, right there is the thing you want replaced, and the second right there is the thing you want it replaced with. So if I was to take this string and run this command, what's going to happen is I'm going to take every instance of hyphen and replace it with a space. This code is identical except instead of replacing the hyphen with the space, I'm replacing it with the empty string. So I'm replacing it with nothing. So the outputs we get should be slightly different. They're still going to say this string is lots of hyphens, but one is going to replace all the hyphens with spaces. The other is going to replace all the hyphens with nothing, essentially going to delete all the hyphens without replacing them. Just like you'd expect, we deleted all the hyphens without replacing them here. We replaced them with spaces here. So using replace, we're going to make a little bit of progress towards generating emails of people from this list. I want to approach this one thing at a time. So we're first going to solve this problem by just working on the first element of the list. Once we have it solved for the first element of the list, we'll take the code we developed, and kind of move it over into our for loop into our list comprehension to run that code, to run that name to email conversion code on every element of the list. So this first line, it replaces every instance of space with dot, there's only one instance of space in the name Marcel Marcellinus. So we're going to get Marcel.Marcellinus, I'm guessing. Then assuming that works, we're going to take whatever we got from that. We're also going to lower case everything. You don't have to lower case emails, but sort of a conventions and there's a function called lower that makes it really easy. It takes a string and it returns the exact same string, but with only lower case letters. And assuming that works, we're going to just take our Marcel.Marcellinus and paste or add or append the @ whatever website.com to the end of it. What we should get is the email version of that name for whatever company that person works for, @ example, Inc. And we do, our first code does what we thought, our second line, lower cases everything and our third creates an actual email, that's pretty handy. So, having gotten this basic template right, take a string, take whatever placeholder string, run or replace, lower case everything, and then add example.com to the end of it. We're allowed to do that. We should get a bunch of emails, look at that Sandra Autobahn at example, linda watashi at example and on and on and on. Were able to take a list of names and produce a list a new list of email addresses. But let's go back. This is actually a little bit new. So let's let's dwell here. How how does this make sense? How am I allowed to do this string dot replace then a bunch of stuff, parentheses, then another dot than you know, lower and more parentheses. That that's kind of funny. It's like we just strung together two commands and it worked somehow. It worked. How is that? Well you can think of it this way when we ran if I just run this string, I would have gotten that string back. If I literally printed that string, I would get that string back. So you can almost think of that, the string as its own output. So we run this string and this zone output and we're going to run replace on that on it sort of itself. We're going to run replace on it. We're going to run under place on it and we're going to get a new string which is pretty much the same string except one thing changed and that's going that new string is going to be the return value of all this. Okay so we could assign that to a variable and then run lower on the variable but we're just sort of but it's the same thing this you know, reduces to a string. So how about we just take the lower case of that string of that output. That's how this works. The output of that is going to be another string. That's a lower case. And that string right here is one we're allowed to add more string to and that's how this all works. This is part of you learning the thinking code and getting your familiarity with python, getting your head around on this idea of outputs this idea of chaining commands together. It'll become something you're more familiar with. You'll you'll certainly see it a lot. So I'm happy to dwell on it a little bit. For yet another example we can look into the abbreviations for for the names of the States of the U. S. Here's a really long list of all the names of all the states. It really just goes on and on and on. Did I type all that out now? I googled I googled python list of states and I got that big list and I just sort of copied it into the code. You can do that. So what I want, let's say boy there's a lot of abbreviations, A lot of states, I wish they were simpler. I wish they were all the first two letters of that state name. So let's run it. And what I'm going to get is a bunch of abbreviations. They look kind of ugly, they're not upper case. So we can fix that okay there but now we can see kind of other problems. Now there's two states with the abbreviation A L there's a lot of states with the abbreviation and a whole bunch more. Look at all those states with the abbreviation. Any let me think Nevada and Nebraska and new Mexico. Yeah the news. Right. Are there any other news? Well you get the idea, this isn't the most brilliant compression scheme. This isn't the most brilliant way of getting abbreviations. But it was easy code to write. Let's try something else. Let's try. Some states use their their very first and their very last letter, Let's see if that works for every state. We're going to four state and states, right? So states are placeholder for the name of every state. Here's what we're going to do. We're going to take the very first element of that of that name and we're going to take the very last element of that. We're going to uppercase it and we're going to add them together. What's an example of a state that whose abbreviation is both the first and the last letter I Iowa California kind of because it's got all those a's in it. Hawaii okay so there's a couple examples of this. Let's see how well it generalizes. Let's run it. Boy. Not too much of an improvement that didn't work. On the other hand You know several states got ski thrilled abbreviations and some have the same abbreviation under both schemes. That's that's flexible. So what we have is a bunch of examples of using list comprehension is using this pretty elegant, concise syntax to take one list and create a new list. That's a variation on it. And I'm going to give you lots of opportunity to practice that.