Have any question?    +91 9810600764      info@palin.co.in

What are python libraries, packages or modules

What are python libraries, packages or modules

Python Packages, Libraries, Modules

When we are dealing with data and we have to do some mathematical operations like finding the sum, or difference or standard deviation of a particular thing, and we have to repeatedly do these operations so we will make separate functions for maybe finding the sum or finding the difference etc. instead we can make a file of the scripts we have written, in Python for the same operations called calculator.ty, which will have four different functions.

These files can be called Libraries or modules or can be called a package. These files can be shared, for example there is a date time library which has listed in it all the operations related to date time. Now suppose with the mathematical operations certain string operations also need to be done. If we want to know the difference in days between two particular dates, the system will do the needful as there has been programme done and saved in the files. These libraries can be made by yourself or they can be downloaded. Sometimes it so happens that the libraries are so big that they have to be segmented into smaller libraries. And there could be a file which correlates the various libraries. There is a keyword “import” which is used to make use of these libraries. We also have the option of importing just a relevant section of the file instead of the whole file which could be very heavy. So suppose we need to import only L1 from a file called PKG. then we can import only L1 from the concerned file and not the whole file.

 

WORKING WITH DATETIME

If we are importing Datetime from a library which is also called Datetime. So if we write import datetime and datetime. The contents will be displayed of the various libraries within Datetime.

And if we look for datetime.datetime. we will see that there are various functions, or attributes in it.

If we write datetime.datetime.now(), we will get the following result.

 

It will show the year first and then the month, followed by date and time. All date notifications are written in this sequence.

datetime.datetime.today() will also give the above output.

Suppose we have to read three different dates written in three different ways randomly.

 

 

If the year is to be seen as a two digit number then the code % with small y is used and if it is to be seen as a four digit number then the code % with capital Y is used.

Now if we want to read the three different dates, which string will have to convert and also the format in which we want to read it, we will have to give the following command.

Similarly to read the second string, the command would be this

And for the third date, the command would be this

If we want to see the difference in days between the first and the second date

 

We have a list with some numbers written in it. We want to make another list containing the squares of all the numbers of this list.

List comprehensions is a technique which can do the whole work and show results in one line.

Dictionary comprehension is similar to List comprehension

There is an enumerate function which will give a sequence to the numbers,

Now using these if were to put them in a dictionary we would write this command

 

 

 

×