Welcome back to the beginner's guide to Python in this lesson we're going to
learn about Python dictionaries. Dictionaries are a datatype within
Python that allow you to map keys to values so you can think of dictionaries
as a list of key value pairs. Let's go ahead and create our first dictionary.
Alright so load up the Atom editor with the same project that we were working on
in the last video the beginner's guide to Python. Let's create a new file
within this and let's call it dictionaries.py. Alright so let's
imagine that we wanted to create a dictionary that contained a list of the
apples that we have in stock and the value is the number of apples that we
have in stock so you basically have an inventory of apples and the number of
amount that we have in stock. The way that you create a dictionary is you
write the name of the dictionary so let's call ours apples and then you put
equals and then you open up these curly brackets here. If you just do this you
can create an empty dictionary but if you want to assign values to the
dictionary when you first create it what you do is you type the name of the key
and this can be a string or an integer or a string or a number. We're gonna use
a string in this case because we're going to call it the name of the apples
that we have in stock so let's take one of our apples, the cox
variety of apple and what you do is you create the key that you want and
then you use the colon and then you tell it what value you want to assign to this
key. So we're gonna create a key called Cox and we're going to assign the value of 17
so if you imagine this is the name of the item we have in stock and this is
the number or the quantity of that item that we have available. Okay so you
can basically add as many key value pairs as you like. So you separate the
key value pairs by commas, so if we do , braeburn and
let's just say we have 21 braeburns in stock and then let's add one for pink lady
say we have seven of them and then let's say we have royal gala and we have 12 of
them and then let's say we have Fuji and we have one of them. Okay so this is our
dictionary now in Python there's a... We haven't touched on this so far but
there's a recommended line length of the file so they recommend that your line
that each line should be no longer than 79 characters and that's what this
kind of gray line is here that atom displays for us. So it's recommended for
dictionaries like this that you can actually break them down into individual lines so
you can basically put each of that key value pair on its own line and then you
can... It basically makes the code look a lot cleaner and easier to read. So we're
gonna do that break that down. Alright save the file
Okay so now you can see we have a dictionary with the items that we have
in stock and the number of the quantity of that item that's available. So what
you do with dictionaries is you can access these values in a couple of ways.
So, one way and probably the most common way is if you wanted to output a
particular value from this dictionary then you use the square brackets to
identify which key you want to retrieve the value for. So let's say we
wanted to create a new variable called cox stock, so we wanted to know how
many cox apples we have in stock, we can type cox_stop =
and then the name of our dictionary and then the key that we want to
retrieve from our dictionary. So we want to retrieve the key for Cox then this
will retrieve the value that is currently assigned to Cox at the time of
calling this line. So now we can actually print the value, so we do print we have
Alright let's save that and then let's load up
the command prompt window and let's run our code, so if we type Python dictionaries
and it's saying we cannot convert yes so we basically need to wrap this Cox stock
in a STR and that is because as we touched on in the previous videos you
can't merge a number or an integer value into a string you need to convert
it to a string first using this STR function here. We run that... I've got to
save the file. Okay there you can see it worked
third time is a charm and we have output to the screen here we have 17 Cox apples.
Okay so that's how you retrieve an individual value from a dictionary, let's
imagine we wanted to print out every single value from the dictionary. So for
this we need to iterate over the dictionary and then at each iteration we
need to print the value and their key that we have retrieved. So you can
basically do this by typing apples or typing for apple in apples.keys print
apple. Let's save that. Okay so what this does
is it basically uses a for loop to print each key that is available in the
dictionary. So this apples.keys is a
function that comes with any dictionary you create and you can do this you can
call the keys function to list all of the keys in that dictionary. So we run
this again you can see that it outputs each and every key that we have in the
dictionary. So that's one way to iterate over the dictionary. Another way is you
can actually iterate with the keys and the values in the same line. So what we
can do is we can type for key or let's call this actually for apple stock count in
apples.items print we have
stop or let's convert it to a string STR stock
count and then the name of the apple
Alright I'll save that. Just expand this so you can see the full line. Okay so
what this is going to do is it's going to iterate over all of the items in our
dictionary and it's going to pass into the for loop the name of the key first
so it always starts with the key which is the apple type and then the value
which is the amount we have in stock. So if we were to run this now you can see that
it loops through and it outputs each and every each and every apple that we have
and the number that we have in stock so that's how you loop through the
dictionaries. Another useful function in the dictionary is the get function.
So, the get function works just like this this square bracket notation here where
it retrieves a value in the dictionary. The difference is if the value doesn't
exist you can assign a default value, so let's say we want to have we want to
know how many McIntosh apples we have. We can use we can use the dictionary and type
the dictionary name apples.get Macintosh and we'll set the default
value to zero so what this does is it basically provides a safe way to
retrieve items from the dictionary in case they don't exist. So in our case
there are no Macintosh apples in there so we have zero in stock so save that
and let's just print mac_apples to see what the value is. You can see the last
thing that it output was this print statement and it says zero because
basically there is no Macintosh apples in stock if we were to change that to
one then it would output that value. So this is basically a useful function for
retrieving values because it allows you to retrieve values without creating any
errors because if the values don't exist you can just basically set a default.
Okay so that's how you use Python dictionaries. Thank you so much for
watching I hope you found this video useful and if you have any questions at all
please leave them in the comments below and don't forget to subscribe for
notifications for future videos that we publish on this channel. Thanks for
watching and see you in the next video.