1
00:00:00,500 --> 00:00:05,330
In previous section, you learned how to make a simple list and you learned how to work with individual

2
00:00:05,330 --> 00:00:06,170
elements in list.

3
00:00:06,800 --> 00:00:12,730
In this section, you will learn how to loop through an entire list using just a few lines of code,

4
00:00:12,740 --> 00:00:15,270
regardless of how long the list is.

5
00:00:15,290 --> 00:00:21,350
So looping allows you to make the same action or set of actions with every item in a list.

6
00:00:21,380 --> 00:00:27,590
As a result, you will be able to work efficiently with a list of any length, including those with

7
00:00:27,680 --> 00:00:29,630
thousands or even millions of items.

8
00:00:30,620 --> 00:00:37,340
And you will often want to run through all entries in the list.

9
00:00:38,740 --> 00:00:42,220
So performing the same task with each item.

10
00:00:42,220 --> 00:00:46,720
For example, in a game you might want to move every element.

11
00:00:47,560 --> 00:00:53,350
But on the screen to the same account practice.

12
00:00:55,800 --> 00:01:02,460
Um, or in a list of numbers you might want to perform the same statistical operation on every element.

13
00:01:02,460 --> 00:01:08,700
Or perhaps you will want to display each headline from a list of articles on a website.

14
00:01:08,700 --> 00:01:15,330
So when you want to do the same action with every item in a list, you can use The python's for loop,

15
00:01:15,780 --> 00:01:17,250
Python's for loop.

16
00:01:17,640 --> 00:01:24,210
So say we have a list of magician's names and we want to print out each name in the list.

17
00:01:25,190 --> 00:01:29,720
We could do this by retrieving each name from the list individually, but this approach could cause

18
00:01:29,720 --> 00:01:30,950
several problems here.

19
00:01:30,980 --> 00:01:35,810
For one, it would be repetitive to do this with a long list of names.

20
00:01:35,840 --> 00:01:42,470
Also, we'd have to change our code each time the list length created and using the for loop avoids

21
00:01:42,470 --> 00:01:48,530
both of these uses by letting Python manage these uses internally.

22
00:01:48,530 --> 00:01:52,640
So let's use a for loop to print out each name in a list of magicians.

23
00:01:52,640 --> 00:01:57,740
So here actually mutations.

24
00:02:01,930 --> 00:02:02,650
So just like that.

25
00:02:02,650 --> 00:02:03,160
Yes.

26
00:02:03,340 --> 00:02:04,860
So, magicians.

27
00:02:04,900 --> 00:02:07,330
And let's actually make it Alice.

28
00:02:10,010 --> 00:02:10,760
Alice.

29
00:02:10,940 --> 00:02:11,990
David.

30
00:02:13,650 --> 00:02:15,690
David and Carolina.

31
00:02:17,880 --> 00:02:20,010
And here we will create a for loop here.

32
00:02:20,010 --> 00:02:22,350
So for magician.

33
00:02:24,030 --> 00:02:25,860
In magicians.

34
00:02:26,570 --> 00:02:31,430
And after that we will print the magician every time.

35
00:02:32,300 --> 00:02:39,890
And here we begin by defining a list, just as we did in previous lectures.

36
00:02:39,920 --> 00:02:40,280
Oops.

37
00:02:40,280 --> 00:02:42,690
Here, let me fix my mouse right now.

38
00:02:42,710 --> 00:02:43,430
Yes.

39
00:02:43,550 --> 00:02:44,690
So here we go.

40
00:02:44,780 --> 00:02:50,630
We begin by defining a list, just as we did in previous lectures here.

41
00:02:52,280 --> 00:02:53,120
So.

42
00:02:54,580 --> 00:02:56,320
Then we define a for loop.

43
00:02:56,320 --> 00:02:57,910
This is new to us, right?

44
00:02:58,060 --> 00:03:02,050
So then we define the for loop.

45
00:03:03,210 --> 00:03:05,310
So this line.

46
00:03:06,070 --> 00:03:10,690
Tells Python to pull a name from the list.

47
00:03:10,720 --> 00:03:12,370
Magicians.

48
00:03:13,600 --> 00:03:14,350
And.

49
00:03:15,170 --> 00:03:18,950
Associated with the variable name magician.

50
00:03:20,180 --> 00:03:29,810
Next we tell Python to print the name that just been assigned to this magician here.

51
00:03:32,370 --> 00:03:37,380
Biden then repeats these last two lines, one for each name in the list.

52
00:03:37,410 --> 00:03:41,320
It might help to read this code as like this.

53
00:03:41,340 --> 00:03:46,170
So here we for this code, we have this explanation.

54
00:03:51,450 --> 00:03:57,210
Every magician in the list of magicians.

55
00:03:59,570 --> 00:04:00,650
Magicians.

56
00:04:03,450 --> 00:04:06,120
Print the magician's.

57
00:04:09,070 --> 00:04:09,520
Nay.

58
00:04:12,080 --> 00:04:16,520
So the output is simple printout of each name in the list.

59
00:04:16,550 --> 00:04:17,780
Let's try this out.

60
00:04:21,360 --> 00:04:30,450
As you can see here, we printed every user in the in the our magicians list without a problem.

