One of the key features of Python is that the core language that we've been learning up until now is
pretty small.
As a matter of fact, we've pretty much covered most of Python.
Now, Python was created in this small, tiny package as a feature to keep things simple, although
you may feel like you've learned a lot at the end of the day.
It shouldn't have been that bad because there's not a lot of things to memorize.
We also learn how to organize our code so that we have multiple python files.
But the reason Python is so popular and it's used in all sorts of industries from big tech companies
like Google and Netflix to Amazon to small projects and people like us who just maybe are single developers
building tiny projects.
Python is extremely useful through what we call external modules and packages, that is outside of what
we get from Python.
Well, we have an entire package system where we can borrow and expand on the Python functionality.
And these are called Python's Built in Modules.
Let me show you what I mean.
If you go to the Python documentation, they have the Python module index that I'll link to.
And here we have all the modules that Python give us.
Now, these modules actually were installed when we downloaded the Python interpreter.
It comes with Python, but in order for us to use them, we have to import them.
These are the built in modules and they're really, really nice.
For example, do you want to send, I don't know, an email?
Well, there's an email module.
Maybe you want to really do some mathematical operations that are complex while there's a math module
for that and there's a module for a lot of things.
Now, don't get scared.
This looks like a lot, but you're not supposed to memorize any of these.
You just need to know that they exist so that when you're Googling something, you can type in Python
built in module.
And let's say, for example, you want to build a game.
Say Python built in module game or graphical user interface and check if the module exists.
I have to say, I've probably only used 30% of these.
But they're there for us to gain extra powers.
In other languages.
This might be called a standard library.
That is it's a standard library maintained by the Python community members and making sure that these
packages work really, really well with Python and that they're built into the language.
So let's learn how to use them and use one of my favorite ones, which is called random.
So let's see here if we can find it are random generate pseudo random numbers with various common distributions
and you can read all about it here.
It's a long wait.
So let me just show you.
Now the beauty is that we've actually installed this.
Whenever you install Python on your machine, you get access to all the built in module, the standard
library.
So all I have to do is say import random and now I have access to random if I run this.
Let's click Run.
Look at that module random from library frameworks python.
You see that it's in versions 3.7 library python and random pi.
This is actually installed on my machine and it should be installed on yours as well if you follow it
along and installed the Python library.
Now where is this?
Where you can actually see it in our pie chart?
If you go to external libraries here and you open this up, you see here that I have the Python 3.7
modules and if I scroll down and you go to Python 3.7 or whatever your installation was.
And scroll all the way down to, let's say, ah, where is it?
Where is it?
As you can see, there's a lot.
Here it is.
Random dot pie.
This is installed on our machine.
And if I double click this.
Look at that.
I have the random variable generator and this is the file that allows us to use random.
And you can see over here that random is a class that is going to allow us to, well, use all this
functionality and look at that.
It even has this little line that I told you about in the previous video.
Only run this test function.
If this is the main fault run.
This is probably something that the maintainers of this module, whoever coded this, add it to say,
hey, don't run this for anybody else.
But if, for example, we're working on this and we want to run the test function to test that all of
these things are working, then run that.
But in our case, when we use random as users or consumers of module, this is never going to run very,
very cool.
Now, you can see here how many?
Modules we have and we even have some packages like the email package with well a whole ton of things.
So this is already existing in our computer and that's why we have access to import random.
So let's learn more about random.
Now, what I like to do usually when I'm importing a library and I don't know what it does is to actually
use the help function that comes with Python.
If I run this.
You'll see here that I'll get a help message with, well, all the information and documentation of
what it does.
So it gives you some description if you scroll down.
It shows you some of the code and some of the methods that perhaps we get.
Which is really, really nice.
Let's lower that down.
Another thing that I like to do after perhaps I read the documentation is to do the.
Dear.
Function and if I click Run.
And let's actually print that.
If I click Run.
Here we go.
It shows me all the methods.
Available on this package.
So I can see.
All right, all these things that I have, I have rand range sample seed that I can use.
If you're using an ID like pie charm, I could just do dot and actually see all these methods as well,
which is really, really nice.
All right, let's have some fun with this random module.
Random has randomly a random method.
So random dot random.
Is going to give us.
Well, let's print it out and find out.
It's going to give us a random number between zero and one, as you can see here.
So this is really useful.
Another thing it does is it gives you a rent int, which is a random integer and we can give it the
first parameter where it should start and where it should end.
If I run this, look at that, I get four, then one, then six, then two, just a random integer every
time between the start and the finish.
Another useful one with random is something called choice.
And here, inside of the choice, we give it, let's say, a choice to make, for example, a list one,
two, three, four.
Or you know what?
Let's do four or five if I click Run.
It just makes the choice.
Picks one from the iterator list that we have.
Which is really, really nice.
Just random choices.
So why is this useful?
Well, maybe you want to build, I don't know, a roulette game or a game where you need a random generator
between, let's say, number 1 to 10.
So this is a nice way to just do that without coding it yourself.
Another one that I like is called Shuffle.
And Shuffle if I click run here.
I'm probably using it the wrong way.
So how can I find out more about this?
Well, this ID allows us to actually select and do view quick documentation, which you can see is F
one.
In my case it might be different based on your machine.
So I can just say f one and look at that.
Well, I see that shuffle has the sequence X in place.
That is a modifies whatever list we're giving it in place.
So it returns none.
So what we need to do is let's say my list, that will be 1 to 3.
Let's add a few more.
Give my list in here.
And instead of printing that after we've shuffled things, we'll say my list and then just print this.
If I click run now.
There you go.
I've shuffled my list in place.
Very, very cool.
Now, another important thing that I haven't shown you yet and you don't see it as often is to actually
do something like this.
I can say from random import shuffle.
Or I can actually do an alias so I can say import random as and then give it an alias that is whatever
we want to call it in this file.
So let's say ooh la la.
So in that case, a random becomes ooh la la.
So you can actually avoid name collisions if you want.
So if I click run, this should still work.
But again, a good practice for me personally is to always import only what you need.
In our case, we want to import from random, just the shuffle.
So I'm going to say shuffle because I'm being explicit here.
And when a developer comes and sees that I'm importing shuffle from random, they know that I'm only
using this function from the random file and there's no guessing involved just like this.
Let's learn some other built in modules in the next video.