In this video we will cover dictionaries in Python. In the previous video when we created a list we used square brackets and comma separated values. To create a dictionary we will use curly brackets instead of the square brackets. In a previous video I may have mentioned that we do not use curly brackets in Python, and that was a lie. What I meant is that we don't need to wrap code blocks in curly brackets like PHP or JavaScript. So, let's go ahead and define a dictionary.
{"name": "Nick", "age": is 27, "hobby": "code"}
As you can see, instead of just comma separated values we used in the list, the dictionary uses a key and a value separated by a colon. You can access an item in the dictionary like so:
{"name": "Nick", "age": is 27, "hobby": "code"}{"name"}
Python will then print out "Nick." Dictionaries will be very important when we get to the JSON part of the course, because we will need to parse though a ton of data.
So let's get into dictionaries and that's gonna end the subseries I guess
of types, this is going to be the last type that we're going to talk about. So
let's go ahead and open up a terminal here and jump into Python. Now the
previous video when we created a list we used square brackets and we entered
comma-separated values in there which became our list,
however in order to create a dictionary we're going to open up sand close curly
brackets here. Now in a previous video I told you guys that we don't use curly
brackets in Python and that was a lie, and just so you know there's going to be
a few of those throughout this course, so keep your eyes open for them. What I meant to
say, and just to clarify, you don't wrap code blocks in curly brackets like you do in
PHP and JavaScript. Instead the only use that Python has for curly brackets is to
define a dictionary. So, rather than entering comma-separated values we're
going to enter comma for separated values but each one of those is going to
be a key and a value, so let's go ahead and create one. Let's create a person so the name is
going to be {"name": "Nick", "age": is 27, "hobby": "code"} and that
becomes our dictionary. Now, again, you can...you can access, I had tried to do that
the JavaScript way here which you put a dot and then the name of the key that
you're looking for,
in Python you do it like this kind of like when you define an index number
here. If you were to do this it wouldn't be able to find it because it's not a
list but with an array we would have done that, in here we just type
the name of the value we want. So name is going to print out "Nick" if we put in age
that's going to print out "27" and if we enter hobby that's going to print out
"code" and so that's how to define a dictionary and how to retrieve certain
values from the dictionary.
This is also going to become a very useful later on once we get to, well once
we get JSON itself because we are going to do a video on JSON, but also once we
start once we start working with a lot of data some of it's, some
of it we're going to explicitly convert into JSON data just so we can parse it
like this because this is one of the easiest ways to parse data is using
this form. So thanks for watching this video and I hope you guys enjoyed it.
This ends the sub section of types and in the next video we're going to be
learning about variables.