WEBVTT

0
00:00.530 --> 00:00.980
All right.

1
00:00.980 --> 00:05.510
So the first thing I want to talk about is Dictionaries in Python.

2
00:05.510 --> 00:11.510
Now dictionaries in Python work kind of similarly to dictionaries in real life.

3
00:11.510 --> 00:11.990
Right?

4
00:11.990 --> 00:19.610
So if you were to look up a word in the dictionary, say the word code, then you might find the definition

5
00:19.610 --> 00:24.260
as something along the lines of, "program instructions for the computer."

6
00:24.260 --> 00:31.220
And dictionaries are really useful because they allow us to group together and tag related pieces of

7
00:31.220 --> 00:32.390
information.

8
00:32.540 --> 00:37.310
The way I like to think about dictionaries is in the form of a table.

9
00:37.370 --> 00:40.910
Every dictionary has two parts to it.

10
00:40.910 --> 00:47.330
On the left-hand side is the key, and that is the equivalent of the word in the dictionary,

11
00:47.540 --> 00:54.670
and then it's also got an associated value. That would be the equivalent of the actual definition of

12
00:54.670 --> 00:55.420
the word.

13
00:55.630 --> 01:02.500
Now let's say that we took this very simple table of definitions of programming words that we've come

14
01:02.500 --> 01:03.730
across so far,

15
01:03.730 --> 01:06.700
and we go ahead and we try to convert it into a dictionary.

16
01:06.730 --> 01:08.080
How would we do that?

17
01:08.080 --> 01:12.910
Let me firstly drop the last two rows and let's just start with the first one.

18
01:12.940 --> 01:20.740
The first thing we want to do is we want to create a dictionary and to do that in Python, this is what

19
01:20.740 --> 01:22.210
the syntax looks like. ({Key: Value})

20
01:22.210 --> 01:24.670
We have a set of curly braces.

21
01:24.670 --> 01:29.500
And everything that's inside the curly brace is the content of our dictionary.

22
01:29.620 --> 01:36.220
The key goes first, followed by a colon and then followed by the value.

23
01:36.250 --> 01:41.110
In our table we've got this word, "Bug" which is the first key.

24
01:41.110 --> 01:44.680
So we can replace that over here in our dictionary.

25
01:44.680 --> 01:49.510
And the value that's associated with this key is the definition for a bug.

26
01:49.510 --> 01:56.050
So, "An error in a program that prevents the program from running as expected." That becomes the value and

27
01:56.050 --> 01:58.690
can be replaced here after the colon.

28
01:58.690 --> 02:03.310
So now we've created an actual dictionary using Python code.

29
02:03.910 --> 02:07.630
What if you wanted to have more than one entry in your dictionary?

30
02:07.630 --> 02:15.060
Well, you would separate each of the key value pairs using a comma, and then you can continue adding

31
02:15.060 --> 02:20.190
key and value pairs until you get to the end of your dictionary.

32
02:21.330 --> 02:21.750
All right.

33
02:21.750 --> 02:24.600
Let's take a look at dictionaries in action.

34
02:24.630 --> 02:31.800
If you head over to the Day 9 start code file, then you can see that in here I've already added

35
02:31.920 --> 02:38.010
the dictionary that we were defining earlier on, and I've stored it inside a variable called,

36
02:38.010 --> 02:38.910
programming_dictionary.

37
02:39.180 --> 02:45.180
Now currently I've only got two entries in this dictionary, the definition for bug and the definition

38
02:45.180 --> 02:46.020
for function.

39
02:46.740 --> 02:52.620
Now the first thing I want to highlight is when you create a dictionary that has more than one element,

40
02:52.620 --> 02:59.460
such as in this case, then you want to take care to format it properly so that it's more easily readable.

41
02:59.610 --> 03:05.880
So what you'll see Python programmers do by convention is they will start off the dictionary with the

42
03:05.880 --> 03:13.140
open curly brace at the top, and then every subsequent entry is indented by one indent.

43
03:13.560 --> 03:17.760
And then at the very end of that entry there's a comma.

44
03:17.760 --> 03:22.730
And then we hit enter so that the next item goes on to the next line.

