Hello guys.
So we are going to continue our discussion with respect to Python.
And in this video we are going to discuss about standard library overview.
Now Python has a lot and lot of libraries and packages which are completely open source.
And you can definitely use them while you're coding your projects or developing something.
Right?
So in this video I'll be showing you some set of libraries.
Obviously, it is not possible to cover all the libraries because they are more than, you know, 300
K plus libraries, I guess.
Uh, so it is very much difficult to cover each and every thing.
But just to give you an idea how to use some of the libraries, I'll be talking about them.
So first of all I'm going to show you about array.
So array uh, is one important library which will actually help you to create an array itself.
Right.
So here if I go ahead and define my array variable.
And if I just go ahead and write array dot array.
Right.
And here uh in this particular array array we have to give two parameters Okay.
One is the temporary variable.
Uh, let's say I'm giving I.
And then if I give one comma, two comma three comma four.
So here you'll be able to see type code.
Uh sorry I basically means integer type code.
And your initializer.
Right.
So with respect to that it returns a new array whose items are restricted by type code okay.
So here it is basically going to print an array with this integers okay.
So you can see over here that I'm able to get this particular answer right.
So this is one uh I've already spoken about math.
So let me just go ahead and again show you with respect to math.
So here I'm going to use math dot skew RT1 common function that you have over here.
So if I go ahead and want to find out the square root of any number I will be able to find it out.
Then let me just go ahead and print.
Okay.
Math dot pi.
And here you'll be able to see that I'll be able to get an answer.
Okay.
Perfect.
Now let me just talk about one more inbuilt, uh, library that we have, which is called as random.
And this is most commonly we use we use it okay.
So all we have to do is that go ahead and just do import random okay.
And then we will go ahead and print over here random We specifically use two important uh inbuilt function.
So let's see which all inbuilt function is there which is called as Randint okay.
Randint basically means it provides a any random integer based on the value that we give over here.
Like let's say if I give one comma ten.
So it is going to give a random integer between one to 1 to 10, only one single number it is going
to give you.
Right.
Uh, you know, it will be between 1 to 10.
It will just pick up randomly and it will be giving you a number.
Right.
And in one more function that we specifically use in random, which is called as random dot choice,
let's say if I have multiple choice over here.
And from that I want to select any choice in the case of string or anything as such.
In in the case of collection.
Sorry, then uh, I will be able to use this.
So let's say apple is there, banana is there.
And over here you'll be able to see that cherry is there.
Okay.
So if I go ahead and execute it.
So here you'll be able to see that okay Randint I'm able to get one value in and the value will be between
1 to 10 okay.
Random choice is nothing but from the collection what choice it is basically picking up.
Okay.
So if I again execute it this may change.
Okay.
If I again execute it it this may change right?
It may not be all the time same you know.
So it is randomly picking up this particular values.
Now, uh, we are going to talk about one very common library that we use that is called as OS.
Right.
So that is used for file and directory access.
Okay.
Directory access.
We'll be talking about file handling and more operation there.
Also we'll be using this.
So let me just go ahead and import it.
So this is called as import OS.
And let's say I want to go ahead and print in which current working directory I'm working in my project.
So I can use uh OS dot get cw d get current working directory.
Okay.
And if I get an execute it here, you can see that I am in this particular folder location that is my
Udemy final Python.
And this is my fifth module right.
So I'm inside my fifth module.
And automatically I'll be able to see that which working location I'm there.
Let's say in this particular working location, I want to probably go ahead and create a folder.
So I will go ahead and write OS dot make directory right.
So they are make directory, make directories different different function is there.
Let's say I want to go ahead and write test dir.
So this will be the directory name as soon as I execute it.
Now you'll be able to see that I will be having a folder which will be of text directory.
Okay.
Now one assignment that I really want to give, go ahead and find out the function that is used to create
any file okay.
So that you can actually go ahead and do it okay.
Now um, let's say I want to do some high level operations on files.
Right.
High level operations on files and collection of files.
Okay.
Let's say uh, for that I will be using this import.
Scott, shoot util.
Okay.
Ill.
And this function, let's say that I want to create util over here.
And uh, let's say that I have one source dot txt file okay.
So here let's say I have one source dot txt file.
And let's say here I go ahead and write.
Hello.
Uh, how are you?
I'm good, I am fine.
Welcome to the course.
Okay.
Something like this.
Now inside the standard library.
By using this, let's say I will go ahead and do one operation which is called as copy file.
Okay.
I will copy this file.
That is nothing but source dot txt.
Please make sure that you have.
You are in the same working location.
And then let's say I will go ahead and create my another file which is called as destination dot txt.
Now let's see whether we'll get an error saying that whether there is a file or not.
So here you can see destination dot txt is there.
Did I get it?
Yes, this is my destination dot txt.
This is my source dot.
TXT automatically the copy from source dot txt happened in the destination dot txt.
Okay, so this is one another important operation that we specifically use in this.
Okay, so let me just close this.
I think many many files has got opened okay.
But I hope you are able to understand it.
What we are trying to talk about okay.
Now the next thing is that, uh, let's talk about one very important, uh, or common property or common
thing that we will be doing.
It is nothing but data realization, like converting a dictionary into a JSON right, or converting
any a data frame data frame, what it is.
We will discuss more about it.
Let's say that we want to convert that into a JSON.
JSON is very much important because whenever we create an API's, let's say whenever we are retrieving
any data from the databases, specifically NoSQL database, the return type of the data or the response,
or we are interacting with any API, the response will be in the form of API itself, right?
Uh, sorry.
Response will be in the form of JSON.
Right?
So in order to work with JSON so we usually import this JSON uh library.
And let's say that I am creating one data okay.
Let's say the name is name colon is Krish okay.
And here what we are going to do is that we are going to basically write the age will be colon equal
to 25, right?
So right now this is just my dictionary.
Now if I want to convert this dictionary into JSON.
So I will use this JSON dot dumps okay.
And when I write JSON or dump on this particular data I will be getting JSON in the form of string okay.
Which is basically required.
Okay.
Now let me just go ahead and print this okay.
So I will go ahead and print JSON underscore str okay.
Now once we print it and let me just go ahead and print the type of this also so that you will not get
any confusion.
So type of JSON underscore str okay.
Now let's say I want to convert this back into this kind of dictionary again.
Right.
So for that one operation that I used is JSON dump, one operation will be JSON dot load load.
Right.
So whatever JSON let's say with when I'm interacting with an API, I get kind some kind of JSON in response
that me that will be always in the string format, which I've actually converted.
Now, if I want to convert this back into a dictionary and probably do any kind of operation over here
so I can actually go ahead and use JSON dot loads operation, and here I will give my JSON string.
And now this will basically be my past data is equal to this one right.
And now what I am actually going to do after this is that I will go ahead and print my past data.
And uh, I will also go ahead and see the type of it.
So I'm going to see print type of past underscore data.
So if I go ahead and execute it here you can see initially this was the my uh see it got converted into
a string.
So the class is string.
Then again we converted back into a dictionary.
Right.
So JSON, whenever we get in the form of API from an API it will be in this particular string format.
We can just load that particular information, convert that into a dictionary and perform data serialization.
Okay.
So this is one very good example of, uh, probably writing each and every thing over here.
And, uh, this is what is all about deserialize data serialization.
And we'll be using this a lot as we go ahead.
Okay.
Now let me just go ahead and talk about one more standard library, which is called as CSV.
Okay.
Whenever you need to work with a CSV file.
Right.
And there are a lot of files which will be learning and we'll be using pandas and all.
Uh, there will be learning more different kind of operations and all.
Okay.
So I will go ahead and just import CSV.
And now with open right with open.
And here I'm going to specifically use let's say I will I will create an example dot CSV file okay.
And then let me just go ahead and write the mode.
Right.
So open has first of all you need to give the file name.
Then then what mode you really want to open it.
Let's say I want to more open this in write mode.
So I will go ahead and write w.
There is also something called as w plus mode.
So w plus is nothing but a write append mode okay.
And here I will go ahead and say hey use the new line as this.
Okay.
So it should be as this.
And I will open this as a file object okay.
Now by using the CSV dot, I will go ahead and write inside this particular file.
So there is a writer function right.
We will write inside this file.
And this will basically be my writer.
Now I can go ahead and take any data and write each and every row.
So here what I'm going to do I'm going to basically write writer dot write row okay.
And let me just go ahead and give the row.
So first row will be my column names name comma age.
I have to give this in the form of list or collection.
Then I will go ahead and write a writer dot write row and I will start writing my second row.
And now I will start with my data.
So let's say crush.
And here I will be able to put um 32 or I'll go ahead and put integer right now this, in short, what
it is going to do is that it is going to create an example dot csv file.
Okay.
And there it is going to insert two records.
One is with the column name name comma age.
And the next one is with respect to the data.
Okay.
Now what I'm actually going to do I'm going to write with open okay.
And here I'm going to specifically write example dot csv.
Because once I create this particular CSV file it is in the right mode, right?
I'll create the CSV file.
Now we'll try to read it okay.
Now I'll change the mode to R okay.
And now what I will do I will basically read the entire file the CSV file.
So I'll create a reader is equal to CSV.
Dot reader function is there.
And I will give the file name okay.
Whatever file is over here.
Then since I will be having multiple low.
So I will go ahead and write for row in row reader and we will just go ahead and print the rows.
So what we have actually done we have created a CSV file.
We have written two records.
And then now we are opening the same CSV file and we are reading the data.
Right.
So let's go ahead and execute it.
So here you can see.
Let's see first of all whether the CSV file is created or not see example dot CSV is created.
And here is my two records.
You can open Also open this with Excel file.
Okay.
And now I'm reading this and I'm also displaying it over here.
So perfect.
Uh, this is one very important function that we can actually use.
Okay.
Uh, so uh, you can actually go ahead with, uh, seeing this.
Okay.
Now, one more function that I or one more, uh, standard library that we specifically use is date
time.
Okay.
Now, with respect to date time, you can go ahead and import something like this from date time import.
So date time is again a module.
So I'll be importing from date time import date time.
Along with that I can also import one more function which is called as time delta okay time delta.
Now if I want the current time so I can go ahead and write date time dot.
Now okay.
So date time dot now will specifically give you the current time or current date okay.
Then let's go ahead and print now okay.
Let's go ahead and print now.
And then uh, let's say uh I want to probably um, you know currently what is the time.
And yesterday one day back.
Right.
So I can actually use time delta.
So let's say yesterday I will go ahead and write is equal to now minus time Delta.
Let's say that I want to go ahead and calculate a difference.
And here I'm going to use one parameter.
Days is equal to one.
I'm saying that hey go one day back.
And with respect to that just try to calculate the delta itself.
Right.
So uh like one day back what will be the time.
And all right.
So here I'm going to just go ahead and print yesterday.
So if I go ahead and execute right now the time is 1137 and I can see the watch, it is perfectly fine.
And yesterday, one day back it was 1137.
I'm just doing currently divided by minus time.
Delta of day is equal to one and yesterday time will be something like this right?
So this is also one very good thing uh, with respect to this.
Okay.
Now, uh, one more standard library that we can specifically use and which is common is nothing but
time.
Okay, let's say I'm writing a code.
I want to make the program sleep for some couple of seconds.
Right.
So for that I can use time.
So import time.
And let's say if I go ahead and print time dot time okay.
And uh let me just go ahead and write, say hey, after this you just sleep for two seconds, sleep
for two seconds, and then again, let's go ahead and print time dot time.
Now if I go ahead and execute it uh, here you can see it is sleeping for two seconds.
And then I got this specific thing right.
Uh, this is, uh, what it is more about.
And, uh, you will be able to see that.
Wow.
We are able to do some amazing thing with respect to this.
We are able to make the program sleep for some seconds also.
Okay.
now, uh, one more, uh, common libraries that we will be using is something called as regular expression.
So for regular expression, uh, uh, there is a library again which is called as import re okay.
Now regular expression basically helps you to match a specific, uh, expression in a top of a sentence.
You can do it on an entire data set and do many things.
Let's say that I define a pattern.
Okay.
Now, with respect to this particular pattern, uh, I'm saying, hey, use this pattern.
Something like this.
Okay.
I'll say, hey, go ahead and use this.
Uh, it will be mine.
Uh, slash d plus.
Okay, let's say this is the pattern that I select, and I'm having a text.
Okay.
There are 123 apples.
Okay.
Then I will say let's go ahead and match whether this pattern is there or not okay.
Digit plus okay.
Whether this pattern is there or not.
So in order to do that I will just go ahead and write Ari Dot search.
And here we are going to use pattern comma text okay.
So let's go ahead and print match.
Match dot group okay.
So if I go ahead and use dot group.
So here you'll be able to see the answer where this specific D plus pattern is getting matched.
So here you can see that 123 is present and D is basically digits right.
We are just going to see that where digits are specifically there.
Right.
And we are going to get over here.
If I go ahead and write over here another one four, five, six.
So here we'll be able to see that after getting this continuous digit match, then, uh, it will stop
over there because the match is successfully done over here.
Right.
So match is successfully done.
So we don't have to, you know, uh, check the forward one.
So this is some of the things that we specifically use in regular expression.
Now this can be lot more complicated.
It is not you always have to probably use this.
Right.
It is other things.
Also, you can probably check.
Okay, so I hope, uh, you are able to understand this.
Uh, these are some of the standard libraries.
Again, they are lot, lot libraries as I always said.
Okay.
There is no limit as such.
It is up to you how much you can try.
You can practice, keep on practicing and based on different different scenarios we can use this.
So I hope you like this particular video.
And this was it for my side.
I'll see you in the next video.
Thank you.