WEBVTT

1
00:00.000 --> 00:04.560
Now let's talk about two new data types, the list as well as the dictionary.

2
00:04.560 --> 00:08.040
And to begin, let's first focus on the list.

3
00:08.040 --> 00:10.640
Think about it like this, when we happen to say, for example,

4
00:10.640 --> 00:15.240
assign a string to a variable, all we're doing is kind of storing a singular

5
00:15.240 --> 00:15.600
thing,

6
00:15.600 --> 00:19.520
you know, like an email address or a name or perhaps we were using

7
00:19.520 --> 00:22.560
and end of your data type to store a singular number.

8
00:22.560 --> 00:26.000
When we're talking about utilizing something known as a list,

9
00:26.000 --> 00:29.920
what this is doing, it's giving us a way to store multiple values,

10
00:29.920 --> 00:31.120
so let me show you what I mean.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

10
00:29.920 --> 00:31.120
so let me show you what I mean.

11
00:31.120 --> 00:35.200
Now the way we actually do this in Python is we make the use of these square

12
00:35.200 --> 00:36.000
brackets.

13
00:36.000 --> 00:39.200
And for every single element in the list, we will separate them out,

14
00:39.200 --> 00:40.960
utilizing commas.

15
00:40.960 --> 00:43.040
Now right now, this might look a little bit strange.

16
00:43.040 --> 00:46.960
So the best thing to do is just to get hands on and see it in action.

17
00:46.960 --> 00:49.600
So if that said, let's just get to it them.

18
00:49.600 --> 00:53.440
So I'm just creating new example called example 2.py and open this up.

19
00:53.440 --> 00:56.720
Okay. So again, let's maybe say we want to have

20
00:56.720 --> 01:00.240
multiple items stored as a variable. Okay.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

20
00:56.720 --> 01:00.240
multiple items stored as a variable. Okay.

21
01:00.240 --> 01:03.840
So let's begin with a variable. Let's just call this myFoods.

22
01:03.840 --> 01:06.400
Okay, this is going to be something which is plural, obviously.

23
01:06.400 --> 01:09.440
So as opposed to just saying, you know, pizza as so,

24
01:09.440 --> 01:12.080
this would be a singular value as you print out.

25
01:12.080 --> 01:16.000
If you want to have multiple values, let's just wrap it within square brackets.

26
01:16.000 --> 01:19.120
Okay. Now right now, this is actually a list,

27
01:19.120 --> 01:21.680
even though we only have one value within it.

28
01:21.680 --> 01:22.720
Let me just prove it to you.

29
01:22.720 --> 01:26.960
If I just print the type of myFoods and save this,

30
01:26.960 --> 01:31.040
and let's just run example 2.py, we can see this is now a list object.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

30
01:26.960 --> 01:31.040
and let's just run example 2.py, we can see this is now a list object.

31
01:31.040 --> 01:34.480
Now as it stands right now, it's a list with only one value in it.

32
01:34.480 --> 01:37.360
So maybe not quite so useful, but it is still a list.

33
01:37.360 --> 01:40.240
However, if I want to add in more values like I say,

34
01:40.240 --> 01:43.200
I just have a comma and then I can separate the values.

35
01:43.200 --> 01:46.160
So that's maybe adding ice cream as well as chocolate.

36
01:46.160 --> 01:48.560
You can see here this is obviously a well-balanced diet.

37
01:49.920 --> 01:54.320
So let's go and we'll try to print out the MyFoods variable right here.

38
01:54.320 --> 01:56.160
Let's just hit Enter.

39
01:56.160 --> 02:00.320
As we can see here, we're now able to store these three different items
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

39
01:56.160 --> 02:00.320
As we can see here, we're now able to store these three different items

40
02:00.320 --> 02:03.200
within this one variable value right here, MyFoods.

41
02:03.200 --> 02:05.920
Now one thing to note about the list is that the elements within

42
02:05.920 --> 02:10.560
happen to be ordered, meaning that when we happen to write in the values,

43
02:10.560 --> 02:14.400
they are going to always be accessed in the same position,

44
02:14.400 --> 02:16.400
unless we happen to modify the list that is.

45
02:16.400 --> 02:17.280
So what do I mean?

46
02:17.280 --> 02:19.920
I mean that when I print out this list,

47
02:19.920 --> 02:23.520
pizza is always going to be first, then the second item will always be ice

48
02:23.520 --> 02:23.920
cream,

49
02:23.920 --> 02:25.680
then the third will always be chocolate.

50
02:25.680 --> 02:28.720
So no matter how often I run this script and keep printing this out,

51
02:28.720 --> 02:31.280
it's going to print out things in the exact same order.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

51
02:28.720 --> 02:31.280
it's going to print out things in the exact same order.

52
02:31.280 --> 02:32.400
This is important, okay?

53
02:32.400 --> 02:34.560
So because things are always in the same order,

54
02:34.560 --> 02:38.480
we can actually access them depending upon their position within the list.

