1
00:00:05,359 --> 00:00:08,960
So what is a dictionary?
well a dictionary is a collection of

2
00:00:08,960 --> 00:00:13,920
values that are stored using a key.
As an example, employees in a company

3
00:00:13,920 --> 00:00:18,720
might be given an employee id. That
would form their key, and the value

4
00:00:18,720 --> 00:00:22,960
would be their personal records.
When a card triggers a speed camera, the

5
00:00:22,960 --> 00:00:25,439
car's registration number,
or license number,

6
00:00:25,439 --> 00:00:30,240
is recorded. That becomes the key that's
used to find the owner of the vehicle,

7
00:00:30,240 --> 00:00:34,399
so that a speeding fine can be issued.

8
00:00:34,880 --> 00:00:38,960
If the camera only recorded that it was
a blue car, the system wouldn't function

9
00:00:38,960 --> 00:00:42,399
very well. The key's needed
to find the records for

10
00:00:42,399 --> 00:00:45,680
that particular blue car, in
the collection of all cars in the

11
00:00:45,680 --> 00:00:49,280
country. So that's basically
what a dictionary is.

12
00:00:49,280 --> 00:00:52,480
Every value that's stored in the
dictionary has a key.

13
00:00:52,480 --> 00:00:55,520
We use the key to get the
values from the dictionary.

14
00:00:55,520 --> 00:00:59,359
You'll find dictionaries described
as storing key value pairs.

15
00:00:59,359 --> 00:01:04,640
Each value has a unique key,
that's used to refer to it.

16
00:01:04,640 --> 00:01:07,680
Alright, so let's look at some examples
that are going to demonstrate this.

17
00:01:07,680 --> 00:01:10,880
So we need to - in our project
we created in the last video -

18
00:01:10,880 --> 00:01:16,720
create a new python file, and we'll call
this one dict_intro.

19
00:01:23,680 --> 00:01:27,880
So we're going to start with a dictionary
to store the values for different vehicles;

20
00:01:27,880 --> 00:01:30,079
vehicles equals and a left curly brace,

21
00:01:30,079 --> 00:01:33,680
and you can see that the right curly
brace was added by IntelliJ.

22
00:01:33,680 --> 00:01:37,920
So to create a dictionary literal,
we use curly braces.

23
00:01:37,920 --> 00:01:41,439
Within the curly braces,
we have keys and values,

24
00:01:41,439 --> 00:01:46,159
separated by a colon. Now, I gave the
example of vehicle records in the last video.

25
00:01:46,159 --> 00:01:50,159
The key, in that case, would be the
vehicle's registration number.

26
00:01:50,159 --> 00:01:53,200
But things are complicated enough,
without having to deal with

27
00:01:53,200 --> 00:01:57,940
real registration numbers
such as HHU494V, for example,

28
00:01:57,940 --> 00:02:03,360
so I'm going to use friendlier names for
the keys. Now Honda called their 250 T motorbike,

29
00:02:03,360 --> 00:02:07,040
the dream, for example. That's the sort
of names I'm going to use for the keys.

30
00:02:07,040 --> 00:02:09,000
So I'm going to paste in some data,

31
00:02:09,000 --> 00:02:13,460
over the top of what I typed.
Now pause the video while you catch up

32
00:02:13,460 --> 00:02:17,520
with the typing.
As you can see, a dictionary looks a bit

33
00:02:17,520 --> 00:02:21,040
like a list. We use curly braces instead
of square brackets -

34
00:02:21,040 --> 00:02:25,920
you can see them on lines 1 and 10. The
other difference is that each entry is

35
00:02:25,920 --> 00:02:30,160
in two parts.
We have a key - things like dream, virago

36
00:02:30,160 --> 00:02:35,440
and tenere.  After the key, and separated
from it with a colon, comes the value.

37
00:02:35,440 --> 00:02:38,720
The values in this dictionary are the
string containing the manufacturer's

38
00:02:38,720 --> 00:02:41,880
name and model. That's why we talk about

39
00:02:41,880 --> 00:02:46,620
dictionaries storing key value pairs.
Each entry on lines 2 to 9,

40
00:02:46,620 --> 00:02:50,400
consists of a key
and a value. Okay, so we've got a

