1
00:00:00,000 --> 00:00:03,990
One of the most common reasons for using a loop in programming is what's

2
00:00:04,000 --> 00:00:05,990
called an iterator.

3
00:00:06,000 --> 00:00:09,990
This is where you need to step through some data structure, taking one object

4
00:00:10,000 --> 00:00:14,990
at a time, and doing something with each of those objects before going back for the next one.

5
00:00:15,000 --> 00:00:17,990
Let's take a look at how this works in Python with the 'for' loop.

6
00:00:18,000 --> 00:00:25,990
We'll make a working copy of for.py, and call it for-working.py, and we'll go ahead

7
00:00:26,000 --> 00:00:27,990
and open that working copy.

8
00:00:28,000 --> 00:00:33,990
Here, we are opening a file with lines of text and reading that file line-by-

9
00:00:34,000 --> 00:00:36,990
line and printing out each line on the screen.

10
00:00:37,000 --> 00:00:40,990
So, let's go ahead and run it and see what it does, and we see that it prints

11
00:00:41,000 --> 00:00:43,990
these lines of text, and there they are.

12
00:00:44,000 --> 00:00:47,990
If we open the file here, lines.txt, we see that it has these five lines of text.

13
00:00:48,000 --> 00:00:52,990
You'll notice that in the file the lines are right next to each other, and in our

14
00:00:53,000 --> 00:00:55,990
result, it's printing a blank line between them.

15
00:00:56,000 --> 00:00:59,990
The reason for that is that the print function prints a blank line after each

16
00:01:00,000 --> 00:01:04,990
object that it prints, and the line itself has a line ending at the end of it.

17
00:01:05,000 --> 00:01:10,990
So, we can make print not to do that by typing end = " " just a empty string. If we

18
00:01:11,000 --> 00:01:14,990
save that and run it, then we get the lines of text, just like we expect them.

19
00:01:15,000 --> 00:01:20,990
So, the readlines method, inside the file object, is what's called an iterator.

20
00:01:21,000 --> 00:01:27,990
What this does is it takes one object at a time from a sequence of objects, and

21
00:01:28,000 --> 00:01:31,990
it returns those, one at a time, in this case into the line variable.

22
00:01:32,000 --> 00:01:36,990
The for loop is designed to work with iterators, so the for keyword introduces

23
00:01:37,000 --> 00:01:41,990
the for loop, and then a variable name, which is used to contain each object in

24
00:01:42,000 --> 00:01:45,990
the iteration, and in keyword introduces the iterator.

25
00:01:46,000 --> 00:01:52,990
So, it reads like for line in iteration or for line in file object readlines, and

26
00:01:53,000 --> 00:01:55,990
then it prints each line, one at a time.

27
00:01:56,000 --> 00:02:00,990
So, this here, this file object, .readlines, is the iterator.

28
00:02:01,000 --> 00:02:04,990
In Python, all the container types are iterators.

29
00:02:05,000 --> 00:02:12,990
So, if I have a list, let's say 1, 2, 3, 4, 5, then it will work exactly the same.

30
00:02:13,000 --> 00:02:15,990
It will just print 1, 2, 3, 4, 5 all in one line because we are not ending our

31
00:02:16,000 --> 00:02:18,990
lines with a new line here in the print function.

32
00:02:19,000 --> 00:02:21,990
So, if I save that and run it, we just get 12345.

33
00:02:22,000 --> 00:02:28,990
In fact, a string is a container object, and this will work exactly the same way.

34
00:02:29,000 --> 00:02:32,990
It will just take each letter from the string, and of course that will just look

35
00:02:33,000 --> 00:02:33,990
like the word string.

36
00:02:34,000 --> 00:02:37,990
But instead of we take this out and print them one line at a time, we can

37
00:02:38,000 --> 00:02:41,990
see that we are getting each letter as separate object.

38
00:02:42,000 --> 00:02:46,990
So, the string is a container that contains individual characters.

39
00:02:47,000 --> 00:02:54,990
In this case, we are printing the lines of the file, and the readlines method,

40
00:02:55,000 --> 00:02:59,990
from the file object, is the iterator that gives us one line at a time.

41
00:03:00,000 --> 00:03:03,990
So, if I save this and run it, there we get the lines of the file.

42
00:03:04,000 --> 00:03:04,990
So, that's the for loop.

43
00:03:05,000 --> 00:03:10,990
The for loop is used for stepping through iterators, and virtually all container

44
00:03:11,000 --> 00:03:13,990
types in Python are iterators.

45
00:03:14,000 --> 00:03:16,990
So, you'll see the for loop use a lot.

46
00:03:17,000 --> 00:03:22,990
In fact, in my informal survey of Python code, as I was preparing for this course,

47
00:03:23,000 --> 00:03:26,990
I saw probably 20 or 30 for loops for every while loop that I saw.

48
00:03:27,000 --> 00:03:31,990
The for loop is by far the most common type of a loop in Python because

49
00:03:32,000 --> 00:03:37,990
iteration is such a common thing to need to do, and so many objects in Python are iterators.

50
00:03:38,000 --> 00:03:43,990
So, that's the for loop, and that's how it works for line in readlines, so it's

51
00:03:44,000 --> 00:03:46,990
for variable in container.

52
00:03:47,000 --> 00:03:51,990
Then that variable becomes available to the suite of the loop, and that's how a for

53
00:03:52,000 --> 00:04:02,000
loop works in Python.

