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 keys.
So let's say this is Grete and this is a I don't know, let's say a basket and we can access basket
like this if I click run.
Again.
One, two, three.
But what if we don't know what this dictionary is?
Maybe a dictionary is something.
Well, 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.
Mm hmm.
I get an error.
Key error.
H 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 dot
get is.
Did you guess a method?
Well, there you go.
Dot get 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 agent, 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 H from the user dictionary.
If H doesn't exist on the user dictionary, use 55.
So now we have 55.
But if the user already has h.
And I click Run.
I get 20.
So this is.
Hey, in case age doesn't exist, add 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 this syntax, but just so you're aware of it, here's another way.
Let's say we have a new user.
So a 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 variable 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.
John.
If I now print out user two well before I click run, I see that I have a red underline.
And by the way, if you see here, pie flakes, that's something called lynching, something we'll talk
about.
When we talk about developer environments, we're using this lender that tells you whenever you make
errors in Python and it says keywords can be an expression.
That just means that, hey, the keyword needs to be, well, just a variable that we create.
So if I click Run here.
Look at that.
I have name 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.