Working With Third-Party Libraries in Python (83/100 Days of Python)
Python is a popular programming language with a wide range of libraries that help developers to perform complex tasks easily. Libraries are pre-written codes that provide functionality to Python programmers. They save time and effort by eliminating the need to write code from scratch for every task.
Installing libraries in Python
Before we can work with a library, we need to install it. Python has a built-in package manager called pip that makes it easy to install libraries. To install a library using pip, open a terminal or command prompt and type the following command:
pip install <library-name>
For example, to install the NumPy library, we can run the following command:
pip install numpy
Importing libraries in Python
Once we have installed a library, we can use it in our Python code by importing it. To import a library in Python, we use the import
statement. For example, to import the NumPy library, we can use the following statement:
import numpy
This will import the entire NumPy library, which means that we can use all of its functions and classes in our code.
Alternatively, we can use an alias for the library name to make it easier to refer to the library in our code. For example, to import the NumPy library with an alias of np
, we can write:
import numpy as np
This will allow us to refer to the NumPy library as np
in our code:
print(np.abs(-3)) # 3
a = np.array([1, 3]) # array([1, 3])
print(a) # [1, 3]
Using functions from libraries in Python
After importing a library, we can use its functions in our code. To use a function from a library, we need to specify the library name, followed by the function name. For example, to use the sqrt()
function from the NumPy library to find the square root of a number, we can write the following code:
import numpy as np
x = 25
y = np.sqrt(x)
print(y)
This will print the square root of 25
, which is 5.0
.
Some Popular Libraries in Python
There are many popular libraries in Python that are widely used by developers for various purposes. Some of the most commonly used libraries and their applications are as follows:
- NumPy — NumPy is a library for scientific computing in Python. It provides support for multidimensional arrays and matrices, and a wide range of mathematical functions for performing operations on them. NumPy is widely used in data analysis, machine learning, and scientific computing.
- Pandas — Pandas is a library for data manipulation and analysis in Python. It provides powerful data structures for storing and manipulating tabular data, and a wide range of functions for cleaning, filtering, and transforming data. Pandas is widely used in data science and data analysis.
- Matplotlib — Matplotlib is a library for data visualization in Python. It provides a wide range of functions for creating charts, plots, and other visualizations from data. Matplotlib is widely used in data science and scientific computing.
- Scikit-learn — Scikit-learn is a library for machine learning in Python. It provides a wide range of algorithms for classification, regression, clustering, and dimensionality reduction, as well as tools for model selection and evaluation. Scikit-learn is widely used in machine learning and data science.
- PyTorch — PyTorch is a popular library for machine learning in Python. It provides a dynamic computational graph that allows developers to define and train complex neural networks. PyTorch is widely used in research and production for natural language processing, computer vision, and other applications.
- Pygame — Pygame is a library for game development in Python. It provides tools for creating and controlling graphics, sound, and user input, making it easy to build games and interactive applications. Pygame is widely used in game development and education.
- Scrapy — Scrapy is a library for web scraping and data extraction in Python. It provides a powerful framework for crawling websites and extracting data from them, including support for handling various data formats and web protocols. Scrapy is widely used in data scraping, data mining, and web automation.
These are just a few examples of popular libraries in Python. There are many other libraries available for various purposes, and new libraries are being developed all the time. By using these libraries, developers can save time and effort in their programming projects, and take advantage of powerful tools and functionality for data analysis, machine learning, game development, and more.
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course
- Full series: 100 Days of Python
- Previous topic: What are Packages in Python and What is the Role of __init__.py files?
- Next topic: Virtual Environments