55
02:38.480 --> 02:40.800
Now, there will be actually access elements within the list,

56
02:40.800 --> 02:43.040
is utilizing an index number.

57
02:43.040 --> 02:46.800
Now, the index number starts from the index zero,

58
02:46.800 --> 02:48.320
all the way up to the end of the list.

59
02:48.320 --> 02:50.800
So that means that right here,

60
02:50.800 --> 02:54.240
the first element is in list index position zero.

61
02:54.240 --> 02:56.640
The next element is in list index one,

62
02:56.640 --> 03:00.160
and the last element in this case here is in list index two.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

62
02:56.640 --> 03:00.160
and the last element in this case here is in list index two.

63
03:00.160 --> 03:04.720
And if we want to target particular pieces of data within that list,

64
03:04.720 --> 03:07.040
we just specify the index number.

65
03:07.040 --> 03:10.320
So say, for example, I want to get the first element within the list.

66
03:10.320 --> 03:13.520
I can just say MyFoods and then list position zero.

67
03:13.520 --> 03:15.680
If I run this, look at this right here.

68
03:15.680 --> 03:18.800
We're able to access pizza within this data type.

69
03:18.800 --> 03:20.240
Then if I change the number to one,

70
03:20.240 --> 03:21.520
what do you think we're going to print out?

71
03:21.520 --> 03:22.480
We're going to get ice cream.

72
03:22.480 --> 03:24.880
Then the number two, as we can see,

73
03:24.880 --> 03:26.320
we're going to get chocolate.

74
03:26.320 --> 03:28.480
And if I go above the value,

75
03:28.480 --> 03:29.680
we're going to get an error.

76
03:29.680 --> 03:30.960
As we can see right here,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

76
03:29.680 --> 03:30.960
As we can see right here,

77
03:30.960 --> 03:32.720
the list index is out of range.

78
03:32.720 --> 03:36.800
We do not actually have any element in list position three.

79
03:36.800 --> 03:38.960
Now, we actually can do things like adding to our list.

80
03:38.960 --> 03:41.680
I can say MyFoods and then append to the list,

81
03:41.680 --> 03:43.520
which is going to add on new value.

82
03:43.520 --> 03:45.120
So I get append in, let's go with.

83
03:45.600 --> 03:46.880
I don't know, let's say, stake.

84
03:46.880 --> 03:49.600
And if I then print out MyFoods,

85
03:49.600 --> 03:51.120
we have created the list,

86
03:51.120 --> 03:52.800
added on a new element,

87
03:52.800 --> 03:54.960
which is going to take the last place.

88
03:54.960 --> 03:57.360
And then suddenly, if I print out the value now,

89
03:57.360 --> 04:00.400
we can see here our list is now got a new element within it.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

89
03:57.360 --> 04:00.400
we can see here our list is now got a new element within it.

90
04:00.400 --> 04:02.240
List position three, the value of stake,

91
04:02.240 --> 04:05.280
which means that if we now try to grab index three

92
04:05.280 --> 04:06.560
and I run the script,

93
04:06.560 --> 04:08.400
we are able to access the value stake.

94
04:08.400 --> 04:11.200
Now one thing to note here with respect to our list,

95
04:11.200 --> 04:13.840
we can actually mix and match data types.

96
04:13.840 --> 04:15.840
So say, for example, the second data type

97
04:15.840 --> 04:16.880
was going to be a number.

98
04:16.880 --> 04:18.320
Let's just go with 45,

99
04:18.320 --> 04:20.640
not that it would make much sense in this context here,

100
04:20.640 --> 04:22.720
but nevertheless, let's just prove it is true.

101
04:22.720 --> 04:25.360
If I just print out the entirety of MyFoods

102
04:25.360 --> 04:26.640
and run the script again,

103
04:26.640 --> 04:28.560
I enter, look at this right here.

104
04:28.560 --> 04:31.040
We actually have an integer in amongst all
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

104
04:28.560 --> 04:31.040
We actually have an integer in amongst all

105
04:31.040 --> 04:32.320
of these other string values.

106
04:32.320 --> 04:33.680
And we can actually see here,

107
04:33.680 --> 04:36.480
the integer is not wrapped within quotation marks

108
04:36.480 --> 04:38.000
like the other ones are, okay?

109
04:38.000 --> 04:40.400
So a list is going to allow us to group together

110
04:40.400 --> 04:41.760
multiple pieces of data,

111
04:41.760 --> 04:43.680
even data of different data types.

112
04:43.680 --> 04:46.720
The elements within are going to follow a particular order

113
04:46.720 --> 04:49.360
and can be accessed via their position within the list.

114
04:49.360 --> 04:53.280
And we can modify the list as we just saw right here, okay?

115
04:53.280 --> 04:56.640
Now the next data type is going to be a dictionary.

116
04:56.640 --> 04:58.640
Now dictionaries are so so important

117
04:58.640 --> 05:00.480
in the world of network automation
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

117
04:58.640 --> 05:00.480
in the world of network automation

118
05:00.480 --> 05:02.240
because they are so commonly used.

