A Python library is a collection of precompiled codes, sometimes known as modules. These modules may contain some useful functions, documentation, configuration data, message templates, classes, values, etc.
Using a library in a Python program eliminates the need for writing codes from scratch, you can simply import and use functions or anything else that a library provides in your program. This allows code reusability in a programming language and makes the software development process easier.
In this article, I will discuss more on python libraries and some important and mostly used libraries in Python.
Python standard library
The Python standard library comes included with the standard version of Python. It gets installed, when you install Python on your system so no additional installation is required. We use the functionalities of standard libraries in our day-to-day Python programming.
Some examples of tasks that the module of standard Python library provides are generating random numbers, working with CSV files, interacting with the operating system, working with date and time, etc.
Third-party libraries in Python
The third-party libraries can provide functionalities apart from what is provided by Python’s standard library. These could be open source or proprietary, developed by individuals or organizations.
For software development, Python offers a huge collection of third-party modules. To use a third-party library in your program first your need to install it on your system. Examples are NumPy, SciPy, TensorFlow, etc.
Some important Python Libraries
As of now, Python has over a million libraries you can simply import and use in your program. The following are a few examples of Python libraries that are mostly used.
NumPy – NumPy means Numerical Python it adds support for multidimensional arrays, matrices and provides a large number of high-level mathematical functions.
TensorFlow – TensorFlow library is used for machine learning and artificial intelligence. It provides a collection of workflows to develop and train models for desktop, mobiles, web, and cloud.
Scikit-Learn – Scikit-Learn is a machine learning library for Python, it is best for working with complex data. It is designed to interoperate with NumPy and SciPy libraries.
Keras – Keras is an open-source library for Python it acts as the user interface for TensorFlow. It provides the best tools for compiling models, processing data sets, visualization of graphs, etc.
Pandas – Pandas is a software library designed for data manipulation and analysis.
Matplotlib – Matplotlib is a Python library for creating static, animated, and interactive visualizations in Python.
SciPy – SciPy is a free and open-source library for scientific computation. It provides functions for optimization, linear algebra, integration, interpolation, image processing, etc.
Pytorch – It is built on the Torch library and is used for computer vision, natural language processing, etc. Primarily it is developed by Facebook’s AI Research Lab.
Example of using a Python library
To use a Python library you first need to import it to your program, Python standard library are preinstalled but if you want to use a third-party library then you first need to install it by using Python pip.
Now let us see some examples of using a Python library –
Here we will import the random module and use
randint() to generate random numbers.
# Importing the random module import random print(random.randint(0,99))
On executing this code you will see a random number generated between 0 and 99.
In the given example we will find the rank of a matrix by using
linalg.matrix_rank() method of the Numpy module to find the rank of a matrix. So first we import the NumPy module in our program.
# Python program to find the rank of a matrix import numpy as np # Enter a mxn matrix myMatrix = np.array([[1, 2, 1], [-2, -3, 1], [3, 5, 0]]) print("Given matrix is:") for row in myMatrix: print(row) # Finding the rank of the matrix rank = np.linalg.matrix_rank(myMatrix) print("Rank of the given Matrix is : ",rank)
On executing this program it will display the rank of the given matrix.
I hope this article gives you a basic understanding of Python libraries and their usage in a Python program. Now if you have a query write us in the comments below.