In this lesson, you'll learn about a very important concept in programming the "for loop". This is a special code pattern or a special command that's found in many programming languages, and it lets you tell the computer to repeatedly perform actions in all of the items in a list in this an automated way, without writing code over and over. So, if you have a list with many data items, a for loop lets you tell the computer to do the same thing to item one or item zero. We start from zero, then item one, item two, and so on to all the items in the list. Let's take a look. Let's start by loading some helper functions like we did before. From the previous lesson, we saw this example of a list of tasks. And if I run this, this shows this list printed out down here. And we saw how you can set task equals list of task zero. Then run this and then maybe change this to one. And we run this and it changes a two. And we run this. And this way of getting the LLM to run through all three tasks, it felt very manual and that we needed to type and run code like this three times. Let's take a look at how you could use a for loop to automate this process. A for loop lets you repeat a set of commands for each element in the list. This is what the code to use a for loop looks like. So in plain English, this is saying for each task and a list of tasks. Let's print the LLM response to that task. And this piece of code, which is called a for loop, is equivalent to writing all of these different lines of code. Print LLM response to this task zero. Print LLM response to this task one, and so on. For as many elements or items as there are in the list of tasks. Let's go through the detail of exactly what every single part of this code is doing. "For" here it mixes a for command or a for statement. "Task" is the name you define for the variable that would take on the value of list of task zero. List is task one, list is task two, and so on. So, task will in turn take on the value of the different elements of the list of tasks. "In" is another special Python keyword. And that's the list that we had with the elements we want to go through. And we use a colon to tell Python where is the start of the commands we want it to run on every item in the list, and that's the command that it will repeat on each element of the list. Now, one of the special parts of this code is, there are four spaces over here. In coding we call this an indentation and that just means print LLM response is indented or shifted four spaces to the right. By convention Python programer is used for spaces and so I would recommend you stick to that convention. Let's take a look at this code in action. We've already set list a task to be equal to this up here. And so, I'm going to say for task in list of tasks colon. And then because I wrote a colon when it hit enter Python already knew I wanted to indent these four spaces. So put my cursor there and then let me just say print task. So what this will do is it'll say for each task in this list of three tasks, print out that task. And if I run it. Shift enter. What you're seeing is that the task is being replaced each time by one of the items in the list of tasks. Now, lets do for task in list of task colon. And now let's print LLM and response task and let's run that. And this takes a little bit of time. There's the first task. There's the second task the birthday message. And that's a third task. The review. Just to show you some common errors of for loops. If I didn't have four spaces here, to not indent this and I would to run it, this would have an error. But if you were to put a few spaces there four spaces are the convention, then this would work. And another common mistake in using for loops is to forget the colon here. So if I had to run this, this also generates an error. And if you see an error, well this actually says syntax error expected a colon. But you were to see an error like this and not be quite sure exactly what to do. Well, if you ask your AI chatbot companion to please help me debug this code. And then I''l past the code there, then there's actually pretty good. It says: there's a syntax error, a colon is missing. And then it actually fixes up the code for you and reminds you the missing colon. Sometimes all of us will just miss a colon, in our call. Miss a single character in our code. And many programmers have spent many hours starting at the code to try to figure out what is that one little bug, that one little error in their code. But since AI chatbots became widely available, our ability to copy paste our code into chatbot to have it help us figure out what might be wrong has helped many programmers find bugs and fix them much more efficiently. Here's another example where we're going to use a large language model to automate a writing task, specifically creating ice cream flavor descriptions. All right. Here are some ice cream flavors. And if you want to have descriptions of each of these ice cream flavors, maybe because you're writing marketing copy, a marketing text for a website, this is what you can do. You can say for flavor in ice cream flavors. And then I'm going to set the prompts to be for the ice cream flavor listed below, provide a captivating description to keep use for promotional purposes, and then use a flavor. And lastly, I'm going to tell it to print response for that prompt. So let's run this. Here's the description of vanilla ice cream. Here's chocolate ice cream is actually my favorite strawberry ice cream. And there's mint chocolate chip ice cream. One thing I would point out is that this entire block of code is indented, meaning that all of this code is shifted by four spaces to the right, and Python looks at what code is indented to figure out what is the code you want it to run four times, once for each of these ice cream flavors. So in contrast, if you were to run this, this won't work because this will cause Python to run prompt equals the prompt with vanilla and chocolate and so on. They'll do that four times without ever calling Print LLM response, and only after running this highlighted code four times will try to print out LLM response. So this code won't work. Feel free to trt and see what happens as only by indenting all of this code to do then runs all of this code four times, which is what you want. And feel free to modify this code and play with it to see if you can understand what it's doing. I want to point out that as you run through this block of code, this chunk of code, this block of code four times, every time you run this, flavor takes on a different value, right? Another the chocolate, the strawberry, the mint chocolate chip. And every time you run to this code, this prompt variable is set to a different value. And so runs print LLM response on a different prompt. If you want, one thing you could do is print out the prompts here so you can see the prompt before is passed on to the large language model. I'm not going to run this, but feel free to pause the video and make this change and run this code if you're interested. Now let's look at one last example before we wrap up this video. Which is, we had a list of four ice cream flavors. What if you want to take the descriptions to the large language models generating and save the descript to their own lists? Let me show you how you could do that. I'm going to create a new list. Promotional descriptions equals. And then I'm going to create an empty list. So open square bracket. And then without putting anything on the list, I'm going to close the square bracket right away. So in our little tractor pulling a few carts along, this is like a tractor that's all by itself with no carts pulling behind it. And this is an empty list. Then here's the code we're going to use when I say for flavor in ice cream flavors. And as usual, remember the colon. And then all this code is indented. So you can run all of this code multiple times. We've set the prompt to be this for ice cream flavor. This it write a captivating description. Then we're going to set the description variable to be get LLM response to the prompt. So the description is one of those delicious descriptions of these ice cream flavors. And then we're gonna take promotional descriptions which starts off as an empty list, and append to it the description that we just had. In the examples be seen so far, we launch the initialize the list with all of the items it needed to have, be it this a friend's name, so the list of tasks or the ice cream flavors. But it's actually a pretty common coding pattern in Python. To start off with an empty list and then repeatedly add or append items to the end of that list. Then build up that list. Let's run this code and actually take a few seconds to run, because is now calling the large language model four times to generate these descriptions. And now if we were to print promotional descriptions then this is a list of all the four descriptions. There's a little bit hard to read in this format, but I want to print out, say, the description of the chocolate ice cream, my favorite. Then you can print out this promotion descriptions square bracket one. Because this is the second ice cream flavor on our list. Since you can see the combination of for loops and lists is quite powerful and lets you do a lot in Python. In these examples, we've been using pretty short list with maybe 3 or 4 items, but imagine people that loops through hundreds or thousands of items in a list. That can get your computer to do repetitive tasks for you hundreds or thousands of times. Now, one limitation of this is it can be tricky to access items if you don't know where they are. I happen to remember that chocolate ice cream was item one so I could type one here. But if you had dozens of ice cream flavors and don't remember what exactly was the number of the flavor you like, then it could be harder to figure out how to access that one description that you want. In the next lesson, you learned about a new data type that has some similarities to this. The way finding and working with a specific data item can be much easier. This data type is called a dictionary, and in Python, lists and dictionaries are the two most important ways to store collections of multiple data items. So let's go on to the next video to learn about dictionaries.