Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the most common reasons for using a loop in programming is what's called an iterator. This is where you need to step through some data structure, taking one object at a time, and doing something with each of those objects before going back for the next one. Let's take a look at how this works in Python with the 'for' loop. We'll make a working copy of for.py, and call it for-working.py, and we'll go ahead and open that working copy. Here, we are opening a file with lines of text and reading that file line-by- line and printing out each line on the screen.
So, let's go ahead and run it and see what it does, and we see that it prints these lines of text, and there they are. If we open the file here, lines.txt, we see that it has these five lines of text. You'll notice that in the file the lines are right next to each other, and in our result, it's printing a blank line between them. The reason for that is that the print function prints a blank line after each object that it prints, and the line itself has a line ending at the end of it. So, we can make print not to do that by typing end = " " just a empty string. If we save that and run it, then we get the lines of text, just like we expect them.
So, the readlines method, inside the file object, is what's called an iterator. What this does is it takes one object at a time from a sequence of objects, and it returns those, one at a time, in this case into the line variable. The for loop is designed to work with iterators, so the for keyword introduces the for loop, and then a variable name, which is used to contain each object in the iteration, and in keyword introduces the iterator.
So, it reads like for line in iteration or for line in file object readlines, and then it prints each line, one at a time. So, this here, this file object, .readlines, is the iterator. In Python, all the container types are iterators. So, if I have a list, let's say 1, 2, 3, 4, 5, then it will work exactly the same. It will just print 1, 2, 3, 4, 5 all in one line because we are not ending our lines with a new line here in the print function.
So, if I save that and run it, we just get 12345. In fact, a string is a container object, and this will work exactly the same way. It will just take each letter from the string, and of course that will just look like the word string. But instead of we take this out and print them one line at a time, we can see that we are getting each letter as separate object. So, the string is a container that contains individual characters. In this case, we are printing the lines of the file, and the readlines method, from the file object, is the iterator that gives us one line at a time.
So, if I save this and run it, there we get the lines of the file. So, that's the for loop. The for loop is used for stepping through iterators, and virtually all container types in Python are iterators. So, you'll see the for loop use a lot. In fact, in my informal survey of Python code, as I was preparing for this course, I saw probably 20 or 30 for loops for every while loop that I saw. The for loop is by far the most common type of a loop in Python because iteration is such a common thing to need to do, and so many objects in Python are iterators.
So, that's the for loop, and that's how it works for line in readlines, so it's for variable in container. Then that variable becomes available to the suite of the loop, and that's how a for loop works in Python.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101328 Viewers
61 Video lessons · 88090 Viewers
71 Video lessons · 71944 Viewers
56 Video lessons · 103758 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.