Unit I · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
Almost no useful Python program in pharmaceutical science is written entirely from scratch. Instead, programmers rely on libraries — pre-written, tested code that handles common tasks such as mathematical calculations, data analysis, or plotting a concentration-time curve. Understanding the difference between the libraries that ship with Python and the ones you must fetch separately, along with how to install and remove them safely, is a foundational skill for every topic that follows in this course, from NumPy arrays to Pandas dataframes. This article covers standard libraries, third-party libraries, and the pip-based workflow for installing and uninstalling them, as prescribed under BP101T.

What Is a Library?
In programming, a library is a ready-made bundle of code — functions, classes, and modules — that someone has already written to accomplish common tasks. Using a library means a pharmacy student writing a dosage calculator or a data-cleaning script can call on tested, working code instead of building every piece of functionality from first principles. Python libraries fall into two broad categories: standard libraries, which come bundled with the language, and third-party libraries, which must be obtained separately.
Standard Libraries
A standard library is a set of modules and packages that ships along with the Python language itself. The moment Python is installed on a computer, these modules are already present and ready to use, so there is no separate download or installation step required to access them — only an import statement.
Purpose of Standard Libraries
- Basic functionality: they give programmers ready access to fundamental operations needed in everyday coding.
- Less repetitive coding: they cut down on the need to rewrite common logic that almost every program requires.
- Stability and security: because they are part of the language distribution, they are built and tested to be dependable and safe to use.
- Core programming support: they underpin the essential tasks that most programs rely on.
Key Characteristics of Standard Libraries
Standard libraries are officially maintained by the developers of Python itself, are reliable and well-tested through extensive review, are available by default with every installation, are designed for general-purpose rather than niche needs, and remain stable over time so that existing code rarely breaks when Python is updated.
Examples of Python Standard Library Modules
Python ships with many built-in modules. The five most relevant to a first-year pharmacy student are described below, each demonstrated with a realistic use.
# The math module -- mathematical calculations
import math
print(math.sqrt(25)) # 5.0
print(math.pi) # 3.14159...
# The random module -- generating random values
import random
print(random.randint(1, 10)) # a random whole number between 1 and 10
# The datetime module -- handling dates and times
from datetime import datetime
print(datetime.now()) # the current date and time
# The os module -- interacting with the operating system
import os
print(os.getcwd()) # the current working directory
# The json module -- working with JSON-formatted data
import json
patient_record = json.dumps({"name": "Asha", "dose_mg": 500})
print(patient_record) # '{"name": "Asha", "dose_mg": 500}'
A pharmacy example that combines two of these: generating a mock batch expiry check using datetime, and simulating a random sample of tablet weights for a quality-control exercise using random.
import random
from datetime import datetime
sample_weights = [round(random.uniform(495, 505), 2) for _ in range(5)]
print("QC sample tablet weights (mg):", sample_weights)
print("Report generated on:", datetime.now())
Why Standard Libraries Matter
Standard libraries allow faster development because ready-made modules let programmers build applications more quickly, require no external installation since they come with the language, are secure and stable because they are officially maintained, and are widely documented, which makes them easy to learn and reference during self-study.
Third-Party Libraries
A third-party library is code written and maintained by developers, communities, or companies who are not part of the core team that builds the programming language itself. Because these libraries live outside Python’s own distribution, you must fetch and install them separately before you can use them — this is exactly the position a student is in before their first NumPy or Pandas exercise later in this course.
Key Features of Third-Party Libraries
They originate from outside developers or organisations rather than Python’s core team, they require installation using a package manager such as pip, they frequently offer more advanced or focused capabilities than what the standard library provides, and they tend to be revised and improved on a regular, independent schedule set by their own maintainers.
Examples of Python Third-Party Libraries
# NumPy -- scientific computing with arrays
import numpy as np
concentrations = np.array([2.1, 4.5, 6.8, 8.2])
# Pandas -- data analysis and manipulation
import pandas as pd
adr_data = pd.DataFrame({"Drug": ["Metformin"], "Reactions": [3]})
# Matplotlib -- data visualization
import matplotlib.pyplot as plt
# Requests -- sending web requests and fetching data
import requests
response = requests.get("https://example.com")
print(response.status_code)
Other well-known third-party libraries by application area include Django and Flask for web development, and TensorFlow and PyTorch for machine learning — libraries a pharmacy data-science elective might later introduce for predictive modelling of drug response.
Standard Library vs Third-Party Library
| Feature | Standard Library | Third-Party Library |
|---|---|---|
| Definition | Built-in modules packaged with Python | External code from outside developers/organisations |
| Installation | None required | Must be installed separately, e.g. with pip |
| Availability | Always available once Python is installed | Available only after download and installation |
| Reliability | Highly stable, officially maintained | Varies by community or company support |
| Scope | General-purpose functionality | Specialized or advanced functionality |
| Examples (Python) | math, os, datetime, json | numpy, pandas, requests, flask |
| Dependency Risk | Very low, since it is part of the core language | Can introduce dependency or compatibility issues |
pip install step: math, os, random, datetime, and json never need installing, while numpy, pandas, and matplotlib always do.Installing Libraries
Installing a library means adding an external software package into your programming environment so that its functions, classes, and modules become usable within your programs. Libraries are installed for several reasons: code reuse (relying on existing, tested code rather than writing every feature from the ground up), faster development, access to advanced features in areas like data analysis and artificial intelligence, reduced complexity, and improved reliability, since code that has already been tested by others is generally more dependable.
Package Managers and pip
A package manager is a tool that handles installing, updating, and removing libraries. In the Python ecosystem, this tool is called pip, short for “Python Package Installer.” Pip connects to an online repository named PyPI (the Python Package Index), where the vast majority of third-party Python libraries are hosted.
Basic Installation Process
When you install a library, the following sequence takes place: pip is invoked to carry out the installation; pip searches for the requested library on PyPI; the necessary files for that library are downloaded; any other libraries the package depends on (its dependencies) are installed as well; the downloaded files are stored in a system directory known as site-packages; and once stored, the library becomes available to be imported and used inside programs.
# General syntax
pip install library_name
# Example: installing NumPy
pip install numpy
Types of Installation
Beyond the default installation shown above, pip supports several other useful forms:
# Version-specific installation -- requests a particular version, not the newest
pip install numpy==1.23.9
# Upgrade installation -- replaces an already-installed library with its latest version
pip install --upgrade numpy
# Multiple installation -- installs several libraries in a single command
pip install numpy pandas matplotlib
# Batch installation -- installs everything listed in a requirements file
pip install -r requirements.txt
Batch installation is particularly useful for a pharmacy data-analysis project shared among classmates: everyone can recreate the identical set of libraries by running one command against a shared requirements.txt file, rather than installing each package by hand.
Virtual Environments
A virtual environment is a self-contained, isolated workspace created for an individual Python project. Working within virtual environments matters because they prevent version conflicts from arising between different projects, keep each project’s dependencies separate from the others, and ensure that a project can be moved or shared while remaining portable. Every virtual environment maintains its own independent set of installed libraries, separate from any other environment on the same machine.
Advantages and Limitations of Installing Libraries
Installing libraries saves time and effort, promotes code reuse, provides advanced functionality not found in the standard library, reduces programming errors through well-tested code, and improves overall productivity. The main limitations are that a working internet connection is generally necessary to download libraries, different libraries or projects may require incompatible versions of the same dependency, some installed libraries may eventually fall out of maintenance, and complications can arise from the web of dependencies a library relies on.
Uninstalling Libraries
Uninstalling a library is the process of removing an already-installed software package from a programming environment so that it can no longer be used in programs. This process permanently deletes the library’s files from the Python environment or the package manager’s environment.
Why Libraries Are Uninstalled
Common reasons include freeing storage space by removing packages that are no longer needed, removing unnecessary or redundant packages, fixing conflicts or errors caused by a problematic package, replacing an outdated version to make way for a newer one, and maintaining a clean, organised development environment.
Basic Uninstallation Syntax
In Python, libraries are removed using the same tool used to install them: pip.
# General syntax
pip uninstall library_name
# Example: removing NumPy
pip uninstall numpy
Types of Uninstallation
# Normal uninstallation -- pip asks for confirmation before proceeding
pip uninstall pandas
# Forced / automatic uninstallation -- the -y flag skips the confirmation prompt
pip uninstall -y numpy
# Multiple library uninstallation -- removes several libraries in one command
pip uninstall numpy pandas matplotlib
Checking Installed Libraries Before Uninstalling
Before removing a library, it is good practice to check which packages are currently installed. This can be done with either of the following commands:
pip list
pip freeze
Frequently Asked Questions
Do I need an internet connection to import a library I have already installed?
No. Once a library’s files are downloaded into the site-packages directory during installation, importing and using that library afterward works completely offline. An internet connection is only needed at the moment of installing, upgrading, or uninstalling a package, since those actions communicate with PyPI.
What is the difference between a module, a package, and a library?
A module is a single file containing code. A package is a library that has been organised and bundled for distribution, typically as a collection of related modules. A library is the general term for any collection of reusable code, and in everyday conversation “library” and “package” are often used interchangeably.
Why should I use a virtual environment instead of installing everything globally?
Installing every library globally on one machine means all projects share the exact same versions of every package. If one pharmacy data-analysis project needs an older version of Pandas while another needs the newest release, a single global installation cannot satisfy both. A virtual environment gives each project its own isolated set of libraries, avoiding this version conflict entirely.
Summary
Python code relies heavily on libraries: standard libraries such as math, os, random, datetime, and json ship with Python and need no installation, while third-party libraries such as NumPy, Pandas, Matplotlib, and Requests must be fetched separately using the package manager pip, which downloads them from the PyPI repository. Installing a library with pip install supports default, version-specific, upgrade, multiple, and batch forms, and virtual environments keep each project’s dependencies isolated from others on the same machine. Uninstalling with pip uninstall permanently removes a package’s files, and because libraries can depend on one another, removing one package can occasionally destabilise another that relies on it — a relationship worth checking with pip list or pip freeze before making changes.
References
- Pharmacy Council of India (PCI), B.Pharm Regulations, NEP 2020 – BP101T Syllabus (Basics of Python Programming for Pharmaceutical Sciences)
- Python Software Foundation, “The Python Standard Library” – docs.python.org/3/library/index.html
- Python Packaging Authority / PyPI, “Installing Packages” – packaging.python.org/en/latest/tutorials/installing-packages
- Python Package Index (PyPI) – pypi.org
