Unit 6: Real-world applications

ECE181 — Introduction To Python 7 min read

Python's reach extends far beyond scripting because a large ecosystem of specialised libraries lets a single language address vision, games, the web, numeric computing and communication. This unit surveys how general-purpose Python is applied to concrete problems, treating each library as a domain-specific toolbox layered on the same core syntax.

Defining characteristics of Python in applied settings:

  • Glue language role: Python orchestrates fast compiled backends (C/C++, CUDA) through thin bindings, so import cv2 calls native code while the developer writes readable Python.
  • Package ecosystem: Reusable functionality is installed via pip install <name> from the Python Package Index (PyPI); e.g. pip install opencv-python pygame requests matplotlib.
  • Interpreted, dynamically typed: Rapid prototyping without compilation; suited to exploratory work like data analysis and visualisation.
  • Array-centric computing: Most applied libraries interoperate through NumPy ndarray objects — an image, a dataset column and a game surface are all arrays.
  • Batteries-included plus third-party depth: Standard library covers files/networking; domain toolboxes cover vision, ML and plotting.

II. Real-World Applications

Where Python is actually deployed

A. Demonstration of real-world applications

Python appears across industry because one skill set transfers between domains.

  • Web and backend: Instagram and Spotify use Django/Flask frameworks to serve requests; a route maps a URL to a Python function returning a response.
  • Data science and ML: pandas, scikit-learn, TensorFlow power recommendation and forecasting; a model is model.fit(X, y) then model.predict(X_new).
  • Automation and scripting: File renaming, report generation, scheduled jobs using os, shutil, schedule.
  • Scientific computing: NumPy/SciPy for simulations; e.g. solving ODEs in physics.
  • Embedded and IoT: MicroPython on microcontrollers reading sensors.
  • Common thread: Each domain imports a specialised package but reuses identical control flow (for, if, functions), so learning one application shortens the path to the next.

III. OpenCV — Image Operations

Computer vision through the cv2 binding

OpenCV (Open Source Computer Vision, released 2000) treats an image as a NumPy array of pixel intensities, enabling reading, transforming and analysing visual data.

A. OpenCV-based image operations

The core workflow is read → process → display/save, with the image held as an array of shape (height, width, channels).

  • Loading and display:
    PYTHON
    import cv2
    img = cv2.imread("photo.jpg")   # BGR array, dtype uint8
    cv2.imshow("window", img)
    cv2.waitKey(0); cv2.destroyAllWindows()
    • BGR order: OpenCV stores channels Blue-Green-Red, not RGB — a frequent bug when mixing with Matplotlib.
  • Colour conversion: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) collapses 3 channels to 1, halving data for many algorithms.
  • Geometric transforms:
    • Resize: cv2.resize(img, (200, 100)) — width then height.
    • Rotate/crop: cropping is array slicing img[50:150, 30:200].
  • Filtering and edges:
    • Blur: cv2.GaussianBlur(img, (5,5), 0) smooths noise using a 5×5 kernel.
    • Edge detection: cv2.Canny(gray, 100, 200) returns a binary edge map from two intensity-gradient thresholds.
  • Drawing and thresholding: cv2.rectangle, cv2.threshold for binarisation.
  • Applications and limitations: Enables face detection, OCR pre-processing, medical imaging; limited by lighting sensitivity and the BGR/RGB confusion that corrupts colours if unconverted.

IV. PyGame — Game Creation

Interactive graphics via an SDL wrapper

PyGame wraps the SDL multimedia library to handle windows, drawing, sound and input, structured around a continuous game loop.

A. PyGame-based gaming creation

Every game repeats a loop that processes events, updates state and redraws the frame.

  • Initialisation and window:
    PYTHON
    import pygame
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
  • The game loop:
    PYTHON
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        screen.fill((0, 0, 0))           # RGB black
        pygame.draw.circle(screen, (255,0,0), (100,100), 20)
        pygame.display.flip()            # push frame to screen
        clock.tick(60)                   # cap at 60 FPS
    • Event handling: pygame.event.get() yields keyboard/mouse/quit events each frame.
    • flip() vs fill(): fill clears the back buffer; flip swaps it to the display, preventing flicker.
  • Sprites and collision: pygame.Rect objects support rect.colliderect(other) for hit detection.
  • Frame rate control: clock.tick(60) limits loops per second, making motion speed independent of CPU speed.
  • Applications and limitations: Ideal for 2-D games and teaching event-driven programming; not suited to 3-D or high-performance commercial titles.

V. Web Scraping — URL Access

Programmatic retrieval and parsing of web pages

