Iterating with Basic For Loops3:39 with Ashley Boucher
Use basic Python for loops to iterate over your sequence data.
Welcome back, we're going to dive right into something we can actually do with 0:00 sequences, iteration. 0:04 You might have heard this term before or a similar term, iterable. 0:06 Basically, iterating means looping over a sequence. 0:09 If something is iterable it means it can be looped over. 0:12 All Python sequences are iterable, they can all be looped over. 0:15 And that's what we'll be talking about in this video, 0:18 basic iteration with for loops. 0:20 Basic iterating over Python sequences can be done with what we call a for loop, or 0:23 more descriptively, a for in loop. 0:28 A for loop in Python is just a way to perform an action on every element in 0:31 a sequence, one by one, in order. 0:35 The for loop will continue to loop or 0:37 iterate on its own until it's worked its way through every element in the sequence. 0:39 You can think of a for 0:44 loop as saying for each element in a sequence, do this action. 0:45 Where the element will be referenced by a variable name of your choice, and 0:50 the action is whatever code is inside the for loop block. 0:53 Consider a basic string. 0:57 To iterate over each letter in this string, that is, to access all of 1:00 the letters individually, one by one, and in order, we can use a for loop. 1:04 The pseudo code becomes real code. 1:08 Where letter is a variable I created to reference the current element 1:11 in the my_name sequence on each iteration through the loop. 1:15 All right, let's take this example a little further, 1:19 feel free to follow along with me. 1:21 You'll have an opportunity to try this on your own in a workspace in just 1:23 a little while. 1:27 Down on my command line tool, I'm gonna start up the python interpreter by typing 1:27 python or python3 if I want to use the newest version of python. 1:31 I'll recreate the my_name variable which held the string. 1:38 And then I'll write a for loop that iterates over it and 1:47 prints out each letter. 1:49 To write the for loop, I write the word for. 1:51 Then I create a variable that will store each element as the for loop iterates. 1:54 I'll call it letter, then the word in, and 1:58 then the name of the sequence which is my_name. 2:01 Now I'll add a print statement. 2:06 Now I'll hit Enter to run it. 2:11 Cool, we can see here that the loop iterated six times, once for 2:14 every letter in my name. 2:18 On each loop, it printed out the letter that was being accessed in that iteration. 2:19 Now you try, open up the attached workspace. 2:23 Inside the groceries.py file, you'll see a Python list called groceries. 2:26 Pause the video here and write your own for loop that iterates through 2:31 all the elements in the groceries list and prints them out. 2:34 When you think you've got it, or if you've gotten stuck, unpause and 2:37 I'll show you the solution. 2:40 Okay, how did it go? 2:43 Let's exit the console and jump to the groceries.py file to see the solution. 2:44 To exit the interpreter, I use Ctrl + D. 2:49 And then I'll clear the console to free up some space. 2:52 Okay, up in our file we have the groceries list with six elements. 2:59 To write the for loop, I use keyword for. 3:04 Then I'll create the loop variable, I'll call it item. 3:07 And then the word in and the name of the sequence, groceries. 3:10 On the next line, I'll write a print statement that prints out 3:15 the current item. 3:17 And that's it. 3:18 If we save and 3:22 run this, we'll see each element in the grocery list printed on its own line. 3:22 Let's take a look Cool, there's our grocery list. 3:26 Join me in the next video to learn about how we can take our loops 3:35 to the next level. 3:38
You need to sign up for Treehouse in order to download course files.Sign up