1
00:00:00,720 --> 00:00:06,880
In previous lectures, you learned how to create and use sequencers and dictionaries.

2
00:00:07,260 --> 00:00:13,680
These are collections of multiple items and this is actually showing you how you can look through those

3
00:00:13,680 --> 00:00:18,870
collections so that you can process each of them containing items individually.

4
00:00:19,710 --> 00:00:26,460
So in the Python three documentation, there is Section five point six that talks about looping techniques

5
00:00:26,460 --> 00:00:29,250
in some of those who going to show you in this lecture.

6
00:00:29,250 --> 00:00:31,170
But you can go here for more if you're curious.

7
00:00:31,680 --> 00:00:32,100
All right.

8
00:00:32,100 --> 00:00:34,000
Let's go into our examples.

9
00:00:34,650 --> 00:00:37,170
Let's start with Rangers'.

10
00:00:37,170 --> 00:00:44,220
A couple of years ago, you learned that in order to create a range, you can use the range function.

11
00:00:44,220 --> 00:00:46,920
In this case, I'm using it with three parameters.

12
00:00:47,430 --> 00:00:54,660
So I'm going to create a range of numbers from zero to 50 50 non-inclusive step five account.

13
00:00:55,380 --> 00:01:03,660
So now to print out the individual items in this range, I can just print out the range itself.

14
00:01:03,760 --> 00:01:05,060
Show what happens if I do that.

15
00:01:05,940 --> 00:01:12,800
So if I just say print range of zero to 50, that's the name of the variable that I'm using here.

16
00:01:13,350 --> 00:01:16,950
What I'm going to get back is just the object.

17
00:01:19,510 --> 00:01:26,900
In order to print out the individual items in the range, you need to use a loop and to do that, going

18
00:01:27,190 --> 00:01:33,940
to create a loop using the full keyword, I'm going to give it a variable so that each time we go through

19
00:01:33,940 --> 00:01:39,910
the loop, the current item out of the loop will be stored in this arbitrary variable.

20
00:01:39,940 --> 00:01:42,100
So this name here is a variable.

21
00:01:42,280 --> 00:01:48,040
I choose to hold that current item that we are browsing through the range in this case.

22
00:01:49,030 --> 00:01:54,450
So this is going to be in this range zero to 50 step files.

23
00:01:55,300 --> 00:01:58,110
And this is going to be a block that I'm creating here.

24
00:01:58,110 --> 00:02:05,730
And that's why I need to finish with the column that dots Sony realizes that I'm creating a block because

25
00:02:05,740 --> 00:02:07,480
I've just finished with the column.

26
00:02:07,510 --> 00:02:10,420
So here I just want to print out what's inside.

27
00:02:11,650 --> 00:02:20,830
So the value stored in a number variable, enter and then enter again to get out of this block and the

28
00:02:21,280 --> 00:02:28,360
interpreter will just immediately execute my folder and print out the individual.

29
00:02:29,560 --> 00:02:31,600
Items out of this sequence.

30
00:02:33,000 --> 00:02:37,810
That's how you can create a loop from a range.

31
00:02:38,220 --> 00:02:42,000
Let's do something similar now, but this time for a list.

32
00:02:42,250 --> 00:02:47,050
So I've got a list of letters, which is this one here.

33
00:02:47,430 --> 00:02:49,950
We saw this for the first time a couple of years ago.

34
00:02:49,990 --> 00:02:54,610
So we got a list of letters to iterate through it in a loop.

35
00:02:54,750 --> 00:02:56,850
Use the exact same methodology.

36
00:02:57,030 --> 00:03:01,860
The only difference is that instead of a list created by the range function, we've got a list that

37
00:03:01,860 --> 00:03:11,550
we have explicitly created that contains letters, so say for later in the list of letters.

38
00:03:14,970 --> 00:03:17,570
Print out.

39
00:03:19,590 --> 00:03:27,090
Enter and enter again, and there are my three letters to exactly the same methodology for both range

40
00:03:27,090 --> 00:03:28,090
and lists.

41
00:03:28,860 --> 00:03:31,740
What about tuples by name?

