For Loops
- 04:04
Learn how to automate repetitive processes in Python using for loops, by iterating through elements in an iterable object and performing a specified action on each.
Downloads
No associated resources to download.
Glossary
Conditional Logic Custom Function For loops PythonTranscript
Anytime you need to repeat a process in Python, you should ask if you can use a for loop to automate that. When you're creating a for loop, you need three ingredients. First of all, an iterable object, like a list like we have right here that contains all of the objects that you want to iterate through. Then when you're writing your for loop, like down here, you need a variable, and that variable represents each element inside the Iterable object here, it represents the letters inside our list. And then finally, you have an indented code block that describes the process that Python needs to perform with each variable in the list. So here our code block is simply to print the letter. So this for loop is saying for each letter here in list Iterable, we're giving it this list, print the letter. So for each of these elements in this Iterable object, we want you to perform this action. When I execute that cell, it's just going to print each of those objects in that list. The variable in your for loop can be named anything that you want as long as it's consistent throughout the for loop. What matters is that you have a name to reference inside your code block to tell Python what to do with each object in the list. So if we wanted to code back, go back and say for thing in list Iterable, print that thing, it's gonna give us the same exact output. What I recommend is using a descriptive variable name just so that when you're going back and reviewing your code or when somebody else is reviewing your code, it's obvious to them what you're doing. Let's return to this example where we have our future value function that receives a metrics list as an argument, and then these five lists of metrics. Right now we can type the name of each of those lists into our future value function and it works fine, but we need to type in each one individually. To create a for loop, I first need to pass all of these metrics lists into a list that contains all of them, and that will give Python something to iterate through when we create our for loop. So I'm gonna call it all metrics equals, and then I'm just gonna type the names of all these. Here I have a list of lists, all metrics containing each of my five metrics lists with our investment details. And while we're here, I wanna show you one quick thing about how Python works with spaces. So I could put all of this on one line and there's nothing wrong with that at all, especially since there are only five objects in this list and it's really easy to understand. Anyways, if I wanted to though, I could break each of these objects into a different line, and you'll notice that when I hit enter here, it's automatically indented. So I'm gonna do that for each of these objects. Python will automatically indent them, and then when I get to the end, I'm gonna hit enter to put this square bracket on the next line and Python will automatically unindent that. So sometimes I like to do this just to make my code a little bit cleaner, especially when I'm doing something like a dictionary. You'll notice like in the last election, in the last lesson, I broke my dictionary into different lines. You don't actually need to do that. It's just formatting and good etiquette and makes your code easier to read and interpret for your future self and for other people. But anyways, now we have our all metrics list of lists. So I'm gonna execute that to define that. And now I can create a for loop that iterates through all metrics and applies our custom future value function to each of the investment metrics lists that we stored in all metrics. I'm gonna start with for metrics list. That's my variable. In all metrics. That's my iterable.
And now I have to tell it what action to perform on each of the objects inside all metrics. I want it to print the future value of that metrics list and Python iterates through each of the lists in all metrics and applies our future value function to give us the answer.