45
03:22.730 --> 03:31.040
And finally, the last curly brace should go at the very beginning in line with the start of the dictionary.

46
03:31.220 --> 03:38.390
And another thing that's quite nice to do is to cap off all entries in your dictionary or list with

47
03:38.390 --> 03:39.080
a comma.

48
03:39.110 --> 03:44.630
This means that if you needed to add more items into the dictionary, you can simply just hit Enter

49
03:44.630 --> 03:47.130
and continue typing the next thing.

50
03:47.460 --> 03:54.420
And if we wanted to add another entry, it's as simple as adding in the key, a colon, and then the value.

51
03:54.660 --> 03:58.140
And to cap it off again with a comma.

52
03:58.350 --> 04:06.900
So now our dictionary represents exactly the same data as we saw in our table over here with a bunch

53
04:06.900 --> 04:11.350
of key/value pairs and a total of three entries.

54
04:11.650 --> 04:18.100
Now, the next thing I want to do is what if I wanted to retrieve an item from the dictionary?

55
04:18.100 --> 04:23.800
Because we know that if we had a list, what we would do is we would use a set of square brackets,

56
04:23.800 --> 04:27.730
and then we would give the index of the item that we wanted.

57
04:27.730 --> 04:32.830
So the item at index 0 or 1 or 2 and so on and so forth.

58
04:32.860 --> 04:39.760
Now for dictionaries it's kind of similar in terms of syntax, but the only difference is that dictionaries

59
04:39.760 --> 04:43.690
have elements which are identified by their key.

60
04:44.080 --> 04:50.860
If we wanted this piece of information, for example, then all we have to do is tap into the dictionary

61
04:50.860 --> 04:53.920
and then add a set of square brackets,

62
04:53.920 --> 04:57.940
and inside the square brackets, we're going to provide the key.

63
04:57.970 --> 05:02.920
So here the key is a string and it's the string, "Bug".

64
05:02.920 --> 05:05.380
So let's go ahead and put Bug in here.

65
05:06.100 --> 05:13.420
And now if I go ahead and print this then you can see that it's going to give me the value, which is

66
05:13.420 --> 05:17.500
"An error in a program that prevents the program from running as expected."

67
05:17.530 --> 05:25.570
Now it's really, really important that you make sure that when you're fetching something out of a dictionary

68
05:25.570 --> 05:29.470
by its key, that you actually spell the key correctly.

69
05:29.830 --> 05:35.950
A really, really common error is when you're trying to retrieve something out of a dictionary and you've

70
05:35.950 --> 05:37.870
just made a very simple typo.

71
05:37.870 --> 05:42.220
So instead of u, I'm typing o here, and you'll see we get an error.

72
05:42.790 --> 05:47.950
And the error tells us that it's a KeyError referring to this particular key,

73
05:47.950 --> 05:53.580
and it highlights this Line 7, where we're trying to retrieve something out of this dictionary

74
05:53.580 --> 05:54.720
by this key.

75
05:54.720 --> 06:00.090
Basically, it's telling you that this key doesn't actually exist and it can't be found, so it doesn't

76
06:00.090 --> 06:01.950
know what it is that you want.

77
06:02.580 --> 06:09.270
Remember how when we had lists and when we tried to retrieve something that was not inside the list?

78
06:09.270 --> 06:15.390
So, for example, this particular list at index[4] doesn't actually exist because this is 0,

79
06:15.390 --> 06:20.910
1, 2, 3, and 4 is actually not a piece of data inside this list.

80
06:21.030 --> 06:27.930
Similarly with dictionaries, if we try to put in a key that doesn't exist, then we get this KeyError.

81
06:28.800 --> 06:37.200
Now another common pitfall that students fall down into is they don't actually use the correct data

82
06:37.200 --> 06:37.710
type.

83
06:37.740 --> 06:47.270
So for example, if when we defined this dictionary without putting a string around each of these keys,

84
06:47.270 --> 06:51.050
then it's going to error out and it won't even let us run.

85
06:51.050 --> 06:56.270
It's going to tell us undefined name bug, because it thinks that this is a variable that you've declared

