All right.
So the first thing I want to talk about is Dictionaries in Python.
Now dictionaries in Python work kind of similarly to dictionaries in real life.
Right?
So if you were to look up a word in the dictionary, say the word code, then you might find the definition
as something along the lines of, "program instructions for the computer."
And dictionaries are really useful because they allow us to group together and tag related pieces of
information.
The way I like to think about dictionaries is in the form of a table.
Every dictionary has two parts to it.
On the left-hand side is the key, and that is the equivalent of the word in the dictionary,
and then it's also got an associated value. That would be the equivalent of the actual definition of
the word.
Now let's say that we took this very simple table of definitions of programming words that we've come
across so far,
and we go ahead and we try to convert it into a dictionary.
How would we do that?
Let me firstly drop the last two rows and let's just start with the first one.
The first thing we want to do is we want to create a dictionary and to do that in Python, this is what
the syntax looks like. ({Key: Value})
We have a set of curly braces.
And everything that's inside the curly brace is the content of our dictionary.
The key goes first, followed by a colon and then followed by the value.
In our table we've got this word, "Bug" which is the first key.
So we can replace that over here in our dictionary.
And the value that's associated with this key is the definition for a bug.
So, "An error in a program that prevents the program from running as expected." That becomes the value and
can be replaced here after the colon.
So now we've created an actual dictionary using Python code.
What if you wanted to have more than one entry in your dictionary?
Well, you would separate each of the key value pairs using a comma, and then you can continue adding
key and value pairs until you get to the end of your dictionary.
All right.
Let's take a look at dictionaries in action.
If you head over to the Day 9 start code file, then you can see that in here I've already added
the dictionary that we were defining earlier on, and I've stored it inside a variable called,
programming_dictionary.
Now currently I've only got two entries in this dictionary, the definition for bug and the definition
for function.
Now the first thing I want to highlight is when you create a dictionary that has more than one element,
such as in this case, then you want to take care to format it properly so that it's more easily readable.
So what you'll see Python programmers do by convention is they will start off the dictionary with the
open curly brace at the top, and then every subsequent entry is indented by one indent.
And then at the very end of that entry there's a comma.
And then we hit enter so that the next item goes on to the next line.
And finally, the last curly brace should go at the very beginning in line with the start of the dictionary.
And another thing that's quite nice to do is to cap off all entries in your dictionary or list with
a comma.
This means that if you needed to add more items into the dictionary, you can simply just hit Enter
and continue typing the next thing.
And if we wanted to add another entry, it's as simple as adding in the key, a colon, and then the value.
And to cap it off again with a comma.
So now our dictionary represents exactly the same data as we saw in our table over here with a bunch
of key/value pairs and a total of three entries.
Now, the next thing I want to do is what if I wanted to retrieve an item from the dictionary?
Because we know that if we had a list, what we would do is we would use a set of square brackets,
and then we would give the index of the item that we wanted.
So the item at index 0 or 1 or 2 and so on and so forth.
Now for dictionaries it's kind of similar in terms of syntax, but the only difference is that dictionaries
have elements which are identified by their key.
If we wanted this piece of information, for example, then all we have to do is tap into the dictionary
and then add a set of square brackets,
and inside the square brackets, we're going to provide the key.
So here the key is a string and it's the string, "Bug".
So let's go ahead and put Bug in here.
And now if I go ahead and print this then you can see that it's going to give me the value, which is
"An error in a program that prevents the program from running as expected."
Now it's really, really important that you make sure that when you're fetching something out of a dictionary
by its key, that you actually spell the key correctly.
A really, really common error is when you're trying to retrieve something out of a dictionary and you've
just made a very simple typo.
So instead of u, I'm typing o here, and you'll see we get an error.
And the error tells us that it's a KeyError referring to this particular key,
and it highlights this Line 7, where we're trying to retrieve something out of this dictionary
by this key.
Basically, it's telling you that this key doesn't actually exist and it can't be found, so it doesn't
know what it is that you want.
Remember how when we had lists and when we tried to retrieve something that was not inside the list?
So, for example, this particular list at index[4] doesn't actually exist because this is 0,
1, 2, 3, and 4 is actually not a piece of data inside this list.
Similarly with dictionaries, if we try to put in a key that doesn't exist, then we get this KeyError.
Now another common pitfall that students fall down into is they don't actually use the correct data
type.
So for example, if when we defined this dictionary without putting a string around each of these keys,
then it's going to error out and it won't even let us run.
It's going to tell us undefined name bug, because it thinks that this is a variable that you've declared
somewhere, but it's not, in fact, what you wanted are these strings for keys.
And so when you have a key that is a string, when you're trying to retrieve the data from that key,
you also have to make sure that you provide the key in its actual data type.
So for example, if this was just a number say, 1, 2, 3, then of course all you have to write
in here is just 1, 2, 3.
And it would know that this piece of data is what you wanted.
So that's how you retrieve items from a dictionary, by adding a square bracket and then giving it the
key,
and it will look for the key inside the dictionary and give you back the value.
Now what if you wanted to add a piece of data such as that loop that we had earlier on, but you want
to do it programmatically.
So instead of doing it when you were defining the dictionary at the beginning, what if at some later
stage in your program, you needed to add a new entry?
Well, to do this, it's also really simple.
All you have to do is to tap into the dictionary, which is called programming_dictionary in our case,
and again using square brackets we define the key.
The key I'm going to add is "Loop",
and then after an equal sign I get to assign the value.
So in my case the value is going to be the definition for a loop.
And now when this line of code is executed and we go ahead and just print the programming_dictionary
after this has happened, then we'll see that it's actually different from what we had before,
so let's hit Run.
You can see that previously we had a programming_dictionary that had only two items, Bug and Function,
and after this line where we print our programming_dictionary again, you can see it's now got three
items Bug, Function and Loop.
Now very often when you're writing code, it can be really helpful to start out with an empty dictionary.
Just as you saw previously, you can create an empty list by simply having a set of square brackets
with nothing inside.
You can also create an empty dictionary by simply creating a set of curly braces with nothing inside,
and then add a later stage you can add to your dictionary by using this method that you saw here.
Now on the other hand, you might actually want to wipe an entire dictionary.
Here I'm creating a new empty dictionary by creating this pair of curly braces with nothing inside,
but I can also wipe an existing dictionary by simply doing the same thing.
We know that this dictionary, programming_dictionary has three items in it, but on Line 17 I'm going
to say programming_dictionary equals empty dictionary.
And now if I move this print statement down here, then you can see that when it prints out, it's actually
going to be completely empty.
That can be really useful if you wanted to clear out a user's progress,
or for example, if a game restarts, then all the scores and stats will probably have to be wiped empty.
So this is one way that you could do that.
Now this method of tapping into a dictionary, using the key to fetch the relevant item from it, and
then doing something with it, goes beyond just adding.
You can also use this to edit an item in a dictionary.
For example, let me go ahead and comment out these two lines of code so we don't wipe our programming
dictionary.
And instead I'm going to tap into the programming_dictionary, and I'm going to fetch the item that
has a key of Bug.
And I'm going to redefine its value.
So currently it should be, "An error in a program that prevents the program from running as expected."
And I can prove that if I just wrap this around a print statement. If instead I wanted this to be a
different value, then I can simply use the same syntax as I did for adding new items,
but in this case, I'm actually editing this entry because it's going to look through the dictionary,
find a value with this key, and then assign it to whatever I put on the right hand side of the equals
sign.
Now on the other hand, if it finds nothing with that key then it's going to create a new entry with
the value again on the right-hand side of the equal sign.
Now let's say that a bug is instead, A moth in your computer."
Now if I go ahead and print my programming_dictionary once more, then you can see that the definition
for our bug has now just been changed.
The final thing with regards to dictionaries that I think is really, really useful is how you loop
through a dictionary and using your knowledge from lists,
you might think that to loop through a dictionary you would,
let's say we're using a for loop. And we say for thing in programming_dictionary,
let's go ahead and print this thing each time.
Here's a good moment to play computer and think what do you expect to be printed?
So let's comment out all the other print statements so we don't get confused.
And when I click run, what do you think will be printed?
All right.
Is that what you expected?
Because it certainly wasn't what I expected
when I first learned Python. I thought it would give me each of the items in the dictionary with this
key and its value, but instead this code actually just gives you the keys.
Now, of course, once you do have access to the key instead of thing, I should actually really say,
'for key in programming_dictionary', if I wanted to print the key, then, of course, I could just write
print key.
But if I wanted to get hold of the value, I could equally just as easily tap into my dictionary, use
the square bracket, and pass in that key.
So now when I hit Run, you can see it's giving me first the key from this line and then secondly the
value based on this line.
I'm using that retrieval code in order to get the value.
Of course, remember that you can get access to all the code that I'm writing here, and if you want
to have a quick review of everything that we've gone through so far, because a lot of this knowledge
is going to be tested in the next lesson, where we do a coding exercise that will really solidify everything
you've learned in this lesson so far.
So for all of that and more, I'll see you on the next lesson.