Date – 08/03/2025
📌 Python Libraries – Foundational Notes
What is a Library?
A library in Python is a collection of pre-written modules and functions that help us perform specific tasks
quickly and efficiently.
💡 Instead of writing everything from scratch, we can import a library and use its features directly.
🔹 Why Do We Need Libraries?
They save time by providing ready-made functions.
They reduce errors, as they are well-tested.
They allow us to perform complex tasks easily (like handling large datasets, graphs, or mathematical
calculations).
How to Install a Library in Python?
Not all libraries are built into Python. Some need to be installed manually using pip (Python's package manager).
🔹 Installing a Library
To install a library, open the Command Prompt (Windows) or Terminal (Mac/Linux) and type:
pip install library_name
🔸 Examples:
To install NumPy → pip install numpy
To install Pandas → pip install pandas
To install Matplotlib → pip install matplotlib
🔹 Checking Installed Libraries
To check which libraries are already installed, use:
pip list
🔹 Importing a Library
Once installed, we need to import the library before using it:
import numpy as np # Importing NumPy
import pandas as pd # Importing Pandas
import matplotlib.pyplot as plt # Importing Matplotlib
Here, np, pd, and plt are short names (aliases) that make coding easier.
Difference Between Module, Package, and Library
Term Definition Example
Module A single Python file (.py) containing functions and code. math.py, random.py
Package A collection of related modules stored in a folder. numpy, pandas
Matplotlib, Scikit-
Library A collection of multiple packages designed for specific tasks. learn
🔹 Example to Understand:
Module: A single book 📖
Package: A bookshelf with multiple books 📚
Library: A full library with many bookshelves 🏛️
What are NumPy, Pandas, and Matplotlib?
Python has many libraries, but for data handling, the most important are:
Library Purpose
NumPy Works with arrays and mathematical calculations
Pandas Handles data in table format (rows & columns)
Matplotlib Helps in creating graphs and visualizations
Let's understand each in detail.
🔹 NumPy (Numerical Python)
Used for numerical computations and handling arrays.
Works faster than Python’s built-in lists.
Can perform matrix operations, sorting, and statistics.
📌 Example: Creating a NumPy array
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
🔹 Pandas (Panel Data)
Used for handling structured data (like an Excel spreadsheet).
Works with Series (1D data) and DataFrames (2D table-like data).
Allows filtering, sorting, and analyzing data easily.
📌 Example: Creating a Pandas DataFrame
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
print(df)
🔹 Output:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
🔹 Matplotlib (Data Visualization)
Used for drawing graphs, plots, and charts.
Helps in visualizing data trends.
📌 Example: Creating a simple line graph
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 50]
plt.plot(x, y, marker='o')
plt.title("Simple Line Graph")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
🔹 Output: A line graph with points marked.
Difference Between NumPy, Pandas, and Matplotlib
Feature NumPy � Pandas 📊 Matplotlib 📈
Purpose Works with numbers and arrays Works with tables (dataframes) Creates graphs & charts
Data Structure ndarray (multi-dimensional array) Series (1D), DataFrame (2D) Plots (line, bar, pie, etc.)
Common Uses Math operations, statistics, algebra Data analysis, filtering, sorting Data visualization
Example Use Matrix multiplication Reading Excel/CSV files Creating bar graphs
📌 Understanding Data Types in NumPy and Pandas
When working with large datasets, choosing the right data type is important because:
✅ It saves memory (efficient storage).
✅ It improves speed (faster calculations).
Python, NumPy, and Pandas handle data types differently, so let's explore them in detail.
Data Types in Python vs. NumPy vs. Pandas
Python Data Type NumPy Equivalent Pandas Equivalent Example Value
int (Integer) int32, int64 int64 10, 200, -5
float (Decimal) float32, float64 float64 3.14, 5.67, -2.5
str (String/Text) str_ (Fixed size) object (Flexible size) "Hello", "Python"
Python Data Type NumPy Equivalent Pandas Equivalent Example Value
bool (Boolean) bool_ bool True, False
complex (Complex Numbers) complex64, complex128 Not commonly used 3 + 4j, -1 + 2j
🔹 Why do NumPy and Pandas use their own types?
NumPy's types (int32, float64) are optimized for faster mathematical operations.
Pandas uses int64 and float64 because it often handles large datasets.
What do 32 and 64 mean in data types?
Numbers like int32, int64, float32, and float64 tell us how much memory a number takes in the computer.
32-bit (int32, float32) → Takes 4 bytes of memory.
o Can store smaller numbers and less precise decimals (good for saving memory).
64-bit (int64, float64) → Takes 8 bytes of memory.
o Can store very large numbers and more precise decimals (good for accuracy).
Example:
👉 int32 can store numbers up to 2 billion, but int64 can store much bigger numbers.
👉 float32 can store decimals, but float64 keeps them more precise (useful in calculations).
Which one to use?
Data Type Use When
int32 / float32 You need to save memory (games, mobile apps).
int64 / float64 You need large numbers or high accuracy (finance, science).
✅ Use 32-bit if you want speed and less memory usage.
✅ Use 64-bit if you need accuracy and big numbers.