86
06:56.270 --> 07:01.850
somewhere, but it's not, in fact, what you wanted are these strings for keys.

87
07:01.850 --> 07:07.910
And so when you have a key that is a string, when you're trying to retrieve the data from that key,

88
07:07.940 --> 07:13.340
you also have to make sure that you provide the key in its actual data type.

89
07:13.340 --> 07:19.760
So for example, if this was just a number say, 1, 2, 3, then of course all you have to write

90
07:19.760 --> 07:21.920
in here is just 1, 2, 3.

91
07:21.920 --> 07:26.270
And it would know that this piece of data is what you wanted.

92
07:27.590 --> 07:34.220
So that's how you retrieve items from a dictionary, by adding a square bracket and then giving it the

93
07:34.220 --> 07:34.880
key,

94
07:35.150 --> 07:40.550
and it will look for the key inside the dictionary and give you back the value.

95
07:40.850 --> 07:47.030
Now what if you wanted to add a piece of data such as that loop that we had earlier on, but you want

96
07:47.030 --> 07:48.470
to do it programmatically.

97
07:48.470 --> 07:55.180
So instead of doing it when you were defining the dictionary at the beginning, what if at some later

98
07:55.180 --> 07:58.480
stage in your program, you needed to add a new entry?

99
07:58.630 --> 08:01.690
Well, to do this, it's also really simple.

100
08:01.720 --> 08:08.770
All you have to do is to tap into the dictionary, which is called programming_dictionary in our case,

101
08:08.770 --> 08:12.580
and again using square brackets we define the key.

102
08:12.790 --> 08:16.060
The key I'm going to add is "Loop",

103
08:16.060 --> 08:20.650
and then after an equal sign I get to assign the value.

104
08:21.160 --> 08:25.360
So in my case the value is going to be the definition for a loop.

105
08:25.360 --> 08:34.060
And now when this line of code is executed and we go ahead and just print the programming_dictionary

106
08:34.060 --> 08:39.310
after this has happened, then we'll see that it's actually different from what we had before,

107
08:39.310 --> 08:40.750
so let's hit Run.

108
08:40.750 --> 08:47.110
You can see that previously we had a programming_dictionary that had only two items, Bug and Function,

109
08:47.110 --> 08:53.230
and after this line where we print our programming_dictionary again, you can see it's now got three

110
08:53.230 --> 08:55.420
items Bug, Function and Loop.

111
08:56.230 --> 09:03.400
Now very often when you're writing code, it can be really helpful to start out with an empty dictionary.

112
09:03.490 --> 09:08.910
Just as you saw previously, you can create an empty list by simply having a set of square brackets

113
09:08.910 --> 09:10.470
with nothing inside.

114
09:10.500 --> 09:19.050
You can also create an empty dictionary by simply creating a set of curly braces with nothing inside,

115
09:19.050 --> 09:25.590
and then add a later stage you can add to your dictionary by using this method that you saw here.

116
09:26.490 --> 09:31.560
Now on the other hand, you might actually want to wipe an entire dictionary.

117
09:31.770 --> 09:38.250
Here I'm creating a new empty dictionary by creating this pair of curly braces with nothing inside,

118
09:38.250 --> 09:44.160
but I can also wipe an existing dictionary by simply doing the same thing.

119
09:44.160 --> 09:51.150
We know that this dictionary, programming_dictionary has three items in it, but on Line 17 I'm going

120
09:51.150 --> 09:54.480
to say programming_dictionary equals empty dictionary.

121
09:54.480 --> 10:01.650
And now if I move this print statement down here, then you can see that when it prints out, it's actually

122
10:01.650 --> 10:03.480
going to be completely empty.

123
10:04.740 --> 10:09.930
That can be really useful if you wanted to clear out a user's progress,

124
10:09.930 --> 10:16.800
or for example, if a game restarts, then all the scores and stats will probably have to be wiped empty.

125
10:16.800 --> 10:19.140
So this is one way that you could do that.

126
10:19.440 --> 10:29.150
Now this method of tapping into a dictionary, using the key to fetch the relevant item from it, and

127
10:29.150 --> 10:32.840
then doing something with it, goes beyond just adding.

