Python Libraries

A Python library is a collection of pre-written code that you can use to perform specific tasks without writing everything from scratch. Think of a library as a toolbox full of ready-made tools (functions, classes, and modules) that help you build programs more efficiently.

Why Use Libraries?

  • Save Time – Instead of writing everything yourself, you can use libraries that have already been written by experts.

  • Efficient & Readable– Libraries optimize tasks, making your code run faster and smoother.

  • Easy to Use – You just need to install and import a library to start using its functions.

Types of Python Libraries

  1. Built-in Libraries

  • math – Provides mathematical functions.

  • random – Helps in generating random numbers.

  • datetime – Handles date and time operations.

  1. External Libraries (Need to be installed separately)

  • numpy – For numerical and scientific computations.

  • pandas – For working with datasets.

  • matplotlib – For creating graphs and visualizations.

  • requests – For making HTTP requests to websites.

How to Use a Library?

  1. Import the library before using it

The format for calling a function from a library follows this structure:

  1. Install external libraries using pip(python package manager). It is a package manager for Python that helps you install, upgrade, and manage external python libraries.

Libraries can be installed by using the syntax above. Whether the library has been installed can be checked using the below syntax:

  • python calls the python interpreter

  • -c stands for command mode which allows executing a python command directly from the terminal of the system

  • import library_name imports the library

  • print(library_name.__version__) prints the installed version of the library

Difference between Module, Package and Libraries

  1. Module

A module is a single python file (.py) containing functions, classes or variables that you can import and use in another python program. A module is just one python file that contains reusable code.

  1. Package

A package is a collection of multiple modules stored in a directory. A package contains an __init__.py file (which can be empty) to tell python that the folder should be treated as a package.

Below example is the package structure (my_package)

Modules inside the package: Consider the below packages written together as different python files

  1. Library

A library is a collection of multiple packages and modules that provide extensive functionalities for different tasks. Libraries can be built-in or external. (Designed for specific functionalities)

Libraries for PMs

As PMs, having a basic understanding of python libraries can help in analyzing data, automate tasks and make data-driven decisions. The following libraries can be useful:

  • Pandas and NumPy are single-purpose libraries which are used for data processing, whereas matplotlib is a big library with many submodules and since we don't need the whole matplotlib package every time, we import only .pyplot

Last updated