41
00:02:50,400 --> 00:02:54,959
dictionary containing details on eight
vehicles. Let's see some things we can do

42
00:02:54,959 --> 00:02:57,440
with it.
We can get values from the dictionary

43
00:02:57,440 --> 00:03:01,519
by providing the key.
That works in a similar way to indexing

44
00:03:01,520 --> 00:03:05,600
into a list, but we can use the
key instead of the index position.

45
00:03:05,600 --> 00:03:08,640
Let's type some code in.
I'm going start on line 12;

46
00:03:08,640 --> 00:03:14,720
my_car is equal to vehicles
and square brackets,

47
00:03:14,720 --> 00:03:19,560
single quote and fiesta.
Then on the next line,

48
00:03:19,560 --> 00:03:26,640
print parentheses my_car.
So if you run the program now,

49
00:03:26,879 --> 00:03:34,000
and my_car is a Ford Fiesta Ghia 1.4.
So you can see there that we provided

50
00:03:34,000 --> 00:03:37,740
the key inside the square brackets,
and we get the value returned.

51
00:03:37,740 --> 00:03:41,920
So it looks a bit like indexing a sequence,
such as a string or a list. In fact, it is

52
00:03:41,920 --> 00:03:45,200
similar to indexing,
but when working with dictionaries,

53
00:03:45,200 --> 00:03:49,580
we use the key as the index, rather
than the position. So note that we use

54
00:03:49,580 --> 00:03:53,980
curly braces to create a dictionary -
that's on lines one and ten.

55
00:03:53,980 --> 00:03:56,879
I've already mentioned that.
I'm repeating it because we use square

56
00:03:56,879 --> 00:03:59,519
brackets when we want
to retrieve a value.

57
00:03:59,519 --> 00:04:03,200
We can see the square
brackets on line 12.

58
00:04:03,200 --> 00:04:07,040
Alright, so let's see another example of
retrieving a value, before I move on;

59
00:04:07,040 --> 00:04:14,400
commuter is equal to vehicles
square bracket single quotes virago,

60
00:04:14,400 --> 00:04:18,000
and you can see there that
Python actually recognizes

61
00:04:18,000 --> 00:04:21,440
the dictionary, and it's returning the
dictionary key virago. You can see on

62
00:04:21,440 --> 00:04:24,560
the right-hand side, on the
on the pop-up there, it says dict key.

63
00:04:24,560 --> 00:04:30,900
So virago, and we'll print that
out as well - print commuter.

64
00:04:30,900 --> 00:04:35,440
J-P used a little virago to get to work
because it did over 86 miles to the gallon,

65
00:04:35,440 --> 00:04:38,040
So I've called the variable
commuter, there.

66
00:04:38,040 --> 00:04:41,440
Now that gets bound to the
dictionary value for the key virago.

67
00:04:41,440 --> 00:04:44,880
If we run the program,
check the right value -

68
00:04:44,880 --> 00:04:49,240
or the correct value - you can see
that it shows the yamaha XV250.

69
00:04:49,240 --> 00:04:52,080
Alright, so that's one
way to retrieve values

70
00:04:52,080 --> 00:04:57,360
from a dictionary. You index a dictionary
using the key for the value you want.

71
00:04:57,360 --> 00:05:01,520
Dictionaries also have a get method.
I'll demonstrate how to use get,

72
00:05:01,520 --> 00:05:05,120
then we'll see why you might use it,
rather than indexing.

73
00:05:05,120 --> 00:05:10,479
Alright, so we'll start on
line 18 by typing learner

74
00:05:10,479 --> 00:05:18,080
is equal to vehicles.get
parentheses, and in double quotes er5

75
00:05:18,080 --> 00:05:21,280
double quote right parenthesis, as you can
see there. On the next line,

76
00:05:21,280 --> 00:05:25,980
we're going to print parentheses learner.

77
00:05:25,980 --> 00:05:29,440
The Kawasaki ER5 was popular with
riding schools, which is why I've called

78
00:05:29,440 --> 00:05:33,120
the variable learner.
So on line 18, we call the dictionary's

79
00:05:33,120 --> 00:05:36,240
get method.
We provide the key for the item we want,

80
00:05:36,240 --> 00:05:38,720
and the method returns the value from
the dictionary.

