Python Libraries Overview | Python Course | Dataplexa

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]})
3.141592653589793 12.0 5 2.0 7 b [('s', 4), ('i', 4), ('p', 2)] defaultdict(<class 'list'>, {'key': [1]})
  • Full standard library reference: docs.python.org/3/library
  • pathlib.Path is the modern replacement for os.path — use it in new code.
  • collections alone is worth learning deeply — Counter, defaultdict, deque, and namedtuple solve 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")
Status : 200 URL : https://httpbin.org/get?name=dataplexa Posted : {'username': 'alice', 'score': 99}
  • Always pass timeout= — without it, a hanging server blocks your program forever.
  • raise_for_status() raises requests.HTTPError automatically 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, supports async/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))
product price sold 0 notebook 4.99 120 1 pen 1.50 300 2 desk 89.99 15 3 lamp 24.99 45 4 chair 149.99 22 Info: Rows: 5, Columns: ['product', 'price', 'sold'] Avg price: $54.29 Total revenue: $4768.25 Affordable items: product price sold notebook 4.99 120 pen 1.50 300 lamp 24.99 45 Top earners: product revenue chair 3299.78 desk 1349.85 lamp 1124.55 notebook 598.80 pen 450.00
  • pd.read_csv("file.csv") and pd.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. ]
Array : [10 20 30 40 50] Mean : 30.0 Std : 14.142135623730951 Sum : 150 ×2 : [20 40 60 80 100] >25 : [30 40 50] Det : -2.0 Inv : [[-2. 1. ] [ 1.5 -0.5]] [0. 0. 0. 0.] [0 2 4 6 8] [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")
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.
  • plotly creates 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}")
Accuracy : 1.0 Feature importances: [0.097 0.024 0.441 0.438] CV accuracy: 0.960 ± 0.027
  • 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

DomainLibraryInstall
HTTP / APIsrequests, httpxpip install requests
Data Analysispandaspip install pandas
Numerical Computingnumpypip install numpy
Visualisationmatplotlib, seaborn, plotlypip install matplotlib
Web Frameworksflask, django, fastapipip install flask
Machine Learningscikit-learnpip install scikit-learn
Deep Learningtensorflow, torchpip install tensorflow
Databasesqlalchemy, sqlite3 (built-in)pip install sqlalchemy
Testingpytest, unittest (built-in)pip install pytest
Web Scrapingbeautifulsoup4, scrapypip install beautifulsoup4
Asyncasyncio (built-in), httpxbuilt-in
CLI Toolsclick, 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?





NEXT UP
OOP Basics — Classes and Objects
Learn to define your own custom types using classes — the foundation of object-oriented programming and the structure of every large Python codebase.