42
00:03:31,770 --> 00:03:38,520
You can probably guess that when we are looping through a tuple, the method we follow is exactly the

43
00:03:38,520 --> 00:03:42,140
same as if we are looping through a list.

44
00:03:42,870 --> 00:03:44,570
So we're going to use it for keyword.

45
00:03:45,480 --> 00:03:53,350
We're going to go for just the generic word item since this list contains both numbers and letters.

46
00:03:53,410 --> 00:03:59,820
I'm just going to go for item and then the name of the tuple in this case.

47
00:04:05,310 --> 00:04:05,820
The.

48
00:04:08,000 --> 00:04:09,200
Interesting, so.

49
00:04:11,300 --> 00:04:19,980
I made a mistake here, did not include in and I just show you what happened could completely normally

50
00:04:19,980 --> 00:04:23,150
works unless there is a problem with the syntax that you're using.

51
00:04:23,580 --> 00:04:25,350
So I will go to you in here.

52
00:04:25,530 --> 00:04:28,650
And I went to use Teppo and then the code completion.

53
00:04:28,660 --> 00:04:30,800
But there's and nothing is happening.

54
00:04:30,810 --> 00:04:32,540
That's because the syntax broken.

55
00:04:33,150 --> 00:04:38,830
But if my syntax is correct, then code completion will work correctly as well.

56
00:04:39,420 --> 00:04:40,720
So is that a gotcha?

57
00:04:41,480 --> 00:04:41,940
All right.

58
00:04:41,950 --> 00:04:45,600
So then you can just print the item.

59
00:04:48,310 --> 00:04:54,780
And we have to loop goes through each item of the tuple and print it out.

60
00:04:56,050 --> 00:04:58,660
Now, the last thing that I want to show you, of course, is a dictionary.

61
00:04:58,660 --> 00:05:05,050
We can use dictionary in a slightly modified way because remember, the dictionary contains keys and

62
00:05:05,050 --> 00:05:05,820
values.

63
00:05:05,830 --> 00:05:13,210
So we can't just use a single variable, temporary variable here because we've got two things that we

64
00:05:13,210 --> 00:05:14,050
want to capture.

65
00:05:14,080 --> 00:05:16,830
We've got both the key and each value.

66
00:05:17,320 --> 00:05:20,840
So to make this work.

67
00:05:20,860 --> 00:05:27,060
Again, we start with the four key word, but then we say key value.

68
00:05:27,960 --> 00:05:30,920
And of course, these are arbitrary names.

69
00:05:30,940 --> 00:05:36,820
You can go for whichever name you want for the key and the value and the rest is the same.

70
00:05:36,840 --> 00:05:40,930
So we go for E.S.P three to six dictionary.

71
00:05:43,100 --> 00:05:52,490
And then for the print, we've got two things to print out now, shall we say, key comma value, and

72
00:05:52,490 --> 00:05:53,920
that's a mistake.

73
00:05:53,930 --> 00:05:54,560
What happened?

74
00:05:56,450 --> 00:05:57,670
Yes, it did make a mistake.

75
00:05:57,720 --> 00:06:01,370
So there's one more thing that I need to do and take a step back.

76
00:06:01,370 --> 00:06:10,550
So if I tried to print out a speech, 30 to spec dictionary, what you'll get back is a dictionary like

77
00:06:10,550 --> 00:06:10,910
this.

78
00:06:11,270 --> 00:06:19,170
But what we want to do instead is to get the items out of this dictionary like this.

79
00:06:19,190 --> 00:06:23,210
So now we have a dictionary items object, which looks like this.

80
00:06:23,930 --> 00:06:28,670
And therefore, in my loop, I want to use the items version of the dictionary.

81
00:06:28,850 --> 00:06:29,990
So let's try this again.

82
00:06:31,280 --> 00:06:42,170
I'm going to copy you save a bit of time and get the items for my loop and then print those items out

83
00:06:42,770 --> 00:06:53,180
sophisticated and then devalue it since and now we've got to manufacture a key and it's value, that

84
00:06:53,180 --> 00:06:56,120
type key and each value and so on.

85
00:06:57,510 --> 00:07:02,920
And that's how you can loop through sequences and dictionaries and put in.
