1
00:00:00,700 --> 00:00:01,020
All right.

2
00:00:01,020 --> 00:00:06,020
So the first thing I want to talk about is dictionaries in Python. Now,

3
00:00:06,300 --> 00:00:11,300
dictionaries in Python work kind of similarly to dictionaries in real life,

4
00:00:11,520 --> 00:00:15,350
right? So if you were to look up a word in the dictionary

5
00:00:15,380 --> 00:00:20,380
say the word code, then you might find the definition as something along the

6
00:00:20,690 --> 00:00:25,690
lines of program instructions for the computer. And dictionaries are really

7
00:00:25,940 --> 00:00:30,940
useful because they allow us to group together and tag related pieces of

8
00:00:31,280 --> 00:00:32,113
information.

9
00:00:32,780 --> 00:00:37,040
The way I like to think about dictionaries is in the form of a table.

10
00:00:37,610 --> 00:00:40,700
Every dictionary has two parts to it.

11
00:00:41,210 --> 00:00:43,430
On the left hand side is the key,

12
00:00:44,120 --> 00:00:47,090
and that is the equivalent of the word in the dictionary,

13
00:00:47,840 --> 00:00:52,840
and then it's also got an associated value. That would be the equivalent

14
00:00:53,120 --> 00:00:55,100
of the actual definition of the word.

15
00:00:55,970 --> 00:01:00,970
Now let's say that we took this variation simple table of definitions of

16
00:01:01,250 --> 00:01:03,380
programming words that we've come across so far

17
00:01:03,920 --> 00:01:06,620
and we go ahead and we try to convert it right into a dictionary,

18
00:01:06,920 --> 00:01:08,960
how would we do that? Let me firstly

19
00:01:08,960 --> 00:01:12,710
drop the last two rows and let's just start with the first one.

20
00:01:13,280 --> 00:01:18,280
The first thing we want to do is we want them to create a dictionary. And to do

21
00:01:19,310 --> 00:01:22,010
that in Python, this is what the syntax looks like.

22
00:01:22,520 --> 00:01:26,210
We have a set of curly braces and everything that's inside

23
00:01:26,210 --> 00:01:29,240
the curly brace is the content of our dictionary.

24
00:01:29,870 --> 00:01:34,870
The key goes first followed by a colon and then followed by the value. In our

25
00:01:36,920 --> 00:01:41,000
table we've got this word bug, which is the first key,

26
00:01:41,390 --> 00:01:46,100
so we can replace that over here in our dictionary. And the value that's 

27
00:01:46,130 --> 00:01:49,550
associated with this key is the definition for a bug.

28
00:01:49,550 --> 00:01:53,480
So an error in a program that prevents the program from running as expected,

29
00:01:53,990 --> 00:01:58,460
that becomes the value and can be replaced here after the colon.

30
00:01:58,970 --> 00:02:03,140
So now, we've created an actual dictionary using Python code.

31
00:02:04,040 --> 00:02:08,090
What if you wanted to have more than one entry in your dictionary? Well,

32
00:02:08,090 --> 00:02:12,410
you would separate each of the key value pairs using a comma,

33
00:02:12,980 --> 00:02:17,980
and then you can continue adding key and value pairs until you get to the end of

34
00:02:19,280 --> 00:02:21,830
your dictionary. All right,

35
00:02:21,830 --> 00:02:24,380
let's take a look at dictionaries in action.

36
00:02:24,890 --> 00:02:29,890
If you head over to the day nine start code file and go ahead and fork your own

37
00:02:30,710 --> 00:02:35,150
copy to work alongside me, then you can see that in here

38
00:02:35,180 --> 00:02:39,320
I've already added the dictionary that we were defining earlier on,

39
00:02:39,740 --> 00:02:44,270
and I've stored it inside of a variable called programming dictionary. Now,

40
00:02:44,300 --> 00:02:47,060
currently I've only got two entries in this dictionary,

41
00:02:47,510 --> 00:02:51,830
the definition for a bug and the definition for function. Now,

42
00:02:51,830 --> 00:02:55,730
the first thing I want to highlight is when you create a dictionary that has

