Now, let us briefly talk about dictionaries.
So dictionaries are not that important for us in this course, but still they are an important part
of Python.
So I want to discuss it just a bit.
So a dictionary in reality is, of course, something where you can look up a word, for example, and
here it's a bit similar to dictionaries are related to lists and arrays, but they behave a bit differently.
So let me just show you I will define just some dictionary and call it dictionary one.
And you start and end the dictionary with these curly brackets and to just make it look a bit better.
Let me write it down like this.
So the syntax here is that you have some key words.
For example, a property and I want to make this dictionary about cars.
So the first dictionary should be about some mercy car.
So the first property, the first keyword that I add here is brand.
So the brand property will have to value could be a number or could be a string.
And here it's string Mercedes, and then we can add other keywords and other properties.
For example, the model and here the value is again, a string.
And let's just take some car off Mercedes, and we can, of course, add other properties like color
or year.
The year is, for example, this one.
So now we have a dictionary explaining or containing properties of our car.
So if I run this, it is defined and now I can just call again the variable or the dictionary by writing
one.
And you see, this is what the dictionary now looks like.
Sort of.
Can we do with this?
We can select certain entries.
So the first idea may be to just call a property or, yeah, some feature of the dictionary by using
it in the same fashion as a list or an array.
So maybe you would write something like this.
This would, you may think, would call this property here.
However, it gives us an error because it tells us there's a key error, which basically means the key
of one does not exist here.
So this is not working.
So for working
and what we have to do instead, or is the correct way of calling a property of a dictionary is to not
add a number here, but to add the key.
For example, we could write model, and now it gives us the value corresponding to the key called model.
So you see, this is the main difference.
You have not anymore a list which is numbered.
So the order of these features here doesn't matter at all.
You have instead, instead of writing these indices, you have to write the key words here.
So this can be really useful if you don't want to remember the order in which you have added all of
these properties and you want to just search for a certain value.
So it can be quite useful to use these dictionaries, for example, when you try to build a database
or something.
So what else can we do?
We can, for example, write Dictionary one dot keys.
And this would give us all of the keys.
So all of these words here, and likewise, we can also write a dictionary one dot values, which would
give us then only two values.
And you see, it is an array or, yeah, this type or in this case, it's yeah, a collection of strings
and also of numbers.
And then last thing that I want to show you is how can you manipulate entries in the dictionary?
And this is quite interesting because let me show you we can write a dictionary and then we do not drive
the index, but we write the key.
For example, a year and now I can write the year property should be 1996, which is different to the
1995 that we have defined earlier.
And let me at the same time, also change another thing.
Dictionary one.
And this time I will use a key which I have not defined so far.
Country and country is Germany.
And when I run both of these commands, let us see what the dictionary now looks like.
You see brand and model still the same.
The here has been updated from 95 to 96 and the country.
Property has been added completely, so it means you can use the same syntax for changing the values
corresponding to some key steps already exist and you can add new values and new keys if the keys do
not exist.
So as I might, as I said, dictionaries can be quite useful, but we will probably not use them in
the course.