Hi guys.
Welcome back to another class of our course about them.
So in this class it's very simple.
My goal is to talk to you guys about another way that you can store your data as well as convert your
data from one that I type.
Well from one type of data to another type of data.
So not only converting numbers into let's say certain strings but also converting certain strings to
another types of strings.
So it sounds maybe complicated but you'll see it's very very easy.
So basically what we are going to talk about are dictionaries in Python.
So what's what exactly is a dictionary in the normal world.
So when let's say a real dictionary is basically a place where you can find out a word and the definition
of words of this word.
So it's gonna be one word linked with many words in Python.
It's the same thing.
So you guys will take one word.
So it could be a string or a set of words and make it equal to let's say another set of words.
So this is a way that you guys can store your data and also converter that as well.
For example if you want to make a list of employees.
So for example we can have no the number of each employee and you can have the name of the employee
associated with his number.
So in this case yes.
As you can see this is a way that you can use dictionaries.
So Michael today is really to present you this useful thing that you can use in Python and give you
two examples of how you can use.
Well this dictionary and those two examples well they can apply yet in everyday life.
All right.
So let's start first thing before.
Well first thing that you guys can understand I have to understand is that dictionaries can store any
type of data so it can be.
It could be strings it could be numbers and they can convert.
Well you can store a string with a number or a number with a string.
You'll see it's a very very simple to use.
All right.
So let's begin.
So how do you write down the dictionary.
Well it's very simple.
It's like writing down a variable.
So it starts the same way.
So let's say our dictionary will be named colors in this case will have our first example would be colors.
Then you click on equal.
Instead of writing down a sentence like you would do for a four when you are creating a variable you'll
simply open up this parenthesis.
So in this case those parentheses are used for dictionary.
So everything that is inside of those parentheses will be all the words that we'll have for our let's
say our dictionary.
All right.
So the first word that we are going to use as our dictionary will be blue.
So we will generate Blue by simply the first letter of this word.
So let's say B in this case will be equal to blue.
So be two points in this case write it down like this.
So B two points is equal to blue.
So very important.
This is your first word.
So in this case this will be the key.
And this is the same word.
This is the word that is linked with the key.
So you write down your word you're your first word the key right here and the second word will just
be linked to it.
So when you are going to make a search for inside your dictionary you're going to make a search for
the word that is right here.
And this is going to be your output.
All right.
So this is your first word.
Let's say we want to add more more words in our dictionary.
So very simple.
You click on the comma and you add your second word.
So in this case let's say want to do the same thing for red so write down are two points Red
let's say wanted the same thing for orange.
So.
Oh we write down two points and we write orange.
All right.
So let's just Kip do those two words inside our dictionary.
So as you can see each letter will refer to a word.
So very simple we stored this inside of our dictionary.
So another thing that you guys need to remember you can't have two letters that are the same so two
keys that are the same.
So in this case I can't have it be used two times.
So let's say for example I tried let's say on a store before.
I don't know.
B for ball.
So I want to add a ball.
So as you can see it's not going to work because B is written two times here.
And if you store the same let's say a word two times it's not going to work.
So here for example it's B.
So if I have two same values to same two keys that are the same.
It's not gonna work.
So you need to have two different keys.
Great.
So right now we have those three words and let's say our goal we want to fight what we want to look
for.
The word are.
So imagine you have millions of words and you want to look for are right here.
So how exactly would you look for.
So the first thing that you guys will do is write down what print.
Because we want him to be printed so they don't print we write them or a dictionary name which in this
case is colors
dot jet.
So in this case does get here will have two values.
So the first value will be the value that we are looking for.
So in this case we are looking for B
or r or any value that you want to look for.
So let's say we are looking for a B.
And the second value will be the default value so in this case let's say it's going to be stopped.
So the default value is simply the value let's say.
If for example the diction Python doesn't find the word in the dictionary he will return the default
value.
So for example let's say instead of b we have I don't know h and we don't have each and our dictionary.
So python will return stop.
So let's run the app and see what it looks like.
So as you can see B stands for blue.
So it returns blue.
If for example instead of B will write down 0 which is orange we run our app as you can see it's going
to give us orange.
But let's say we write 0 with a capital letter.
We don't have it in the dictionary.
So as you can see it's going to generate stop because we don't have this value.
And if we can generate let's say write down I don't know G for example we don't have it.
So it's going to generate the same stuff.
So the full value simply the value that is generated when the value of the word where the key doesn't
exist in the dictionary.
All right.
So the second example that you get that we are going to do today is very simple.
We will create employee numbers and we are going to look for those employees.
So let's let's creates this basic app so let's have I don't know five numbers.
In this case we'll have five numbers and give random names.
So let's say.
Name a.
So a random number and a random name.
So in this case the name B
and let's say named C..
So my goal here is to present you that you can use dictionaries for names for names for strings as well
as four numbers.
So in this case we are going to use a dictionary for numbers and we'll find out numbers.
Well we'll find out names by using numbers
grip so let's do it.
As I explained the here it's going to be names so I explain the formula to write this down the function
to write this down is very simple.
Simply write down names so your dictionary named dot get footnote up your parentheses.
The first thing that you guys will write down will be your value your key sorry.
So in this case it's gonna be let's say we are looking for a 150 to and the second thing since it's
a number we keep it like this.
And the second thing is the default value and in this case the default value will be let's say uh this
name doesn't exist don't exist.
All right and want to print all this
here.
All right so simply run the app and as you can see the number one hundred fifty two is then the A.
If we change a number and write down some five for in this case it's gonna be the name D but let's say
we write down 9 9 9 by some during us this name don't exist.
Let's see.
This is the definition of a dictionary in Python.
You can see what is it exactly right now.
And right now I think you can work with this.
Well this thing that is a dictionary so that's it for this class guys and see in our next class.