1
00:00:00,000 --> 00:00:04,990
There are two fundamentals types of loops in Python. There is while loops and for

2
00:00:05,000 --> 00:00:08,990
loops, and let's start by looking at the while loop.

3
00:00:09,000 --> 00:00:15,990
So we'll take our whileloop.py and make a little copy of it, and call

4
00:00:16,000 --> 00:00:20,990
it whileloop-working.

5
00:00:21,000 --> 00:00:25,990
Go ahead and open that, and you'll see this is a simple Fibonacci series.

6
00:00:26,000 --> 00:00:28,990
It's a common exercise for while loops.

7
00:00:29,000 --> 00:00:33,990
So here we are assigning two values to a and b: 0 and 1. The 0 will get assigned

8
00:00:34,000 --> 00:00:40,990
to a, and the 1 will get assigned to b. We say while b < 50 and we go ahead and

9
00:00:41,000 --> 00:00:47,990
do things we'll make b grow in a Fibonacci series and we print it each time

10
00:00:48,000 --> 00:00:49,990
using the print function.

11
00:00:50,000 --> 00:00:56,990
So if I go ahead and run this and select the Python Run, you'll see here we get

12
00:00:57,000 --> 00:00:58,990
the Fibonacci series.

13
00:00:59,000 --> 00:01:03,990
So while loops just work like that. They say while, and then there is a condition.

14
00:01:04,000 --> 00:01:09,990
And as long as this condition is true, the while loop will continue to execute.

15
00:01:10,000 --> 00:01:13,990
And as soon as this condition becomes not true, then the while loop will end

16
00:01:14,000 --> 00:01:17,990
and execution will continue after it if there is any code after it, which in

17
00:01:18,000 --> 00:01:18,990
this case we don't have.

18
00:01:19,000 --> 00:01:26,990
Down here I could say print ("Done") and if I save that and run it, you'll see we get Done

19
00:01:27,000 --> 00:01:28,990
at the end.

20
00:01:29,000 --> 00:01:33,990
You'll notice that the print ("Done") is not indented. If I were to indent it

21
00:01:34,000 --> 00:01:35,990
here, then it would become part of the loop.

22
00:01:36,000 --> 00:01:41,990
But by having it back here, at the same indention level as the while statement

23
00:01:42,000 --> 00:01:49,990
itself, then it is no longer part of this suite of code that is controlled by the while loop.

24
00:01:50,000 --> 00:01:53,990
So that's how while loops work.

25
00:01:54,000 --> 00:01:57,990
Let's take a look at for loops.

26
00:01:58,000 --> 00:02:09,990
Go ahead and make a copy of forloop.py, and I'll name it forloop-working

27
00:02:10,000 --> 00:02:10,990
and we'll open that.

28
00:02:11,000 --> 00:02:14,990
What a for loop does is it works with what's called an iterator.

29
00:02:15,000 --> 00:02:17,990
An iterator is an object that each time you call it,

30
00:02:18,000 --> 00:02:20,990
it gives you the next value.

31
00:02:21,000 --> 00:02:26,990
So in this case, we open a file and it's this lines.txt, which we can see right

32
00:02:27,000 --> 00:02:28,990
here; it just has five lines of text.

33
00:02:29,000 --> 00:02:36,990
Then for line in fh, that's the file handle, which is actually an object and it

34
00:02:37,000 --> 00:02:40,990
has this method readlines, which is an iterator.

35
00:02:41,000 --> 00:02:47,990
So for each time this fh.readlines is called, it will put the next value in the

36
00:02:48,000 --> 00:02:51,990
line variable, which will be the next line of text, and then we'll print it.

37
00:02:52,000 --> 00:02:57,990
So when I run it, we get these lines of text.

38
00:02:58,000 --> 00:03:02,990
Now, you'll notice that there is an extra line ending, an extra new line, in

39
00:03:03,000 --> 00:03:06,990
each one of these, and that's because the print function puts a new line at the

40
00:03:07,000 --> 00:03:13,990
end, and each of these lines actually has a new line at the end of it, so we get two for each one.

41
00:03:14,000 --> 00:03:17,990
If you hover the mouse in Eclipse, you hover the mouse over the print function,

42
00:03:18,000 --> 00:03:18,990
you get a little bit of documentation for it.

43
00:03:19,000 --> 00:03:23,990
You'll notice that there is an end option, and it defaults to having this new line.

44
00:03:24,000 --> 00:03:31,990
So if I just say end= ' ', empty string like that and I save it and run it,

45
00:03:32,000 --> 00:03:34,990
now, we don't have that effect anymore.

46
00:03:35,000 --> 00:03:38,990
So, the for loop works likes this.

47
00:03:39,000 --> 00:03:44,990
It calls an iterator, and its values get put in this variable here.

48
00:03:45,000 --> 00:03:49,990
It has a colon, like all the control structures in Python, and the block of code

49
00:03:50,000 --> 00:03:52,990
underneath it is indented.

50
00:03:53,000 --> 00:03:56,990
So for each line of readlines, it will take the line, put it in the line variable,

51
00:03:57,000 --> 00:03:59,990
and then print it, just like we see here.

52
00:04:00,000 --> 00:04:04,990
So that's how for loops work in Python, and those are the two types of loops that

53
00:04:05,000 --> 00:04:05,990
we'll see in Python.

54
00:04:06,000 --> 00:04:10,990
We have while loops, which are traditional, conditional loops, and for loops,

55
00:04:11,000 --> 00:04:21,000
which work with iterators.

