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 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 get 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 H key.
Well if I click Run
I get an error key error.
H H 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 does
get is did you guess a method.
Well there you go.
Dog 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 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 forget that.
If I click Run I get 55.
Because now I'm saying Hey grab the h 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 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 in 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 User 2.
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
if I now print out user to well before I click Run I see that I have a red underline and by the way
if you see here pi flakes.
That's something called linking something we'll talk about when we talk about developer environments.
We're using this Lintner 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 create 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 in the next one.
By.