Welcome back to the
last time we successfully created, created our very own custom module
and we were able to import and make use of it in our main file.
But now I want to show you how we can import
some of the standard libraries and modules that comes with Python.
So let me go ahead and remove all of this code. And I'm going to leave the keyword,
import the main function
and the name of the module we're going to import is called random.
This is an inbuilt module within Python
that would allow us to make or generate random numbers from a sequence. In fact,
let me show you a bit of the documentation in here.
You can see Python has a built in module called random.
And the thing about random is that it's so powerful,
it has so many methods that comes with it.
Take a look at the choice method. This will return a random element
from the given sequence. And we're gonna make use of this particular method
to generate a random letter. So
check this out. OK.
I'm gonna first of all create a variable called letters and then equals two
and I'm gonna type in
all the letters in here A to Z. OK.
So
we want our program to generate a random letter from this list. OK?
From this sequence.
So
how are we gonna do this? Well,
I am going to create a new variable called a random
on the call letter.
OK.
And now in here, I am going to say random.
Remember the sequence.
First of all, we'll have to add the name of the module library. So it's random in here.
And now the mode,
the method which is choice is gonna be dot
And now choice.
And now in brackets
letters,
that is all I have to do. And now
what do we do?
We print out
and I can say
the random letter chosen
is
and then
I can add my comma and then simply add random underscore letter.
And there it is,
that's all we have to do. So now if I run
the program
there it is, the random letter chosen is M OK.
Let's try running the program again. Maybe we will choose a different letter.
It should.
And there you go. Now it's C
again. Now it's a
one more time. Now it is V you can see right now it actually works.
So a quick recap. What did we do?
First of all, we imported the standard module called random
that has a list of different types of methods
that will generate a
random letter.
So the first thing we did was that we created our letters
variable that will hold all the letters from A to Z.
And then we assigned another variable called random letter
that will now hold the actual
letter that will be generated, the actual random letter that was generated.
And then we said random bin,
the name of the module and then appended the method which is choice
and then in brackets letters
and now we simply printed out
the random letter that was chosen
in line four.
So that's it. Thank you for watching the video. I will see you in the next class.