From the course: Programming Foundations: Beyond the Fundamentals
Iterating through collections - Python Tutorial
From the course: Programming Foundations: Beyond the Fundamentals
Iterating through collections
- To make an omelet, as the saying goes, you have to start by breaking a few eggs. If I want to make a three egg omelet, I need to crack three eggs, one at a time into my mixing bowl. So I grab the first egg, I crack it and I drop the white and the yolk in the bowl and I put the shell aside for my compost pile. Then I repeat with a second egg, and finally with the third. And now that I'm through with all the eggs I can move on to the next steps in my process. Seasoning, beating, and cooking everything up. We use iteration in programming all the time. In Python, you can create a loop using the for keyword. In the start file for this video, I have a list called spices that stores the names of four spices I like to use when making omelets. To print the contents of my list, I can use a for loop. Let's try it out. I'm going to type for spice in spices, colon. The for statement lets us specify a variable name that we can use in each iteration of the loop to reference the current value. The variable name can be anything. And here I'm using the variable name spice for clarity because each value I'm working with is a spice although we can debate about whether or not salt is a spice but I'm the cook here, so it's a spice. The next word in is a python keyword. It indicates that what follows is the set of values that we want to iterate through. And for that set of values, I'm referencing the spices list. Now remember that in addition to the data I want to loop through, I need two other pieces of information for a loop. One of those is where to stop. When I specify a list as the data to iterate through, my for loop will automatically end with the last value in the list. To specify what I want to do with each value, I create a new indented statement under the for statement and I'm going to use the print function and in parens, the word spice. My for statement starts with the list of spices. It assigns the first value to the spice variable and then executes the print statement. Then it moves to the next item in the list and repeats until there are no more items in the list. In this for statement, the spices list itself defines when the loop should start and when it should end. And the print statement specifies what the program should do with each value. I'll save my work and then execute the code in the terminal. The values of the spices list are printed one after the other. Remember that any statements after a loop are executed only after the loop is finished. But it's important not to indent them or they'll be executed in each iteration. For instance, I want to print no more boring omelets after all my list items are logged. What happens if I put that after the print spice statement and indented at the same level? So print and then a string. No more boring omelets. Well, let's save and run. The no more boring omelets phrase is printed after the name of each spice. This is because the indentation makes that final statement part of the for loop. That's not what we want. To fix that I can simply remove the indent. This takes the statement out of the loop and now it should be executed only after the entire for loop is finished. So I'll save and I'll run that again. And this time the names of all my spices are printed first and then my final statement is printed just once at the end. Using loops with lists or other collections allows us to work with sets of data without writing repetitive code. By combining iteration with other programming tools you can build your own complex and amazing projects.