128
10:32.840 --> 10:37.400
You can also use this to edit an item in a dictionary.

129
10:38.780 --> 10:44.600
For example, let me go ahead and comment out these two lines of code so we don't wipe our programming

130
10:44.600 --> 10:45.410
dictionary.

131
10:45.410 --> 10:52.520
And instead I'm going to tap into the programming_dictionary, and I'm going to fetch the item that

132
10:52.520 --> 10:54.530
has a key of Bug.

133
10:54.530 --> 10:57.230
And I'm going to redefine its value.

134
10:57.230 --> 11:03.230
So currently it should be, "An error in a program that prevents the program from running as expected."

135
11:03.230 --> 11:12.070
And I can prove that if I just wrap this around a print statement. If instead I wanted this to be a

136
11:12.070 --> 11:17.860
different value, then I can simply use the same syntax as I did for adding new items,

137
11:17.860 --> 11:23.950
but in this case, I'm actually editing this entry because it's going to look through the dictionary,

138
11:23.950 --> 11:30.400
find a value with this key, and then assign it to whatever I put on the right hand side of the equals

139
11:30.400 --> 11:30.940
sign.

140
11:30.970 --> 11:36.970
Now on the other hand, if it finds nothing with that key then it's going to create a new entry with

141
11:36.970 --> 11:39.940
the value again on the right-hand side of the equal sign.

142
11:40.360 --> 11:45.310
Now let's say that a bug is instead, A moth in your computer."

143
11:46.390 --> 11:54.130
Now if I go ahead and print my programming_dictionary once more, then you can see that the definition

144
11:54.130 --> 11:57.190
for our bug has now just been changed.

145
11:58.750 --> 12:04.810
The final thing with regards to dictionaries that I think is really, really useful is how you loop

146
12:04.810 --> 12:09.970
through a dictionary and using your knowledge from lists,

147
12:09.970 --> 12:13.300
you might think that to loop through a dictionary you would,

148
12:13.300 --> 12:20.770
let's say we're using a for loop. And we say for thing in programming_dictionary,

149
12:20.770 --> 12:25.170
let's go ahead and print this thing each time.

150
12:25.500 --> 12:31.410
Here's a good moment to play computer and think what do you expect to be printed?

151
12:31.410 --> 12:35.250
So let's comment out all the other print statements so we don't get confused.

152
12:35.250 --> 12:40.140
And when I click run, what do you think will be printed?

153
12:42.300 --> 12:42.780
All right.

154
12:42.780 --> 12:44.310
Is that what you expected?

155
12:44.310 --> 12:46.500
Because it certainly wasn't what I expected

156
12:46.500 --> 12:52.590
when I first learned Python. I thought it would give me each of the items in the dictionary with this

157
12:52.590 --> 12:59.280
key and its value, but instead this code actually just gives you the keys.

158
13:00.150 --> 13:06.540
Now, of course, once you do have access to the key instead of thing, I should actually really say,

159
13:06.540 --> 13:12.540
'for key in programming_dictionary', if I wanted to print the key, then, of course, I could just write

160
13:12.540 --> 13:13.450
print key.

161
13:13.450 --> 13:20.080
But if I wanted to get hold of the value, I could equally just as easily tap into my dictionary, use

162
13:20.080 --> 13:23.170
the square bracket, and pass in that key.

163
13:23.200 --> 13:30.550
So now when I hit Run, you can see it's giving me first the key from this line and then secondly the

164
13:30.550 --> 13:33.100
value based on this line.

165
13:33.190 --> 13:37.790
I'm using that retrieval code in order to get the value.

166
13:38.690 --> 13:44.690
Of course, remember that you can get access to all the code that I'm writing here, and if you want

167
13:44.690 --> 13:50.810
to have a quick review of everything that we've gone through so far, because a lot of this knowledge

168
13:50.810 --> 13:57.590
is going to be tested in the next lesson, where we do a coding exercise that will really solidify everything

169
13:57.590 --> 13:59.720
you've learned in this lesson so far.

170
13:59.720 --> 14:03.200
So for all of that and more, I'll see you on the next lesson.