Let's learn about the use of libraries in python and how we can
create our own libraries using simple coding. First of all before further
reading, we need to know why do we use library and what is its use.
Have
you ever thought while visiting the libraries that how the librarians manages
the huge number of books. They use indexing so that books are kept in arranged
manner and they are indexed by categories and keep track of all the books so
that people are easily able to find it when needed.
So
from the above lines we can observe that how important libraries are, in the
similar way programming languages comes with preinstalled or manually install-able options by using different package managers like apt,
yum, pip, chocolaty, conda, etc.
However,
most of the standard libraries comes pre-installed with the programming
language to support the better computation. Also we learnt that in
libraries books are kept in indexed manner and in programming libraries are
indexed by filenames and file formats, like in c programming .h files are known
as library files e.g. stdio.h, conio.h, etc.
One
more point to be noted is, these library files consist the classes and
functions that are optimized to process at very low memory consumption.
For
example, if we calculate the roots of a quadratic equation by using the Sridharacharya’s rule for finding the roots, but if we use cmath library (in
case of python) ,then it uses the sqrt() function which when compared to user
written code takes a very less (approx.. 100 times less) time as well as memory
consumption is also low.
Also, if we see libraries like pandas and numpy then
we find they take comparably very less time w.r.t standard libraries
because they convert the data types of variable to data frames in order to fit
them in the way such that they take very less data and so these libraries use
very advanced data algorithms.
Programming language like python allow us to create our own custom
library/module and publish them on the repositories.
So,
let’s learn by creating a module in the python programming language. To
create the library/module in python we just need to create a module file with .py
(i.e. module.py) format and create another sample code with .py (sample.py)
format again as we will use the previous file to import in sample code and use
its function for work. Please make sure that they both the files lies in
same folder for easy integration, otherwise we will need to provide the path.
To
access the module we need to import the module first and then we can use the
functions by using ‘.’, like if there is square function then we write
module ".square()".
Let
us now create the file and here we are going to create a library for the functions
square() and cube() and we will later take input integer and will apply
operations on those inputs using the module functions and print the result as
output.
Follow the steps and images:
Step 1 – Create the folder and the two files module.py and sample.py
Step 2 – Edit the
module file and add some function, I am using basic square and cube functions.
Step 3 – Now import the module to the code file.
Step 4 – Now take input and apply the operations using the library functions i.e. in
our case square and cube.
2 Comments
Can't we create a library in C
ReplyDeleteYes, you can definitely create your own library in C, but it is quite difficult as compared to Python.
DeleteIf you have any doubts, please let me know.