43
00:02:55,730 --> 00:02:58,610
more than one element such as in this case,

44
00:02:59,110 --> 00:03:03,310
then you want to take care to format it properly so that it's more easily

45
00:03:03,310 --> 00:03:04,143
readable.

46
00:03:04,510 --> 00:03:09,250
So what you'll see Python programmers do by convention is they will start off

47
00:03:09,280 --> 00:03:12,010
the dictionary with the open curly brace at the top

48
00:03:12,580 --> 00:03:17,580
and then every subsequent entry is indented by one indent.

49
00:03:18,550 --> 00:03:22,270
And then at the very end of that entry, there's a comma

50
00:03:22,660 --> 00:03:27,250
and then we hit enter so that the next item goes onto the next line

51
00:03:27,700 --> 00:03:28,960
and finally,

52
00:03:28,990 --> 00:03:33,990
the last curly brace should go at the very beginning in line with the start of

53
00:03:34,720 --> 00:03:35,553
the dictionary.

54
00:03:36,280 --> 00:03:41,020
And another thing that's quite nice to do is to cap off all entries in your

55
00:03:41,020 --> 00:03:43,570
dictionary or list with a comma.

56
00:03:44,080 --> 00:03:47,500
This means that if you needed to add more items into the dictionary

57
00:03:47,770 --> 00:03:51,610
you can simply just hit enter and continue typing the next thing.

58
00:03:52,210 --> 00:03:54,160
And if we wanted to add another entry,

59
00:03:54,160 --> 00:03:59,160
it's a simple as adding in the key, a colon and then the value and to cap it

60
00:04:00,340 --> 00:04:02,530
off again with a comma.

61
00:04:03,250 --> 00:04:08,250
So now our dictionary represents exactly the same data as we saw in our table

62
00:04:09,760 --> 00:04:14,760
over here with a bunch of key value pairs and a total of three entries.

63
00:04:16,570 --> 00:04:16,839
Now,

64
00:04:16,839 --> 00:04:21,839
the next thing I want to do is what if I wanted to retrieve an item from the

65
00:04:21,970 --> 00:04:25,150
dictionary, because we know that if we had a list

66
00:04:25,210 --> 00:04:28,330
what we would do is we would use a set of square brackets

67
00:04:28,660 --> 00:04:32,260
and then we would give the index of the item that we wanted.

68
00:04:32,620 --> 00:04:37,620
So the item at index zero or one or two and so on and so forth. Now for

69
00:04:38,290 --> 00:04:41,590
dictionaries, it's kind of similar in terms of syntax

70
00:04:41,890 --> 00:04:46,890
but the only difference is that dictionaries have elements which are identified

71
00:04:47,230 --> 00:04:51,700
by their key. If we wanted this piece of information, for example,

72
00:04:52,030 --> 00:04:57,030
then all we have to do is tap into the dictionary and then add a set of square

73
00:04:57,700 --> 00:05:02,410
brackets and inside the square brackets, we're going to provide the key.

74
00:05:02,890 --> 00:05:07,390
So here the key is a string and it's the string bug.

75
00:05:07,840 --> 00:05:09,910
So let's go ahead and put bug in here.

76
00:05:10,960 --> 00:05:14,440
And now if I go ahead and print this,

77
00:05:14,500 --> 00:05:17,500
then you can see that it is going to give me the value,

78
00:05:17,530 --> 00:05:21,160
which is 'An error in a program that prevents the program from running as

79
00:05:21,160 --> 00:05:24,190
expected.' Now it's really,

80
00:05:24,190 --> 00:05:29,190
really important that you make sure that when you're fetching something out of a

81
00:05:29,470 --> 00:05:33,820
dictionary by it's key that you actually spell the key correctly.

82
00:05:34,570 --> 00:05:35,260
A really

83
00:05:35,260 --> 00:05:39,340
really common error is when you're trying to retrieve something out of a

84
00:05:39,340 --> 00:05:43,480
dictionary and you've just made a very simple typo. So instead of 'u'

85
00:05:43,480 --> 00:05:45,760
I'm typing 'o' here and you'll see

