Libraries in Python

Last Updated : 3 Oct, 2026

Library is a group of modules that contain functions, classes and methods to perform common tasks like data manipulation, math operations, web scraping and more. Python libraries make coding faster, cleaner and more efficient by providing ready-to-use solutions for different domains such as data science, web development, machine learning and automation.

popular_external_python_libraries2
Popular External Python Libraries

Working of Python Library

Importing a library gives access to pre-written code stored in separate modules. In simple terms instead of writing the logic for a task, you import the library that already has it.

For example, most Python libraries consist of .py source files. Some libraries may also include compiled extension files such as .pyd or .dll on Windows and .so files on Linux/macOS for improved performance.

Example: Here Python's built-in random library is imported and used its randint() function to generate a random number between 1 and 10.

Python
import random
print(random.randint(1, 10))

Output
8

Types of Python Library

Python libraries are divided into two main types:

1. Built-in Python Standard Library

It is a collection of modules that come bundled with every Python installation, we don’t need to install anything separately. Most of these modules are written in C for better performance.

Examples of built-in modules:

  • math: Mathematical operations
  • os: Interact with the operating system
  • datetime: Date and time operations
  • random: Generate random numbers
  • json: Handles JSON data encoding and decoding.

External (third-party) libraries are not included with Python by default. You can install them easily using the pip package manager. Popular External Python Libraries:

  1. NumPy: A library for numerical computing that provides support for arrays, matrices, and mathematical operations.
  2. Pandas: A library for data analysis and manipulation using powerful data structures like DataFrame and Series.
  3. Matplotlib: It is a data visualization library that helps you create a wide variety of static, animated and interactive plots. It supports charts such as bar graphs, line charts, histograms and scatter plots.
  4. SciPy: A library for advanced scientific and mathematical computations built on top of NumPy.
  5. TensorFlow: It is an open-source machine learning and deep learning framework developed by Google. It allows developers to build and train neural networks for tasks such as image recognition, natural language processing and predictive modeling.
  6. Scikit-learn: It is a popular machine learning library built on top of NumPy and SciPy. It offers simple and efficient tools for classification, regression, clustering and dimensionality reduction.
  7. Scrapy: A library for web scraping and extracting structured data from websites.
  8. PyTorch: A deep learning framework with GPU support for building and training neural networks.
  9. PyGame: It is a cross-platform library used for developing 2D games and multimedia applications. It provides modules for handling graphics, sound and user input with ease.
  10. Keras: A high-level deep learning library that provides an easy interface for building neural networks.

Using Libraries in Python Programs

To use any library, it first needs to be imported into your Python program using the import statement. Once imported, you can directly call the functions or methods defined inside that library. You can import libraries in three main ways:

  1. Import the entire library: import library_name for example, import math
  2. Import a specific function or class: from library_name import function_name for example, from math import sqrt
  3. Import a library with an alias: import library_name as alias for example, import pandas as pd

Example 1: This program imports the entire math library and uses one of its functions.

Python
import math
A = 16
print(math.sqrt(A))

Output
4.0

Explanation:

  • math library is imported and math.sqrt() is used to calculate the square root of 16.
  • Since the full library is imported, we must prefix the function with the library name (math.).

Example 2: This program imports only selected functions from an external library to simplify usage.

Python
from numpy import array, mean
a = array([10, 20, 30, 40, 50])
print(mean(a))

Output
30.0

Explanation:

  • array() and mean() functions are imported from the NumPy library.
  • array() is used to create a NumPy array from a list and mean() calculates the average value of all elements in the array.
  • As these functions are imported directly, we don’t need to use the numpy. prefix each time we call them.
Comment