
Python Course
Python Libraries Overview
Python's greatest strength is not just the language itself — it is the enormous ecosystem of libraries built around it. Whether you are building web applications, analysing data, automating tasks, or training machine learning models, there is almost certainly a battle-tested library that handles the heavy lifting for you.
This lesson gives you a working overview of the most important libraries across every major domain — what they do, when to use them, and just enough code to see them in action. Think of this as your map of the Python ecosystem before you dive deep into any one area.
The Standard Library — Built In, Always Available
Python ships with a large collection of modules called the standard library. These require no installation — just import and use. Always check the standard library first before reaching for a third-party package.
import os # file system, environment variables, paths
import sys # Python runtime info, argv
import math # mathematical functions and constants
import random # random number generation
import collections # Counter, defaultdict, deque, namedtuple
import itertools # tools for working with iterators
import functools # lru_cache, reduce, partial
import pathlib # modern object-oriented file paths
import shutil # copy, move, delete files and directories
import re # regular expressions
import json # JSON encode/decode
import logging # production-quality logging
# Quick examples
import math
print(math.pi) # 3.141592653589793
print(math.sqrt(144)) # 12.0
print(math.ceil(4.2)) # 5
print(math.log(100, 10)) # 2.0 — log base 10
import random
print(random.randint(1, 10)) # random int 1–10
print(random.choice(["a","b","c"])) # random item from list
random.shuffle([1,2,3,4,5]) # shuffle in-place
from collections import Counter, defaultdict
c = Counter("mississippi")
print(c.most_common(3)) # [('s', 4), ('i', 4), ('p', 2)]
d = defaultdict(list)
d["key"].append(1) # no KeyError — missing key gets default []
print(d) # defaultdict(<class 'list'>, {'key': [1]})- Full standard library reference: docs.python.org/3/library
pathlib.Pathis the modern replacement foros.path— use it in new code.collectionsalone is worth learning deeply —Counter,defaultdict,deque, andnamedtuplesolve common problems elegantly.
1. requests — HTTP for Humans
requests is the most downloaded Python package of all time. It makes sending HTTP requests so simple that the official Python docs recommend it over the built-in urllib.
Install: pip install requests | Use it for: calling REST APIs, downloading files, scraping web pages, OAuth authentication.
import requests
# GET request — fetch JSON from a public API
response = requests.get(
"https://httpbin.org/get",
params={"name": "dataplexa"},
timeout=10 # always set a timeout in production
)
response.raise_for_status() # raises exception on 4xx/5xx
print("Status :", response.status_code)
print("URL :", response.json()["url"])
# POST request — send JSON body
payload = {"username": "alice", "score": 99}
r = requests.post("https://httpbin.org/post", json=payload, timeout=10)
print("Posted :", r.json()["json"])
# Session — reuse connection, share headers/cookies
session = requests.Session()
session.headers.update({"Authorization": "Bearer my_token"})
# r2 = session.get("https://api.example.com/data")- Always pass
timeout=— without it, a hanging server blocks your program forever. raise_for_status()raisesrequests.HTTPErrorautomatically on 4xx/5xx responses.requests.Session()reuses the underlying TCP connection — faster for multiple calls to the same host.- For async HTTP, use
httpx— same API, supportsasync/await.
2. pandas — Data Analysis
pandas is the backbone of data analysis in Python. Its DataFrame is a two-dimensional table — like a spreadsheet in memory — with powerful tools to load, clean, filter, group, and summarise data.
Install: pip install pandas | Use it for: CSV/Excel files, data cleaning, aggregation, merging datasets, exploratory analysis.
import pandas as pd # pd is the universal alias
data = {
"product": ["notebook", "pen", "desk", "lamp", "chair"],
"price": [4.99, 1.50, 89.99, 24.99, 149.99],
"sold": [120, 300, 15, 45, 22]
}
df = pd.DataFrame(data)
print(df.head())
print("\nInfo:")
print(f" Rows: {len(df)}, Columns: {list(df.columns)}")
print(f" Avg price: ${df['price'].mean():.2f}")
# Computed column
df["revenue"] = df["price"] * df["sold"]
print(f" Total revenue: ${df['revenue'].sum():.2f}")
# Filter — items priced under $30
affordable = df[df["price"] < 30][["product", "price", "sold"]]
print("\nAffordable items:")
print(affordable.to_string(index=False))
# Sort by revenue descending
print("\nTop earners:")
print(df.sort_values("revenue", ascending=False)[["product","revenue"]].to_string(index=False))pd.read_csv("file.csv")andpd.read_excel("file.xlsx")load files in one line.df.head(),df.info(),df.describe()are the first three calls on any new dataset.df.groupby("column").agg({"value": "sum"})is the pandas equivalent of SQL GROUP BY.
3. NumPy — Numerical Computing
NumPy provides the ndarray — a fast, memory-efficient multi-dimensional array — and hundreds of mathematical operations that run at C speed. Nearly every scientific and ML library in Python is built on top of NumPy.
Install: pip install numpy | Use it for: matrix operations, linear algebra, statistical calculations, signal processing.
import numpy as np # np is the universal alias
arr = np.array([10, 20, 30, 40, 50])
print("Array :", arr)
print("Mean :", arr.mean())
print("Std :", arr.std())
print("Sum :", arr.sum())
# Vectorized math — no loop needed, runs at C speed
print("×2 :", arr * 2)
print(">25 :", arr[arr > 25]) # boolean indexing
# 2D array (matrix)
matrix = np.array([[1, 2], [3, 4]])
print("Det :", np.linalg.det(matrix)) # -2.0
print("Inv :\n", np.linalg.inv(matrix)) # matrix inverse
# Common array creation
print(np.zeros(4)) # [0. 0. 0. 0.]
print(np.arange(0, 10, 2)) # [0 2 4 6 8]
print(np.linspace(0,1,5)) # [0. 0.25 0.5 0.75 1. ]4. matplotlib — Data Visualisation
matplotlib is Python's foundational plotting library. It creates static charts and saves them as PNG, PDF, or SVG files.
Install: pip install matplotlib | Use it for: line plots, bar charts, histograms, scatter plots, subplots.
import matplotlib.pyplot as plt
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
revenue = [12000, 15400, 13800, 17200, 19500, 21000]
costs = [9000, 10200, 10500, 11800, 12400, 13500]
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
# Line chart
axes[0].plot(months, revenue, marker="o", color="#7c3aed", label="Revenue", linewidth=2)
axes[0].plot(months, costs, marker="s", color="#f97316", label="Costs", linewidth=2)
axes[0].set_title("Revenue vs Costs 2024")
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Bar chart
profit = [r - c for r, c in zip(revenue, costs)]
axes[1].bar(months, profit, color="#22c55e")
axes[1].set_title("Monthly Profit")
axes[1].set_ylabel("$")
plt.tight_layout()
plt.savefig("charts.png")
print("Charts saved to charts.png")plt.subplots(rows, cols)creates a grid of charts — the most common pattern in real analysis notebooks.seaborn(built on matplotlib) produces statistically-oriented charts with much less code.plotlycreates interactive charts that work in browsers and Jupyter notebooks.
5. Flask — Web Development
Flask is a lightweight web framework for building REST APIs and web applications. Minimal by design — you add only what you need.
Install: pip install flask | Use it for: REST APIs, web dashboards, backend services, prototyping.
# Save as app.py — run with: flask --app app run
from flask import Flask, jsonify, request, abort
app = Flask(__name__)
products = [
{"id": 1, "name": "notebook", "price": 4.99},
{"id": 2, "name": "pen", "price": 1.50},
{"id": 3, "name": "desk", "price": 89.99},
]
@app.route("/products", methods=["GET"])
def get_products():
return jsonify(products)
@app.route("/products/<int:pid>", methods=["GET"])
def get_product(pid):
match = next((p for p in products if p["id"] == pid), None)
if not match:
abort(404)
return jsonify(match)
@app.route("/products", methods=["POST"])
def add_product():
data = request.get_json()
data["id"] = max(p["id"] for p in products) + 1
products.append(data)
return jsonify(data), 201
# Run: flask --app app run
# GET http://127.0.0.1:5000/products
# GET http://127.0.0.1:5000/products/1
# POST http://127.0.0.1:5000/products {"name":"lamp","price":24.99}@app.route()maps a URL path to a Python function — each function is a view.- Django — full-stack framework with built-in admin panel, ORM, and auth system.
- FastAPI — modern async framework with automatic OpenAPI docs, type hints, and Pydantic validation.
6. scikit-learn — Machine Learning
scikit-learn is the standard library for classical machine learning — classification, regression, clustering, and model evaluation. Its consistent API means switching between algorithms takes one line.
Install: pip install scikit-learn | Use it for: training ML models, cross-validation, feature engineering, pipelines.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
iris = load_iris()
X, y = iris.data, iris.target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
preds = model.predict(X_test)
print("Accuracy :", accuracy_score(y_test, preds))
print("Feature importances:", model.feature_importances_.round(3))
# Cross-validation — more reliable than a single train/test split
cv_scores = cross_val_score(model, X, y, cv=5)
print(f"CV accuracy: {cv_scores.mean():.3f} ± {cv_scores.std():.3f}")- Every scikit-learn model follows the same API:
.fit(X, y)→.predict(X)→.score(X, y). - For deep learning — neural networks, images, NLP — use TensorFlow or PyTorch.
- Lesson 44 covers ML with Python in full detail.
Library Quick-Reference Map
| Domain | Library | Install |
|---|---|---|
| HTTP / APIs | requests, httpx | pip install requests |
| Data Analysis | pandas | pip install pandas |
| Numerical Computing | numpy | pip install numpy |
| Visualisation | matplotlib, seaborn, plotly | pip install matplotlib |
| Web Frameworks | flask, django, fastapi | pip install flask |
| Machine Learning | scikit-learn | pip install scikit-learn |
| Deep Learning | tensorflow, torch | pip install tensorflow |
| Database | sqlalchemy, sqlite3 (built-in) | pip install sqlalchemy |
| Testing | pytest, unittest (built-in) | pip install pytest |
| Web Scraping | beautifulsoup4, scrapy | pip install beautifulsoup4 |
| Async | asyncio (built-in), httpx | built-in |
| CLI Tools | click, argparse (built-in) | pip install click |
Practice
Which Python library is most commonly used for sending HTTP requests to REST APIs?
What is the universal alias used when importing pandas?
What is the core data structure that NumPy provides?
What three methods does every scikit-learn model share in its API?
Which standard library module provides Counter and defaultdict?
What requests method automatically raises an exception on 4xx/5xx HTTP responses?
Quick Quiz
Which library would you use to read a CSV file into a table-like structure for analysis?
What makes NumPy arrays faster than Python lists for mathematical operations?
Which web framework is lightweight and minimal, ideal for APIs and microservices?
What does response.raise_for_status() do in the requests library?
Which library would you choose for deep learning and neural networks instead of scikit-learn?
Which scikit-learn function gives a more reliable accuracy estimate than a single train/test split?