86
00:05:45,760 --> 00:05:50,760
we get an error. And the error tells us that it's a key error referring to this

87
00:05:51,400 --> 00:05:56,400
particular key and it highlights this line 7 where we're trying to retrieve

88
00:05:56,620 --> 00:05:59,210
something out of this dictionary by this key.

89
00:05:59,660 --> 00:06:03,680
Basically it's telling you that this key doesn't actually exist and it can't be

90
00:06:03,680 --> 00:06:08,030
found. So it doesn't know what it is that you want. Remember how

91
00:06:08,030 --> 00:06:13,030
when we had lists and when we try to retrieve something that was not inside the

92
00:06:13,490 --> 00:06:17,870
list. So for example, this particular list at index 4

93
00:06:17,870 --> 00:06:21,560
doesn't actually exist because this is zero one, two, three,

94
00:06:21,980 --> 00:06:25,310
and four is actually not a piece of data inside of this list.

95
00:06:25,910 --> 00:06:30,590
Similarly with dictionaries, if we try to put in a key that doesn't exist,

96
00:06:30,650 --> 00:06:32,420
then we get this key error.

97
00:06:33,740 --> 00:06:38,740
Now another common pitfall that students fall down into is they don't actually

98
00:06:40,370 --> 00:06:43,880
use the correct data type. So for example,

99
00:06:44,030 --> 00:06:49,030
if we defined this dictionary without putting a string around each of these

100
00:06:51,110 --> 00:06:55,640
keys, then it's going to error out and it won't even let us run.

101
00:06:55,760 --> 00:06:59,540
It's going to tell us undefined named bug because it thinks that this is a

102
00:06:59,540 --> 00:07:01,460
variable that you've declared somewhere,

103
00:07:01,640 --> 00:07:06,440
but it's not. In fact, what you wanted are these strings for keys.

104
00:07:06,680 --> 00:07:09,650
And so when you have a key that is a string,

105
00:07:10,040 --> 00:07:12,620
when you're trying to retrieve the data from that key,

106
00:07:12,650 --> 00:07:17,650
you also have to make sure that you provide the key in its actual data type.

107
00:07:18,250 --> 00:07:22,390
So for example, if this was just a number, say one, two, three,

108
00:07:22,780 --> 00:07:26,320
then, of course, all you have to write in here is just one, two, three,

109
00:07:26,830 --> 00:07:30,760
and it would know that this piece of data is what you wanted.

110
00:07:32,530 --> 00:07:37,180
So that's how you retrieve items from a dictionary by adding a square bracket

111
00:07:37,240 --> 00:07:39,340
and then giving it the key.

112
00:07:39,940 --> 00:07:44,290
And it will look for the key inside the dictionary and give you back the

113
00:07:44,290 --> 00:07:46,060
value. Now,

114
00:07:46,060 --> 00:07:50,260
what if you wanted to add a piece of data such as that loop that we had earlier

115
00:07:50,260 --> 00:07:53,110
on, but you want to do it programmatically.

116
00:07:53,260 --> 00:07:55,300
So instead of doing it

117
00:07:55,300 --> 00:07:58,510
when you were defining the dictionary at the beginning,

118
00:07:58,690 --> 00:08:02,860
what if at some later stage in your program you needed to add a new entry?

119
00:08:03,580 --> 00:08:06,070
Well, to do this, it's also really simple.

120
00:08:06,670 --> 00:08:09,880
All you have to do is to tap into the dictionary,

121
00:08:10,060 --> 00:08:14,380
which is called programming dictionary in our case and again,

122
00:08:14,410 --> 00:08:17,110
using square brackets, we define the key.

123
00:08:17,710 --> 00:08:20,500
The key I'm going to add is our loop.

124
00:08:21,070 --> 00:08:25,180
And then after a equal sign, I get to assign the value.

125
00:08:26,020 --> 00:08:29,680
So in my case, the value is going to be the definition for a loop.

126
00:08:30,370 --> 00:08:35,370
And now when this line of code is executed and we go ahead and just print the

127
00:08:37,570 --> 00:08:40,179
programming dictionary after this has happened,