61
00:04:32,470 --> 00:04:37,840
Now looping is important because it's one of the most common ways of computer automates repetitive tasks.

62
00:04:37,870 --> 00:04:44,860
For example, in a simple loop like we used in this file, in this lecture.

63
00:04:45,810 --> 00:04:46,560
Here.

64
00:04:47,880 --> 00:04:52,270
Python initially reads the first line of the loop.

65
00:04:52,290 --> 00:04:53,400
This line here.

66
00:04:53,550 --> 00:05:01,950
So this line tells Python to retrieve the first value from the list magicians and associated with the

67
00:05:01,950 --> 00:05:05,450
variable magician and the first value here.

68
00:05:05,460 --> 00:05:09,240
So here, actually, let me write that down here.

69
00:05:10,210 --> 00:05:10,810
Okay.

70
00:05:10,810 --> 00:05:14,500
So the first value here, The first.

71
00:05:14,530 --> 00:05:15,400
Oh, sorry.

72
00:05:16,000 --> 00:05:16,480
Here.

73
00:05:16,480 --> 00:05:18,640
The first value here.

74
00:05:19,420 --> 00:05:21,490
Is Alice, right?

75
00:05:21,640 --> 00:05:22,400
Yes.

76
00:05:22,420 --> 00:05:25,090
The first value here is.

77
00:05:29,420 --> 00:05:30,980
And the second value.

78
00:05:32,270 --> 00:05:38,390
And I'll also python prints the first value as Alice and the current.

79
00:05:38,510 --> 00:05:43,730
This is the current value of magician right now which is still Alice because the list contains more

80
00:05:43,730 --> 00:05:49,850
values and Python returns to the first line of the loop.

81
00:05:50,090 --> 00:05:50,960
Right.

82
00:05:51,320 --> 00:05:56,630
So Python retrieves the next name in the list, which is.

83
00:05:58,280 --> 00:05:59,120
In this case.

84
00:05:59,120 --> 00:06:00,110
David Right.

85
00:06:00,110 --> 00:06:02,420
So Python also retrieves the.

86
00:06:02,420 --> 00:06:03,050
David.

87
00:06:06,980 --> 00:06:12,950
And associates that value within the variable magician here.

88
00:06:13,250 --> 00:06:20,720
And Python then executes the line which is print the magician and Python prints the current value of

89
00:06:20,720 --> 00:06:28,760
magician again which is now is David and Python repeats the entire loop once more with the last value

90
00:06:28,760 --> 00:06:31,700
in the list, which is Caroline.

91
00:06:32,000 --> 00:06:33,470
This here.

92
00:06:34,080 --> 00:06:36,330
So because no more values are in the list.

93
00:06:36,330 --> 00:06:39,900
Python moves on to the next line of the program.

94
00:06:39,900 --> 00:06:41,970
In this case, nothing.

95
00:06:42,000 --> 00:06:45,750
Nothing comes after this line here.

96
00:06:46,590 --> 00:06:48,900
So this is empty here for now.

97
00:06:50,450 --> 00:06:51,020
And.

98
00:06:53,490 --> 00:06:55,980
And nothing comes after the for loop.

99
00:06:55,980 --> 00:06:59,160
And so for loop will end here.

100
00:06:59,160 --> 00:07:05,610
So when you are using the loops for the first time, keep in mind that the set of steps is repeated

101
00:07:05,610 --> 00:07:07,980
once for each item in the list.

102
00:07:07,980 --> 00:07:12,630
So no matter how many items are in the list and how much.

103
00:07:12,630 --> 00:07:19,050
So if you have a million items in your list, the python repeats these steps a million times and usually

104
00:07:19,050 --> 00:07:19,950
very quickly.

105
00:07:20,320 --> 00:07:26,820
Also, keep in mind when writing your own for loops that you can choose any name you want for the temporary

106
00:07:26,820 --> 00:07:27,600
variable.

107
00:07:28,660 --> 00:07:30,850
That will be associated with each value in the list.

108
00:07:30,850 --> 00:07:35,710
So however itself will do, choose a meaningful name that represents a single item for the list.

109
00:07:35,920 --> 00:07:38,920
So for example, you can.

110
00:07:39,310 --> 00:07:45,040
Let's actually create a good way to for naming these loops here.

111
00:07:45,760 --> 00:07:56,200
So another good way for naming this loop is, for example, look for Cat in cats or for dog in dogs

112
00:07:56,230 --> 00:08:02,710
or for item in list of items.

113
00:08:02,740 --> 00:08:07,120
Okay, so these are the just examples that you can name your loops.

114
00:08:07,120 --> 00:08:10,430
So to make it more meaningful and readable.

115
00:08:10,450 --> 00:08:10,990
Right.

116
00:08:10,990 --> 00:08:17,710
So these naming conventions can help you to follow the action behind being done on each item within

117
00:08:17,710 --> 00:08:18,610
a for loop.

118
00:08:19,120 --> 00:08:24,190
And using singular and plural names can help you identify whether a section of code is working with

119
00:08:24,190 --> 00:08:28,820
a single element from the list or the entire list at.