Web scraping fetches HTML over HTTP then extracts structured data, automating what a human would copy manually.

A. Web-scraping-based url access

The pattern is request a URL → receive HTML → parse the tree → select elements.

  • Fetching a URL:
    PYTHON
    import requests
    resp = requests.get("https://example.com")
    print(resp.status_code)   # 200 means OK
    html = resp.text
    • Status codes: 200 success, 404 not found, 403 forbidden — always check before parsing.
  • Parsing HTML:
    PYTHON
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html, "html.parser")
    titles = [h.text for h in soup.find_all("h2")]
    • Selectors: find/find_all locate tags; soup.select("div.price") uses CSS selectors.
  • Navigating structure: The DOM is a tree; .text extracts content, ["href"] reads attributes.
  • Etiquette and legality:
    • robots.txt: Declares which paths a crawler may access; respect it.
    • Rate limiting: Insert time.sleep() between requests to avoid overloading the server.
  • Applications and limitations: Price monitoring, research datasets, news aggregation; breaks when site layout changes and may be blocked by JavaScript-rendered content requiring tools like Selenium.

VI. Advanced Toolboxes in Python

The scientific and machine-learning stack

Beyond single-domain libraries sit foundational toolboxes that other packages build upon.

A. Discussion on advanced toolboxes in python

These libraries provide numeric, tabular and learning capabilities reused across every applied field.

  • NumPy: N-dimensional arrays and vectorised math; np.mean(arr) runs in compiled code far faster than a Python loop.
  • pandas: Labelled tables (DataFrame); df.groupby("city")["sales"].sum() aggregates in one line.
  • SciPy: Optimisation, integration, signal processing built on NumPy arrays.
  • scikit-learn: Classical ML with a uniform fit/predict interface for regression, clustering, classification.
  • TensorFlow / PyTorch: Deep learning with automatic differentiation and GPU acceleration for neural networks.
  • Interoperability: All exchange NumPy arrays, so a pandas column feeds scikit-learn which feeds a Matplotlib chart without conversion glue.
  • Selection principle: Choose the highest-level toolbox that solves the problem — pandas for tabular analysis, scikit-learn before deep learning unless data scale demands it.

VII. Data Visualization

Turning arrays into graphics

Visualisation maps numeric data to visual properties (position, length, colour) so patterns become perceptible.

A. Data visualization

Matplotlib is the base plotting engine; higher-level tools wrap it for convenience.

  • Basic plot:
    PYTHON
    import matplotlib.pyplot as plt
    plt.plot([1,2,3,4], [1,4,9,16])
    plt.xlabel("x"); plt.ylabel("y = x²")
    plt.title("Quadratic"); plt.show()
  • Chart types and their use:
    • Line plot: trends over a continuous axis (time series).
    • Bar chart: comparison across discrete categories.
    • Histogram: plt.hist(data, bins=20) shows distribution shape.
    • Scatter: relationship between two variables, revealing correlation.
  • Higher-level libraries:
    • Seaborn: statistical plots with sensible defaults, e.g. sns.heatmap(corr).
    • Plotly: interactive, zoomable charts for dashboards.
  • Design principles: Label axes with units, choose the chart that matches the data type, avoid misleading truncated axes.
  • Applications and limitations: Exploratory analysis and reporting; a poorly chosen chart can distort the same data it aims to clarify.

VIII. Storytelling

Communicating insight, not just plotting

Data storytelling combines data, visuals and narrative so an audience reaches a decision, going beyond raw charts.

A. Storytelling

The goal is to lead a viewer from context to insight to action.

  • Three ingredients:
    • Data: the verified evidence underlying every claim.
    • Visuals: charts chosen to highlight the specific finding.
    • Narrative: ordered explanation connecting the visuals to a conclusion.
  • Structure: Context → conflict/finding → resolution, mirroring narrative arcs so the audience follows a reasoned path.
  • Techniques:
    • Focus attention: grey out background series, colour the key line, annotate the critical point directly on the chart.
    • Progressive disclosure: reveal one insight per slide rather than a dense dashboard at once.
    • Plain labelling: replace jargon axis titles with the question being answered.
  • 1. Exploratory vs 2. Explanatory:
    • Exploratory: analyst-facing, many charts, searching for what matters — messy is acceptable.
    • Explanatory: audience-facing, few polished visuals conveying the one finding already identified — every element serves the message.
  • Tools: Jupyter notebooks weave code, output and markdown prose into a single reproducible narrative document.
  • Significance: A technically correct analysis fails if its conclusion is not communicated; storytelling is the step that converts computation into decisions.