128
00:08:40,539 --> 00:08:43,900
then we'll see that it's actually different from what we had before.

129
00:08:44,110 --> 00:08:49,110
So let's hit run and you can see that previously we had a programming dictionary

130
00:08:49,120 --> 00:08:51,580
that had only two items, bug and function.

131
00:08:52,090 --> 00:08:56,910
And after this line 10, where we print our programming dictionary again,

132
00:08:57,240 --> 00:09:00,540
you can see it's now got three items, bug, function, and loop.

133
00:09:01,740 --> 00:09:04,380
Now very often when you're writing code,

134
00:09:04,500 --> 00:09:09,500
it can be really helpful to start out with a empty dictionary. Just as you saw

135
00:09:09,720 --> 00:09:10,290
previously,

136
00:09:10,290 --> 00:09:15,000
you create an empty list by simply having a set of square brackets with nothing

137
00:09:15,000 --> 00:09:15,833
inside.

138
00:09:16,020 --> 00:09:21,020
You can also create an empty dictionary by simply creating a set of curly braces

139
00:09:22,950 --> 00:09:25,950
with nothing inside. And then at a later stage,

140
00:09:25,980 --> 00:09:30,630
you can add to your dictionary by using this method that you saw here.

141
00:09:32,010 --> 00:09:33,150
Now, on the other hand,

142
00:09:33,180 --> 00:09:37,710
you might actually want to wipe an entire dictionary. Here,

143
00:09:37,740 --> 00:09:42,300
I'm creating a new empty dictionary by creating this pair of curly braces with

144
00:09:42,300 --> 00:09:43,230
nothing inside.

145
00:09:43,800 --> 00:09:48,800
But I can also wipe an existing dictionary by simply doing the same thing.

146
00:09:49,530 --> 00:09:53,940
We know that this dictionary, programming_dictionary, has three items in it.

147
00:09:54,360 --> 00:09:55,980
But on line 17,

148
00:09:56,010 --> 00:09:59,490
I'm going to say programming dictionary equals empty dictionary.

149
00:10:00,030 --> 00:10:03,270
And now if I move this print statement down here,

150
00:10:03,660 --> 00:10:06,120
then you can see that when it prints out,

151
00:10:06,330 --> 00:10:08,610
it's actually going to be completely empty.

152
00:10:10,200 --> 00:10:15,030
That can be really useful if you wanted to clear out a user's progress

153
00:10:15,300 --> 00:10:18,150
or for example, if a game restarts,

154
00:10:18,210 --> 00:10:21,990
then all the scores and stats will probably have to be wiped empty.

155
00:10:22,230 --> 00:10:24,090
So this is one way that you could do that.

156
00:10:25,020 --> 00:10:29,100
Now this method of tapping into a dictionary,

157
00:10:29,730 --> 00:10:33,660
using the key to fetch the relevant item from it

158
00:10:34,260 --> 00:10:37,920
and then doing something with it goes beyond just adding.

159
00:10:38,370 --> 00:10:42,330
You can also use this to edit an item in a dictionary.

160
00:10:44,280 --> 00:10:47,880
For example, let me go ahead and comment out these two lines of code

161
00:10:48,180 --> 00:10:53,180
so we don't wipe our programming dictionary and instead I'm going to tap into

162
00:10:53,730 --> 00:10:58,730
the programming dictionary and I'm going to fetch the item that has a key of bug

163
00:11:00,090 --> 00:11:02,430
and I'm going to redefine its value.

164
00:11:02,640 --> 00:11:06,690
So currently it should be an error in a program that prevents the program from

165
00:11:06,690 --> 00:11:08,280
running as expected.

166
00:11:08,820 --> 00:11:12,810
And I can prove that if I just wrap this around a print statement.

167
00:11:14,580 --> 00:11:18,450
If instead I wanted this to be a different value,

168
00:11:18,540 --> 00:11:23,010
then I can simply use the same syntax as I did for adding new items

169
00:11:23,340 --> 00:11:24,360
but in this case

170
00:11:24,420 --> 00:11:28,440
I'm actually editing this entry because it's going to look through the

171
00:11:28,440 --> 00:11:29,273
dictionary,

