PYTHON · Learning guideTheory · Labs · Knowledge checks
Chapter 09

Databases, Tkinter, debugging and web basics

SQLite database access

sqlite3 is included with Python. Always use parameterized queries rather than string concatenation with untrusted input.

import sqlite3
with sqlite3.connect(":memory:") as db:
    db.execute("CREATE TABLE tasks (name TEXT)")
    db.execute("INSERT INTO tasks VALUES (?)", ("study",))
    print(db.execute("SELECT * FROM tasks").fetchall())
Hands-on lab: Insert and list three tasks.

Tkinter GUI

Tkinter is a standard GUI toolkit available with many Python distributions; some Linux installations require an additional OS package.

import tkinter as tk
root = tk.Tk()
root.title("Training")
tk.Label(root, text="Hello Python").pack(padx=20, pady=20)
root.mainloop()
Hands-on lab: Add a button that changes the label.

Debugging and testing

Use breakpoints, a debugger and assertions. Test normal cases and edge cases.

def double(x):
    return x * 2

assert double(3) == 6
breakpoint()
Hands-on lab: Step through a function in the debugger.

Python and the web

CGI scripts are historically relevant but deprecated and removed from Python 3.13. Modern web services typically use frameworks and WSGI/ASGI servers. Network scripting can use urllib or socket.

from urllib.request import urlopen
# Only fetch URLs you trust and can access.
# with urlopen("https://example.org", timeout=5) as r:
#     print(r.status)
Hands-on lab: Explain the difference between an HTTP client and a web server.

TOSA-style final assessment

Practice a short end-to-end project: read a CSV or text file, validate values, compute a summary, store results in SQLite, and present the results in the console or a small GUI. The supplied syllabus mentions TOSA certification; this guide is not an official TOSA exam.

Hands-on lab: Deliver a functioning program with functions, one class, exception handling and a short README.