1 00:00:00,340 --> 00:00:04,635 Welcome back, we're going to dive right into something we can actually do with 2 00:00:04,635 --> 00:00:06,057 sequences, iteration. 3 00:00:06,057 --> 00:00:09,760 You might have heard this term before or a similar term, iterable. 4 00:00:09,760 --> 00:00:12,760 Basically, iterating means looping over a sequence. 5 00:00:12,760 --> 00:00:15,690 If something is iterable it means it can be looped over. 6 00:00:15,690 --> 00:00:18,114 All Python sequences are iterable, they can all be looped over. 7 00:00:18,114 --> 00:00:20,981 And that's what we'll be talking about in this video, 8 00:00:20,981 --> 00:00:22,690 basic iteration with for loops. 9 00:00:23,740 --> 00:00:28,330 Basic iterating over Python sequences can be done with what we call a for loop, or 10 00:00:28,330 --> 00:00:31,060 more descriptively, a for in loop. 11 00:00:31,060 --> 00:00:35,253 A for loop in Python is just a way to perform an action on every element in 12 00:00:35,253 --> 00:00:37,394 a sequence, one by one, in order. 13 00:00:37,394 --> 00:00:39,239 The for loop will continue to loop or 14 00:00:39,239 --> 00:00:44,260 iterate on its own until it's worked its way through every element in the sequence. 15 00:00:44,260 --> 00:00:45,110 You can think of a for 16 00:00:45,110 --> 00:00:50,280 loop as saying for each element in a sequence, do this action. 17 00:00:50,280 --> 00:00:53,950 Where the element will be referenced by a variable name of your choice, and 18 00:00:53,950 --> 00:00:56,700 the action is whatever code is inside the for loop block. 19 00:00:57,960 --> 00:00:59,080 Consider a basic string. 20 00:01:00,280 --> 00:01:04,311 To iterate over each letter in this string, that is, to access all of 21 00:01:04,311 --> 00:01:08,775 the letters individually, one by one, and in order, we can use a for loop. 22 00:01:08,775 --> 00:01:11,900 The pseudo code becomes real code. 23 00:01:11,900 --> 00:01:15,250 Where letter is a variable I created to reference the current element 24 00:01:15,250 --> 00:01:18,010 in the my_name sequence on each iteration through the loop. 25 00:01:19,210 --> 00:01:21,990 All right, let's take this example a little further, 26 00:01:21,990 --> 00:01:23,580 feel free to follow along with me. 27 00:01:23,580 --> 00:01:27,123 You'll have an opportunity to try this on your own in a workspace in just 28 00:01:27,123 --> 00:01:27,959 a little while. 29 00:01:27,959 --> 00:01:31,485 Down on my command line tool, I'm gonna start up the python interpreter by typing 30 00:01:31,485 --> 00:01:34,257 python or python3 if I want to use the newest version of python. 31 00:01:38,902 --> 00:01:41,780 I'll recreate the my_name variable which held the string. 32 00:01:47,180 --> 00:01:49,481 And then I'll write a for loop that iterates over it and 33 00:01:49,481 --> 00:01:50,540 prints out each letter. 34 00:01:51,900 --> 00:01:54,390 To write the for loop, I write the word for. 35 00:01:54,390 --> 00:01:58,116 Then I create a variable that will store each element as the for loop iterates. 36 00:01:58,116 --> 00:02:01,303 I'll call it letter, then the word in, and 37 00:02:01,303 --> 00:02:04,848 then the name of the sequence which is my_name. 38 00:02:06,934 --> 00:02:08,210 Now I'll add a print statement. 39 00:02:11,266 --> 00:02:12,420 Now I'll hit Enter to run it. 40 00:02:14,300 --> 00:02:18,020 Cool, we can see here that the loop iterated six times, once for 41 00:02:18,020 --> 00:02:19,560 every letter in my name. 42 00:02:19,560 --> 00:02:23,920 On each loop, it printed out the letter that was being accessed in that iteration. 43 00:02:23,920 --> 00:02:26,930 Now you try, open up the attached workspace. 44 00:02:26,930 --> 00:02:31,630 Inside the groceries.py file, you'll see a Python list called groceries. 45 00:02:31,630 --> 00:02:34,680 Pause the video here and write your own for loop that iterates through 46 00:02:34,680 --> 00:02:37,830 all the elements in the groceries list and prints them out. 47 00:02:37,830 --> 00:02:40,990 When you think you've got it, or if you've gotten stuck, unpause and 48 00:02:40,990 --> 00:02:41,820 I'll show you the solution. 49 00:02:43,070 --> 00:02:44,930 Okay, how did it go? 50 00:02:44,930 --> 00:02:49,420 Let's exit the console and jump to the groceries.py file to see the solution. 51 00:02:49,420 --> 00:02:52,750 To exit the interpreter, I use Ctrl + D. 52 00:02:52,750 --> 00:02:54,456 And then I'll clear the console to free up some space. 53 00:02:59,132 --> 00:03:04,220 Okay, up in our file we have the groceries list with six elements. 54 00:03:04,220 --> 00:03:06,250 To write the for loop, I use keyword for. 55 00:03:07,450 --> 00:03:10,826 Then I'll create the loop variable, I'll call it item. 56 00:03:10,826 --> 00:03:14,160 And then the word in and the name of the sequence, groceries. 57 00:03:15,700 --> 00:03:17,397 On the next line, I'll write a print statement that prints out 58 00:03:17,397 --> 00:03:18,050 the current item. 59 00:03:18,050 --> 00:03:22,150 And that's it. 60 00:03:22,150 --> 00:03:22,780 If we save and 61 00:03:22,780 --> 00:03:26,540 run this, we'll see each element in the grocery list printed on its own line. 62 00:03:26,540 --> 00:03:35,850 Let's take a look Cool, there's our grocery list. 63 00:03:35,850 --> 00:03:38,520 Join me in the next video to learn about how we can take our loops 64 00:03:38,520 --> 00:03:39,170 to the next level.