81
00:05:38,720 --> 00:05:41,759
When I run the program now,

82
00:05:42,000 --> 00:05:45,360
you can see that Kawasaki ER5
is printed out.

83
00:05:45,360 --> 00:05:49,199
Alright, so we've now got two ways to
retrieve values from the dictionary.

84
00:05:49,199 --> 00:05:52,720
You can use the key as an index,
which I'm doing on lines 12,

85
00:05:52,720 --> 00:05:57,840
and also on lines 15, or we can
pass the key to the get method,

86
00:05:57,840 --> 00:06:02,400
as I'm doing on line 18. I'm going to
finish this video by discussing

87
00:06:02,400 --> 00:06:05,960
when you might use each method,
and what their advantages are.

88
00:06:05,960 --> 00:06:08,800
Before that,  I want to point out
something that might not be obvious.

89
00:06:08,800 --> 00:06:13,120
The keys that we use, when either
indexing or calling dot get,

90
00:06:13,120 --> 00:06:17,960
must match exactly. Our keys
are strings in this dictionary,

91
00:06:17,960 --> 00:06:21,919
and strings are case sensitive.
That means we can't use er5 in capitals,

92
00:06:21,919 --> 00:06:25,199
for example.
Let's see what happens if we do that.

93
00:06:25,199 --> 00:06:28,800
Let's capitalize ER there,

94
00:06:29,520 --> 00:06:32,639
and if we run the program again now,
you can see down the

95
00:06:32,639 --> 00:06:36,800
output at the bottom left-hand corner, we
get None as the last line of the output.

96
00:06:36,800 --> 00:06:40,720
The string ER5 in uppercase, doesn't
match any of the keys

97
00:06:40,720 --> 00:06:43,919
in the dictionary, and you
can see the dictionary there.

98
00:06:43,920 --> 00:06:46,740
If the key doesn't exist
when we use it for indexing,

99
00:06:46,740 --> 00:06:50,880
we instead, get an error. I'm going
to demonstrate that by changing

100
00:06:50,880 --> 00:06:55,599
fiesta to have a capital F.

101
00:06:56,800 --> 00:06:59,840
If we run that again now,

102
00:07:00,479 --> 00:07:04,160
the program's crashed, as you can see,
and we've got a key error showing there,

103
00:07:04,160 --> 00:07:07,039
Fiesta.
An error helpfully tells us which key

104
00:07:07,039 --> 00:07:12,240
doesn't exist -
Fiesta with a capital F, in this case.

105
00:07:12,240 --> 00:07:19,360
So that's one reason why you might
use dot get, rather than indexing.

106
00:07:19,360 --> 00:07:22,479
So if the key doesn't exist,
dot get will return none

107
00:07:22,479 --> 00:07:26,080
whereas indexing will crash
with a key error, as you saw.

108
00:07:26,080 --> 00:07:31,840
The get method is useful when you're
not sure if the key exists or not.

109
00:07:31,919 --> 00:07:35,039
Now you may be wondering why not just
use the get method all the time. Why

110
00:07:35,039 --> 00:07:40,639
bother with indexing, if it can crash?
One answer to that is indexing is faster.

111
00:07:40,639 --> 00:07:44,800
If you know that the key will be present,
then use indexing with square brackets;

112
00:07:44,800 --> 00:07:47,520
if there's a possibility that
the key won't be present,

113
00:07:47,520 --> 00:07:51,840
then call the get method. We'll see how
we can be sure that the key is present

114
00:07:51,840 --> 00:07:54,960
in the next few videos.

115
00:07:55,280 --> 00:07:58,720
Another reason is that sometimes
you want to get an error.

116
00:07:58,720 --> 00:08:02,080
We'll talk more about that when
we look at handling exceptions.

117
00:08:02,080 --> 00:08:05,280
But sometimes it's better
for your code to crash,

118
00:08:05,280 --> 00:08:08,479
rather than for it to
process invalid data.

119
00:08:08,479 --> 00:08:12,319
So experiment by printing out different
values from the vehicle's dictionary,

120
00:08:12,319 --> 00:08:15,919
using both the dot get method
and indexing, and I'll see you in the

121
00:08:15,919 --> 00:08:21,520
next video.

