Python Libraries – Foundational Notes-CLASS 12
Python Libraries – Foundational Notes-CLASS 12
💡 Instead of writing everything from scratch, we can import a library and use its features directly.
🔹 Installing a Library
To install a library, open the Command Prompt (Windows) or Terminal (Mac/Linux) and type:
🔸 Examples:
pip list
🔹 Importing a Library
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
Matplotlib, Scikit-
Library A collection of multiple packages designed for specific tasks. learn
🔹 Example to Understand:
Library Purpose
NumPy Works with arrays and mathematical calculations
Pandas Handles data in table format (rows & columns)
Matplotlib Helps in creating graphs and visualizations
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
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
Python, NumPy, and Pandas handle data types differently, so let's explore them in detail.
NumPy's types (int32, float64) are optimized for faster mathematical operations.
Pandas uses int64 and float64 because it often handles large datasets.
Numbers like int32, int64, float32, and float64 tell us how much memory a number takes in the computer.
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).