Good place to stop if you're short on time — step 3 picks up here.
Lesson idea Tap to fold
What does pandas groupby() do?
Show the answer
It gathers rows into named groups so the same aggregation can run on each group.
Ask SQLite a real question and close its connection
The browser loads SQLite’s optional standard-library module on the first Run. connect(":memory:") creates a temporary database, not a cloud or local disk file. CREATE TABLE defines column names; these exercises supply valid rows with approved equal to integer zero or one. Parameter placeholders bind values separately from SQL text, including names containing quotes.
execute returns a cursor. fetchone returns one row tuple, so [0] reads the first column; fetchall returns a list of row tuples. COUNT(*) counts rows, WHERE filters rows and GROUP BY makes one group per model. SUM(approved) counts approvals for typed 0/1 flags and retains a zero-approved model; filtering away fails before grouping could hide that model. ORDER BY makes display order explicit.
Keep the supplied executemany table loader in these tasks while changing the query or its display. Behavioral checks substitute new typed rows, execute your actual SQL and include empty/zero-approved cases. Explicitly close the connection after reading the cursor. A SQLite connection context manager commits or rolls back transactions but does not itself close; the complete example uses contextlib.closing.
# Bind data values; close the in-memory connection.
import sqlite3
from contextlib import closing
with closing(sqlite3.connect(":memory:")) as db:
db.execute("CREATE TABLE evals (model, approved)")
db.executemany(
"INSERT INTO evals VALUES (?, ?)",
[("Canal", 1), ("Canal", 0), ("Museum", 0)],
)
query = (
"SELECT model, SUM(approved) FROM evals "
"GROUP BY model ORDER BY model"
)
for model, count in db.execute(query):
print(f"{model}: {count}")
Canal has one approved row, while Museum remains present with zero. The closing context releases the connection.
This browser database is temporary; the final report’s explicit download is how you keep a file. Values use placeholders, not string interpolation.
Predict, explain, then test
These three comparisons are self-reviewed, not scored. Coding checks run new inputs; the section quiz records separate knowledge evidence.
Read the tuple
Why use fetchone()[0] for SELECT COUNT(*)?
Compare your answer · self-reviewed
fetchone returns the result row tuple; [0] reads the count column.
Keep a zero group
Why SUM(approved) without first filtering to approved=1?
Compare your answer · self-reviewed
With typed 0/1 flags it counts passes and keeps a model whose rows all fail.
Close a resource
Does with sqlite3.connect(...) automatically close the connection?
Compare your answer · self-reviewed
No. That context manages transactions. Use close or contextlib.closing to release it.
Choose an exercise to load its prompt.
Loading exercises…
Noted on your route map, with the step you were on. Nothing is lost by parking it — the next review day will bring this idea back, and the flag tells the course where to slow down.
The tutor mounts here when JavaScript is available. The lesson above stays readable without it.