Dictionaries in Python are incredibly important,
and in other languages a dictionary is often called an object.
So like in JavaScript or JSON, a dictionary is an object.
And if you're familiar with those already, you'll be familiar
with how it works in Python because they are very, very similar.
But for the most part, you can really think of a dictionary
like a variable with many variables inside of it.
And so instead of saying 'kalob_name = "Kalob"', and
then 'kalob_age = 30', and 'kalob_cats =
2', and 'kalob_fav_food = "Taco"',
instead of writing all of that, we can use this thing called
the dictionary, and put this all inside of one variable called
'kalob'. Now, of course, this doesn't have to revolve around
me, it's just easy for me to make that example, but it could
be around literally anything.
And at this point in time, we're starting to actually sort
of clump data together.
So all of this has to do with me.
So before we get started, let's go ahead and take a look at some syntax.
So I got a variable here, and then I have this thing called a
'key', and this other thing called a 'value';
and I could have another 'key' here, ':', and then another 'value' here.
And so this 'key' should always be a string, ideally something
that's easily accessible.
I like to stick with the idea that your 'key' should always
be named like your variables.
So if we run this, and we check out 'person', we have 'key': 'value',
'key2': 'value2', and 'type(person)' is going to be a 'dict',
which is short for the word dictionary.
Now, the nice thing about this, is if we wanted to get just
a particular 'key', we could simply print out 'person',
and then just like how we use indexes with a list or a tuple
or a string, this would be our new index.
But instead of using a number, we use the name.
So let's go ahead and 'print
[no audio]
(person)', and then just like an index,
but instead of a number, we use a string,
and a string always has those quotations or apostrophes around it.
Now, that doesn't exist, because obviously there's no
'name' in here, or no 'key' called 'name'.
But if we change this to 'key', we're going to see 'value'; 'key2' : 'value2'.
So let's go ahead and work with a real example.
So let's say there's a 'course', and this 'course' has a 'name',
and this course is "Python for Everybody".
Let's say this 'course' also has 'students'.
How many students are there? I don't know,
a hundred thousand students, 100_000, something like that.
And in this 'course', how many 'lessons' are there,
or let's not use an int,
let's use a float.
How long is this course?
I actually have no idea at this point in time.
So let's put 'length', and the 'length' of this 'course' is going
to be I don't know, 8.75, we'll assume that's hours.
Now, if we wanted to, we could actually get really, really
interesting with this. We could say 'lessons' is now a list, and we could list of
all the lesson names.
So we could have 'Strings', 'Lists', 'Dictionaries', 'If Statements',
we haven't got there yet, but we will, all sorts of stuff.
We could have all that kind of data in there as well.
And so really, any sort of data type can be your 'value', and
your 'key' should always just be something that's easy to relate
to, something that's easy to remember, like a variable name.
So let's go ahead and run this.
We'll see that 'course' is a bunch of information in here.
And let's run just for fun
'type(course)'.
It's a dictionary, 'dict' as expected.
But what if we wanted the type of 'lessons'? We could do
'type(course['lessons'])', and remember, instead of using 0,
like we would do for a string, or a list, or a tuple, we
simply put in the string,
and this 'lessons' here matches this one here.
And this one when I hit 'Shift + Enter' is going to say it is a 'list'
just like that.
Now, why is this important?
Well, because now we have all this information grouped together
in one variable.
So this no longer has to look like this.
So it doesn't say 'course_name = "Python for Everybory"',
'course_students = 100_000',
'course_length = 8.75',
'course_lessons = ['Strings',
'Lists', 'Dictionaries', 'If Statements']'.
We just have one variable to rule them all, which is really,
really, really nice.
And once again, just as an example, if you wanted to get
any value in there, you could, with 'course['name']'. This is your
variable name, and this is the 'key',
and that 'key' is one of these - 'name', 'students', 'length', or 'lessons'.
Sure enough, there it is.
But what if we wanted to add something more to this?
We know with the list, we can use 'list.append', but that's
not going to work here because it's not a list.
We want to instead say 'course', we just simply give it
a new 'key' right off the bat.
Let's give this a 'subject', and this is going to be 'Programming',
hit 'Enter', and let's take a look at what it does.
We have - 'name', 'student', 'length', 'lessons', and now we have the
'subject'. It's a string of 'Programming'.
And if we wanted to delete anything, this is actually pretty
interesting as well, we could simply type the keyword 'd-e-l'.
You can see that it's syntax highlighted here as 'del'.
We give it 'course'.
Let's get rid of 'length', but let's make sure we spell 'length' correctly.
And then 'course'.
And before we had 'length': 8.75, we deleted 'course['length'],
and now it's not in here at all.
Now, let's say you just wanted to get all of the keys in
here, we could put a variable in here called 'keys', and do
'course.keys'.
This is a method on the 'course' object.
And let's take a look at what 'keys' are,
and it's a dictionary of 'keys'.
But if you look inside of this, that looks a lot like a list, doesn't it?
And what if you wanted all of the 'values'? You can do 'values
= course.',
and let's just hit tab, and see what we have here.
'clear', 'copy', 'fromkeys', 'get',
actually we'll talk about get in a second,
I'm glad that one came up, 'pop', but let's go ahead and actually just get 'values'.
[no audio]
And let's see what 'values' are.
And the 'values' again is a dictionary of 'values',
but look at the innings of this, the inside.
We have one item here, "Python for Everybody", 100000,
the next one is a list inside of a list,
and then our last item here is a string called 'Programming'.
Now, just for fun, let's do a little more real life example
here. So let's say I'm going to ask the user if they want
information from this 'course'.
They need to type in the 'name', 'students', 'lessons', or 'subject'.
So let's say 'what_info_do_you_want =',
and that's a terrible variable name, very verbose, a little
long though, and say, "Enter name students
lessons or subject".
And then that's going to be stored in this variable.
And what we can do now is say, the data or the 'value',
let's call it 'data = course.get()',
and then whatever this is,
[no audio]
and this is either going to be
'name', 'students', 'lessons', or 'subject'.
And then let's print that out.
'print(data)'.
"Enter name students lessons or subject"
Let's type in 'students', gives us a 100000.
Let's rerun this.
Let's do 'subject'.
'Programming'.
So all this is doing, is saying, "Hey, this variable is essentially
subject". That is actually exactly what it is.
It's a string as well.
So 'data = course.get("subject")' is going to go, "Okay. Well,
you've got a dictionary here called 'course'. Get
the key called 'subject', and whatever that value is, put it
into your variable.
So now we're getting super pythonic here.
We're getting really into the nitty gritty of a programming
language. Now let's see what happens when that key does not
exist. When you have a malicious user who simply does not
follow the rules, which is like 90% of the internet.
So when someone says, "Enter name students lessons are subject",
and I go "cats tacos".
[no audio]
Oh, look at that.
It came up with 'None',
and that's because when you use 'get', and it doesn't find the
key that you're looking for, it's default,
also its second parameter is 'None'.
We could also set the default parameter here to be, 'THIS WASN'T
FOUUUNNDDDD', and let's rerun the cell, "ANYTHING THAT ISN'T A REAL
KEY". So this is going to basically error quote unquote, but
it's actually going to say, "Hey, this key does not exist
as 'name', 'students', 'lessons' or 'subject',
and it's going to return,
"THIS WASN'T FOUUUNNDDDD".
So let's hit 'Enter'.
This is what I entered, and this is what came back.
Now, a little bit later, we're going to look at looping over
a dictionary, I think we might as well bring it up
now. We don't really know about loops, but you know what,
I like to pre-expose my students to some more advanced stuff,
and then once we get to it in a few lessons, you'll be like,
"Oh, this is somewhat familiar".
So we can actually do this quite easily in a for loop.
'for item,', no let's not do 'item', 'key, value in course.items()',
and what this is going to do, is basically turn this into a list.
So we've got a 'name' and a 'value', or a 'key' and a 'value'. 'key':
'value' - 'name': 'Python for Everybody';
'key': 'value' - 'students': 100000; 'key': 'value' - 'lessons': list of lessons,
and we can print 'key' with a fat arrow, '=>',
just so we know that this is a little bit custom here,
'value'. Let's run that. 'name', 'students' is 100,000, 'lessons' is
a list, and 'subject' is 'Programming'.
So for this last part, this particular one here, you do not
need to know this, but I thought it would be fun to sort
of show you how we can do it, because that's going to be
a question that a lot of people are asking.
I know when I was learning Python, that was one of my questions
about dictionaries as well.
So as a quick summary, dictionaries prevent this.
So instead of having four variables, you have one variable.
It's still four lines of code or more,
but instead of 'kalob_name', 'kalob_age',
'kalob_cats', 'kalob_fav_food', you
could just have one variable called 'kalob'.
[no audio]
The type of a dictionary is 'dict'.
If you want to print out a particular value, you just have
to print out the dictionary name,
so that variable name, and then the 'key', and this one matches
right here.
'value2' matches down here, 'value2'.
This is a good example of a bigger dictionary, which has
a string, an int, a float, and a list.
We said that was still a dictionary. But if we wanted to
get just the 'lessons', well that was originally a list, and
when we run 'type' on it, it's a 'list',
so we know that we can have dictionaries with different data
types inside of it.
We added a new item to our dictionary by simply saying the
variable name, and then the key name is equal to the value,
and that simply adds it in here for us.
And that did it right there.
We deleted an item out of our dictionary with the 'del' keyword,
and that got rid of 'length' for us.
8.75 no longer shows up
if you look down here.
[no audio]
You can get a list of all the keys
by running your dictionary, '.keys'. You can get a list of all
the values by running your dictionary '.values'.
And you can get a specific item out of your dictionary by
running the '.get' method with,
let's say we wanted to get the 'name', but if for whatever
reason, this was deleted or this was never set, it can return
a default, and that default is always 'None'.
And last but not least, we did some weird thing that we probably
don't fully understand yet, but it's still kind of cool.
We looped through every key and value pair together in a
for loop and printed them out.
Now, you don't need to remember all of this right now.
Again, this comes with a lot more experience, and that experience
is built over time.
So don't feel like you need to know this before continuing
on. But what I would like you to do is create your own dictionary,
add a new item to it, delete an old item to it, and then
just print out your slightly changed dictionary.
Once you've done that, let's head on over to that
next lesson, we'll learn about another data type.