Instructor: Welcome back.
Now if you remember, let's have something different here
so we have unique keys.
If you remember, we have to have unique keys.
So let's say this is 'greet' 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 1, 2, 3.
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 wanna know if this, let's call this user,
if this user has maybe property, like 'age'.
So do they have this 'age' key?
Well, if I click run, mm mm, I get an error,
KeyError; '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 wanna 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 .get.
And guess what .get is?
Did you guess a method?
Well, there you go.
.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 error.
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 wanna 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 55."
So now we have 55.
But if the user already has 'age'
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 gonna show you another way
to create a dictionary which is not very common.
Most of the time you wanna use this syntax.
But just so you're aware of it, here's another way.
Let's say we have a new user, so user2.
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 'JohnJohn'.
If I now print out user2, well, before I click run,
I see that I have a red underline.
And by the way, if you see here pyflakes,
that's something called linting, something we'll talk about
when we talk about developer environments.
We're using this linter that tells you
whenever you make errors in Python,
and it says keywords can't 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': 'JohnJohn'.
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.