Welcome back.
Now we're going to be spending a good chunk of this course building some projects and using some built
in Python modules, as well as using some popular third party libraries from Python package index.
However, I wanted to just show you a quick video on some of the useful modules that I personally like
in the built in modules that we have from Python.
And some of these really useful modules actually allow us to have more data types in Python.
Remember this list at the beginning of the course where I talked about the data types that comes with
Python, and then we learned about classes and how we're able to create our own data types, our own
objects in Python.
But we also have these specialized data types that come as well built into the Python standard library,
which are really, really nice.
And this can make our programs really, really powerful.
Now they're specialized because you're not going to see them everywhere, but it's good to know that
they exist.
And I'm going to go over some of my favorite.
The first one I'm going to show you is the collection module.
So the collection module is from collection or collections.
And there's three things that I really like here.
So I'm going to say import, I'm going to import the counter, the default dictionary, and then the
ordered dictionary.
Now notice how some of them are classes because they're capital letters.
And some of them look to be just a function.
And you can read up on the collections module by just again, scrolling through here and just looking
at collections and you can see all the ones that we have.
So in these collections, let's start off with the first one.
A counter is extremely useful.
Let's say I have a list that contains one, two, three, four, five, six, seven and I want to count
how many items in a list if I do print counter.
You can see over here what it does.
But essentially, if I give it a list and click run.
All right.
What just happened here?
It creates a dictionary where one has one, two has one, three has one.
Hmm.
What if we add another seven here?
Let's say seven.
If I click run.
Did you see that?
Seven now has two.
So what it does is it creates a dictionary that keeps track of how many times an item occurred in an
iterable.
So that seven occurred twice.
So seven has a value of two.
Another way we can use this is let's say we have a sentence and say in the sentence, I want to check.
Well, let's just write something.
I'll say blah, blah, blah.
Thinking about Python.
If I was to do a counter here and let's say give it sentence if I click Run.
Check that out.
I see which letter occurred the most.
In our case, h occurred five times, space occurred five times, and then B occurred four times.
And this is really, really cool.
Because this type of calculation of a counter of turning an item like a list into what we call a hash
map or a dictionary like this to keep count is actually a very useful function that you see all over
programming.
It actually helps a lot with optimization problems.
Now, we're not going to get into it there, but I just want to show you that counter is something that
is really, really useful if you want to count duplicate users or duplicate emails.
Counter can be really, really useful.
What about the next one?
Default, Dick.
God, that's a really hard word to say.
All right, so let's say we have a dictionary here, and this dictionary we'll have well, a very insignificant
dictionary will have a one and B two.
What a simple dictionary.
Now, in here, I can say dictionary.
Dot.
Or bracket A and I'll be able to grab the value.
Right.
If I click Run.
Oh, and I did not spell dictionary.
Right.
Let's fix that.
That's better.
Let's comment this out for now because we don't need this.
You'll see that I get a value of one.
But if I try to access something that doesn't exist, like.
See?
I'll get an error with a default dictionary.
I'm actually going to get a default value if something doesn't exist.
So let's convert this into a default dictionary.
Like this.
And now if I run this, we get an error.
First argument must be callable or none.
So what default dict does is it allows us to access a key like this, but give it a default value.
In our case, we need to give it something like a callable object in here.
And callable means something like a function that can be called so if I do run.
I get zero.
Why is that?
Well, when I run int.
Like this with nothing inside of it.
We get zero.
So a shorthand way because this is going to be run is to give it a quick function that we're not going
to use.
So let's just do a lambda and just give it.
Let's say five.
So now if I run this, let's do dictionary here.
Run.
Oh, and let's try and grab.
Something that doesn't exist.
If I click Run, I get five.
So this is a common pattern.
You see, with default dict, we just give it a value or a callable object.
In our case, we're just giving it this function, a lambda function that simply returns five.
So I can even say in here.
Does it does not exist.
The second parameter will be whatever we want.
And if I had run.
I get does not exist, even though I'm accessing a key that well doesn't exist.
Now, if I do a here and I click run.
Everything works.
I still get one.
And if I do be.
I guess run as well.
Again, another useful little data type.
Next one is ordered dicked and this one is another interesting one.
And order dictionary retains the order that you insert into a dictionary.
Let me show you.
So if we have, let's say dictionary and this dictionary will be an order dictionary.
So this is how we create it.
Now in here, if I do dx equals to a.
And this is going to equal to one.
And then D.
Be is going to equal to two.
And I now copy this and make a second dictionary.
Let's say rd to.
RD to.
And I check.
Hey, print does DX two equal dx?
Remember this is going to check for the values.
If I run this.
I get true?
Yep, they're equal.
But if I change the order of insertion here and I click run.
I get false.
Sorry, this is not the same thing because it maintains the order.
If this was a regular dictionary.
So let's say instead of what we have here, I'm going to just simply create a dictionary and we'll just.
We'll just do a simple see here that has, let's say, 100.
And do the same for this one as well.
DD two.
Equals to this.
If I now run this.
I get true because all the keys and values are the same with a regular dictionary.
We don't really care.
There's no order.
And I mentioned this before.
To write a dictionary in Python has no sense of order.
Although when we print out the keys, we might get things in order.
As our dictionary gets larger and larger, there's no guarantee that when we print out all the keys
that we're going to get them in order.
In the case of an order dictionary, though, we get that guarantee.
Now, there are some performance implications for those.
However, overall, it can be useful in some situations.
All right.
A few more to go through, but this video is getting long.
So let's take a break and I'll see you in the next video.
Bye bye.