 Hello again, everyone. This is Brian. And in this video, we're going to cover the dictionary in Python three. So what is a dictionary? It's a key value pair. More appropriately, it is a list that's indexed by keys. If you want to be really specific, you notice the brackets, this is actually a set indexed by keys, which can be any immutable type. What does that mean any immutable type means unchangeable. So let's take a look and let's see what it takes to create and work with a dictionary. Let's take a look and see what it really takes to create a dictionary. We're going to do this two different ways. We're going to do it the hard way and the easy way. First, let's do it the hard way. I'm gonna say the equals, we've got our brackets, we need a key value pair. So let's create the key. He is going to be the string pet colon. And now we need a value. Let's say dog. That's it. That's really all there is to it. Now if we want to make another one, we just separate it by a comma. Now we need a key. Let's call this age. Value is going to be five. Let's go ahead and make another one. Let's call it name. And its value is going to be spot. I'm always bad at picking pet names. Good print out the dictionary in all of its glory. There it is. Pat to dog age five name spot. Now let's do it the easy way. I'm going to say equals. And we're going to call the dict function. Funny names say that real loud in class and watch everybody just kind of look at you. But we're going to say pet equals dog age equals five and name those spot. So what's really going on here is it's doing all this formatting for you. That's why I call it the easy way. The end result, however, is exactly the same. They are exactly the same. It doesn't matter which way you create it. Just under the hood know that you have to have a key value pair. For example, pet dog age five name spot. Let's take a look at the keys and values and how we get at them. There's a third type really and it's called items. We're going to work with that one first. So I'm going to say print and the go f items. And we're going to call the dictionary items function save run. And this is going to return a dict underscore items, which you guessed it. Look at this thing. This is insane. So we have a tuple containing a list containing tuples. But what it's really done is it's packaged it into individual items. So you can very distinctly see the key value pairs. Not abundantly useful for us. What we really worry about are well the keys and the items. So let's go ahead and look at those real quick. I'm going to just say keys and values. I say items earlier I did. Alright, so we want to look at the keys and the values save and run and voila. So we're returning three different things, dict underscore items, dict underscore keys, dict underscore values. But what we've got is all of the information in this dictionary right here in these three functions. See items returns pretty much everything nice and packaged the keys is going to tell us what keys are available because remember, everything's a key value pair. So when we go to look something up, we can't do it by index, we have to do it by key, which is why these keys exist. You could easily make these keys integers or numbers of some kind. But we've done it as a string. We can also get the values dog five and spot, which are abundantly unuseful without the keys. So in the next section here, we're going to take a look at how you would actually get those. Let's go ahead and take a look at how we would get a value from a key because these are key value pairs. It's called the key because it unlocks the door to the value. And let's take a look at what I mean here. So I want to format this. And I'm going to say name those capitalize that. And we're going to say D and you inclined to do something like this D zero. Let's run this in the error. What does that mean key error? It means the key was simply not found. We don't have a key named to zero. We do, however, have a key name name, we also have one called pet and age. So you have to use the key to get the value. Let's demonstrate that the name of spot. Now, you notice how I have different quotes, I have single quote and double quote. There is a very interesting little issue here, where if we do a single quote within a single quote, bad things happen very quickly, we get an invalid syntax. You're looking at this going now wait a minute, it should treat everything in these brackets as a separate entity, but it really doesn't. So you do have a quote issue that we've talked about in a previous video. All right, so now that we've wrapped our head around that, let's take a look at the key error in depth. And let's call this test. And let's just say blah. Some key we know doesn't exist. Really, what's going on here is it's going out and it's trying to find any key of that value. If it doesn't find it, it throws a key error. At the very end of this, we're going to show you a little trick using some simple logic, which we haven't covered yet to get around that to determine if the key even exists in the first place. But just right now, know that you have to have the key, otherwise it will throw an error. So just put a quick note there will throw an error if the key is not found. Let's go ahead and take a look at how hard is it to add an item to a dictionary. Well, it's not it's ridiculously simple. You just simply do something like this. There you go. It's that simple, you just simply say dictionary and then some key that's not in use equals a value and it will add it automatically. It's very cool how it does that. Now you should note and we're just going to copy and paste this. What happens if the key already exists. So here we're adding in here, what's going to happen, let's run and find out. It's just simply changed it. This is what I mean by the key is immutable, we cannot change this key. We could delete it and read it. But once that key's in there, all we can do is update the value or delete it. The key itself cannot change. And let's take a look at what it takes to remove an item. Our good old buddy, the delete statement. So we're going to delete. You guessed it, the trick. Now, this is what I mean by this is a key value pair. When we run this, and we delete a key, the value associated with that key is also removed. You see right here, trick, roll over, we've deleted it. And now it's just simply gone. It's not in there. Very simple to do that. But just understand that once you delete the key, the value is gone. However, if you were to try to delete the value, the key will be there, but you'll have a null value. And that could lead to some issues down the road. That's a more complex topic we're going to cover in a future video. I just want you to be aware of that. I've been getting a lot of really good feedback about this video series. And some people have said, hey, dude, you're going a little too slow. I realize these are for beginners, but I want more now. So we're going to give you a little preview of what to come. So we're going to test for existence. And we're going to do something called a loop. And both of these may be a little challenging, especially for newbie programmers. But if you're an experienced programmer and you just want to learn it now, let's just go over it and get it out of the way. So we're going to say if and this is basic logic. Let's say name in the colon and hit enter. What we've done here is an if statement. This is a logic. If this key is in the dictionary, do something. Now, Python has this little guy right here, this colon at the end saying, hey, it's not the end of the statement expect more. And then you have to have it's either spaces or a tab. It doesn't really matter, but it has to be the same amount. If you're from another language, you're expecting something like this. And you may be going, why do you need tabs? Well, if you think about it, your code is here. And if you get rid of the tabs, it looks like this. So it just makes sense. But a lot of people when they're first starting Python can't wrap their head around the tabs or the white spaces and it just infuriates them. So this is what I was saying earlier about we're going to test to see if that key exists before we try to access it. Now if the key was not in the dictionary, it would throw a key error, but we're saying if so only if this exists in the dictionary, are we going to run this code, we're going to go over all of this in more detail in a future video. But I've had a lot of people saying, Hey, they want more now. So there's one. Now let's go over loops. This is the other thing that really trips people up, especially in newbie land is because they're new. They don't understand this stuff. Nobody's explained it to them. What is a loop? Well, have you ever like gotten in trouble as a child and like your mom or dad says, you know, go do this until I tell you to stop? That's basically what you're telling the computer to do. You're gonna say for a key in the keys. And this may look really confusing if you're a complete newbie. Again, gotta have that white space. We're going to go ahead and print. And let's go ahead and format this. And I am going to explain this, but at the moment, just take a huge leap of faith with me here. We got our key. And then we're going to say dictionary and then the specific key. This looks ugly if you're a newbie, but let's run it. It works. So it's saying pet dog age five name spot. That's what we're doing right there. So what's going on here is we're saying for every key in, there's that in keyword again, the dictionary keys. Remember, we talked about this way up here, for every key in the dictionary keys, go ahead and print out the key and the dictionary value that correlates with that key. Again, you have to have that tab there. We're going to cover looping also in a future video, but this is just because people have been saying we want more and we want more now. So if you're having trouble wrapping your head around these two right here, don't worry, I'm going to make a future video that's going to cover those in depth. And we're going to talk about them. And I do mean in depth. But if you are a season programmer, that's very simply how you do it. If you're having trouble wrapping your head around these tabs, just think of it like this, you have some code. And then normally, you would do something like this. And Python's trying to help me out here. But you would have something like that. Strip this out, replace it with that. And of course, you have something here. Sad code. I know, let's say sad code here. But then you strip these out, and it becomes that. And that's why Python is so much smaller than other languages compared to something like Java JavaScript, because it takes all those special characters out of there. I hope you enjoyed this video, you can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the VoidRome's Facebook group. This is a large group with lots of developers. And we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there, drop me a note, I'm either working on it, or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.