119
05:02.240 --> 05:04.160
Pretty much this allows us to store data

120
05:04.160 --> 05:05.680
as key value pairs.

121
05:05.680 --> 05:07.920
So we assign a particular key

122
05:07.920 --> 05:10.560
and then return an associated value.

123
05:10.560 --> 05:12.160
Let me show you how this actually works.

124
05:12.160 --> 05:15.680
So what I will do is I'll create a dictionary called myinfo, okay?

125
05:15.680 --> 05:17.600
Now again, we'll use our equal signs

126
05:17.600 --> 05:21.520
and this time as opposed to utilizing square brackets,

127
05:21.520 --> 05:23.360
I'm going to use my curly braces.

128
05:23.360 --> 05:24.320
And what I will now do is,

129
05:24.320 --> 05:27.120
with quotation marks, I'm going to specify a key.

130
05:27.120 --> 05:29.120
So just give it a key, let's just say name.

131
05:29.120 --> 05:30.240
Then I'll have a colon
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

131
05:29.120 --> 05:30.240
Then I'll have a colon

132
05:30.240 --> 05:31.760
and then I'll have a corresponding value.

133
05:31.760 --> 05:33.520
So let's just say the corresponding value

134
05:33.520 --> 05:35.760
is a string which is John McGovern.

135
05:35.760 --> 05:38.080
And then I can have a comma.

136
05:38.080 --> 05:41.120
So this is the first key value pair, okay?

137
05:41.120 --> 05:44.160
And then after the comma, we can specify the next key value pair.

138
05:44.160 --> 05:46.880
So let's say the next key is going to be occupation

139
05:46.880 --> 05:48.960
and the corresponding value will be trainer.

140
05:48.960 --> 05:50.560
Then I'll have another comma.

141
05:50.560 --> 05:51.760
And what I can actually do here

142
05:51.760 --> 05:53.120
to make this a little bit more readable

143
05:53.120 --> 05:56.320
is to just do this over multiple lines as so.

144
05:56.320 --> 05:57.680
Then I can say hobbies.

145
05:57.680 --> 06:00.000
And what I could do is say I like to play golf.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

146
06:00.000 --> 06:01.840
So similar to a list object,

147
06:01.840 --> 06:04.320
we're able to store multiple pieces of data

148
06:04.320 --> 06:07.200
but as opposed to accessing the individual components

149
06:07.200 --> 06:09.840
via an index position as we see in the list,

150
06:09.840 --> 06:11.840
this time we access the values

151
06:11.840 --> 06:13.760
utilizing their corresponding key.

152
06:13.760 --> 06:16.160
So what I can do this time is I can say print

153
06:16.160 --> 06:18.480
the myinfo dictionary, try that again.

154
06:18.480 --> 06:20.000
There we go.

155
06:20.000 --> 06:22.480
So if I now print it myinfo and run this,

156
06:22.480 --> 06:24.480
we can still see the list is being printed

157
06:24.480 --> 06:27.120
because we are doing that right here as we see.

158
06:27.120 --> 06:29.200
But below notice we're also printing out

159
06:29.200 --> 06:31.360
the contents of this dictionary.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

159
06:29.200 --> 06:31.360
the contents of this dictionary.

160
06:31.360 --> 06:32.560
So look at the start difference.

161
06:32.560 --> 06:34.080
If I want to get a safe example,

162
06:34.080 --> 06:36.080
the pizza value from my foods

163
06:36.080 --> 06:38.240
I would use the list position zero.

164
06:38.240 --> 06:42.400
Whereas if I want to get the value of John McGuffern here,

165
06:42.400 --> 06:44.000
I don't use list position zero.

166
06:44.000 --> 06:46.720
Instead I use the name of the actual corresponding key,

167
06:46.720 --> 06:48.000
which is going to be my case here.

168
06:48.000 --> 06:51.760
If I do within square brackets and just say the name key.

169
06:51.760 --> 06:53.840
If I now run this, look at this right here.

170
06:53.840 --> 06:55.920
We're able to access the elements in the list

171
06:55.920 --> 06:57.120
via the index.

172
06:57.120 --> 06:59.520
And this one here we're able to access the element

173
06:59.520 --> 07:00.640
utilizing the key.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

173
06:59.520 --> 07:00.640
utilizing the key.

174
07:00.640 --> 07:03.520
And again, if I just change this to let's maybe say hobbies

175
07:03.520 --> 07:05.920
and save here, run the script again.

176
07:05.920 --> 07:08.560
As we can see here we're now getting a new value return.

177
07:08.560 --> 07:10.640
So this is our introduction to lists

178
07:10.640 --> 07:11.520
and dictionary.

179
07:11.520 --> 07:13.840
These are very, very popular concepts within Python.

180
07:13.840 --> 07:16.560
You're going to see a lot of it within network automation.

181
07:16.560 --> 07:18.640
With this now done, let's now turn our attention

182
07:18.640 --> 07:21.440
to the idea of conditional logic.