172
00:11:29,370 --> 00:11:34,370
find a value with this key and then assign it to whatever I put on the right

173
00:11:34,500 --> 00:11:37,500
hand side of the equal sign. Now, on the other hand,

174
00:11:37,500 --> 00:11:39,540
if it finds nothing with that key,

175
00:11:39,870 --> 00:11:43,200
then it's going to create a new entry with the value again

176
00:11:43,230 --> 00:11:44,970
on the right hand side of the equal sign.

177
00:11:45,900 --> 00:11:50,250
Now let's say that a bug is instead 'A moth in your computer.'

178
00:11:51,930 --> 00:11:56,890
Now, if I go and print my programming dictionary once more,

179
00:11:57,160 --> 00:12:02,160
then you can see that the definition for our bug has now just been changed.

180
00:12:04,150 --> 00:12:07,540
The final thing with regards to dictionaries that I think is really,

181
00:12:07,540 --> 00:12:12,540
really useful is how you loop through a dictionary. And using your knowledge from

182
00:12:14,350 --> 00:12:18,520
lists you might think that to loop through a dictionary, you would,

183
00:12:18,670 --> 00:12:22,840
let's say we are using a for loop. And we say for, um,

184
00:12:22,900 --> 00:12:25,840
thing in programming dictionary,

185
00:12:26,290 --> 00:12:30,130
let's go ahead and print this thing each time.

186
00:12:30,790 --> 00:12:34,270
Here's a good moment to play computer and think,

187
00:12:34,300 --> 00:12:36,640
what do you expect to be printed?

188
00:12:36,820 --> 00:12:38,950
So let's comment out all of the other print statements

189
00:12:38,950 --> 00:12:42,880
so we don't get confused. And when I click run,

190
00:12:43,480 --> 00:12:45,190
what do you think will be printed?

191
00:12:47,730 --> 00:12:47,880
Right?

192
00:12:47,880 --> 00:12:50,400
All right. Is that what you expected? Because it certainly

193
00:12:50,400 --> 00:12:53,130
wasn't what I expected when I first learned Python.

194
00:12:53,700 --> 00:12:58,680
I thought it would give me each of the items in the dictionary with this key and

195
00:12:58,680 --> 00:12:59,513
its value.

196
00:12:59,940 --> 00:13:04,350
But instead this code actually just gives you the keys.

197
00:13:05,700 --> 00:13:10,380
Now, of course, once you do have access to the key, instead of thing,

198
00:13:10,560 --> 00:13:13,770
I should actually really say for key in programming_dictionary.

199
00:13:14,310 --> 00:13:18,570
If I wanted to print the key, then of course I could just write print key.

200
00:13:18,960 --> 00:13:20,940
But if I wanted to get hold of the value,

201
00:13:21,120 --> 00:13:24,720
I could equally just as easily tap into my dictionary,

202
00:13:24,960 --> 00:13:28,230
use the square brackets and pass in that key.

203
00:13:28,740 --> 00:13:32,520
So now when I hit run, you can see it's giving me first

204
00:13:32,550 --> 00:13:35,520
the key from this line and then secondly

205
00:13:35,640 --> 00:13:38,040
the value based on this line.

206
00:13:38,670 --> 00:13:42,840
I'm using that retrieval code in order to get the value.

207
00:13:44,130 --> 00:13:44,550
Of course,

208
00:13:44,550 --> 00:13:49,050
remember that you can get access to all the code that I'm writing here by

209
00:13:49,050 --> 00:13:51,270
looking at the end code of the day

210
00:13:51,480 --> 00:13:53,910
that you'll find on the course resources list.

211
00:13:54,240 --> 00:13:58,380
And if you want to, have a quick review of everything that we've gone through so

212
00:13:58,380 --> 00:13:59,190
far,

213
00:13:59,190 --> 00:14:04,190
because a lot of this knowledge is going to be tested in the next lesson where

214
00:14:04,230 --> 00:14:08,820
we do a coding exercise that will really solidify everything you've learned in

215
00:14:08,820 --> 00:14:13,410
this lesson so far. So for all of that, and more, I'll see on the next lesson.

