Hello and welcome to this video.
In this video I'm going to introduce you to the Python data type code dictionary.
What is a Python dictionary.
A real life dictionary holes words and their meanings.
A Python dictionary is similar but it holds key value pairs key value pairs.
Basically mean all the values inside the dictionary will have a unique key and a corresponding value.
For example you could have a key that is called fruit and the value would be in name of a fruit.
For example Apple.
So that's what it means by key value pairs.
So they come in pairs.
You have the key and then you have the values.
The values can be changed.
That means they are mutable.
Also the values must have unique keys so you cannot have duplicate keys for the same values.
The keys are unique.
You can also store mixed data types so you can store Strings and you can also store numbers inside a
dictionary.
You can mix the data types or you can have a specific data type.
There are different ways you can create a dictionary.
So let me walk you through a couple of ways.
I'm going to be using the text editor called atm and I've created an empty file on my desktop called
My dict and I'll call it my dict dot P Y.
So any code right here will be a python file.
First you need to do is give your dictionary a name.
Try to give it a name that reflects what it is you are storing.
So I'm going to call my dictionary my car and then R equals 2 and then I'll add my curly braces on our
top in between to separate the curly braces and I will start adding the key.
So I stuck with the key first.
So the key are typing brand and after the key do a column and I then specify the value.
So my value I'll say Range Rover sports OK.
That is one key value.
This is the key which is unique.
And this is a value the value can change.
But the key has to be unique for each value someone to tap and add another.
So you need to separate each of the key value pairs with a comma.
So had a comma come down here and do a different one.
So add another key this time I'm going to give this a model make this model and I will add the colon
and give it a value the fairly how say h s e the comma now had one more key value this time.
Who's going to make this one the year.
Make it in lower case.
Yeah and then the colon and I'll add the year I'll say 2017.
Notice this is a numerical data type so I don't need to put quotes around it and I don't need to add
a comma because that is the last value.
So no need I'm adding more than I use a comma to separate each key value pair.
So that is basically it for creatine addiction or I've just created a basic dictionary so if I wanted
to print this dictionary all I have to do is type in print and in parentheses I specify the name of
my dictionary which is called my car and I'll save my document to control s to save it and then I'll
run it inside my terminal or command prompt so I've got my command prompt here and I've navigated to
my desktop which is where my Python far less and that's the name of the file.
So if I just press enter it should execute the code.
You can see here is printed out my dictionary.
That is the key.
That's the value.
That's another key that's value.
That's another key.
That is the value.
Another way you can create a dictionary is by using a dictionary constructor function the constructor
is basically a special function that you can use to create an object.
So to create a dictionary using the constructor method you first of all have to give the dictionary
names.
I'm going to call mine my my greens and you do the equals to type in the word dict.
Okay and then in parentheses you then specify the key and then the values.
So this is how you do it.
If that's for typing the key is a bit different when you use a constructor method.
So you use the key and then once you specify the key you are the equals two and then you enclose the
values in quotes.
If it's a string values I'm going to say green apples.
Okay so that is one of the comma.
Now add another key.
The key this time is going to be virtually tables.
Okay.
That's the key.
And our two equals two.
And then I'll add value.
I'm going to enclose that in quotes as well.
And the value will be kale I can add a print function and then print out what's inside this new dictionary
are created.
All I need to do is just pass in the name of the dictionary and I'll save the file and open up my command
prompt.
That's my command prompt so I just type in the name.
You can see here that is my first dictionary.
This is the second dictionary I created using the constructor function.
So that's basically how you create dictionaries.
That's it for this video.
Thanks for watching.