Welcome back.
Now, if you remember, let's have something different here, so we have unique keys.
If you remember, we have to have a unique case.
So let's say this is great and this is a I don't know, let's say a basket and we can access basket
like this if I click run.
I get one, two, three, but what if we don't know what this dictionary is, maybe a dictionary is
something somewhere else where we can't see, but we want to know if this let's call this user.
If this user has maybe property like H.
So do they have this age key?
Well, if I click run.
May I get an air key air age age doesn't exist, but remember errors in our programs and this is something
we'll cover later on are not good.
They're going to stop the program as soon as the Python interpreters enters an error.
It's going to kick out of the program and say, hey, you have an error and it's going to completely
ignore what's underneath it.
Here, we don't have anything.
But in a real life program, we want to avoid these errors because it stops execution.
So another good way to access a key and to see if it even exists is to use dot get and guess what it
is.
Did you guess a method?
Well, there you go, doGet is a method on the object or the dictionary in Python.
So here if we do get age.
And do run.
I get none so my program doesn't air.
It just says, hey, there is nothing there, it's an absence of value and we've learned what none means
by now.
So that's a really nice way to avoid those errors.
By the way, if let's say there's no age in the user but you want to have a default value, you can
do something like this.
So you add a comma and then here you say whatever you want as the default value.
So let's figure that out.
If I click, run.
I get 55 because now I'm saying, hey, grab the age from the user dictionary, if age doesn't exist
on the user dictionary, use fifty five.
So now we have fifty five.
But if the user already has age.
And I click run.
I get 20, so this is, hey, in case age doesn't exist at a default value, that's really, really
nice.
Now, I'm going to show you another way to create a dictionary, which is not very common.
Most of the time you want to use the syntax.
But just so you're aware of it, here's another way.
Let's say we have a new user.
So user two, I can create here a dictionary by saying dict, which again is a built in function.
And in here, look at that.
It shows me, hey, this is going to create a new empty dictionary and we can create different values,
key value pairs.
And we simply say, I want the key.
To equal the value, so in my case, let's just say that I want the name.
To equal.
If I now print out user too well before I click run, I see that I have a red underline.
And by the way, if you see here by flakes, that's something called LINTANG, something we'll talk
about when we talk about developer environments.
We're using this linta that tells you whenever you make errors in Python and it says key words can be
an expression.
That just means that, hey, the key word needs to be, well, just a variable that we create.
So if I click run here.
Look at that, I have named John, John.
So we've created a user with this key value pair now, like I said, this way of creating dictionaries
is not very common.
Most of the time you'll do something like this.
But at least now you know that this is why this built in function exists.
I'll see you in the next one.
Bye bye.