Tuesday, 9/29
Choosing a data structure
Checkpoint 2 Due Sunday, 10/04
Open in a new tab
· press f for fullscreen, o for an overview
Lists and records
Open the notebook first load takes about 25 secondsThe notebook is loading. The first load takes about 25 seconds.
Or run it on your own machine, which is how this course works from the second session on.
- Download
02-lists-and-records.pyinto your project'snotebooks/folder. -
Run
uv run marimo editfrom the project, with no file name. You get a page listing every notebook in it.
uv run 02-lists-and-records.py runs the cells and exits without showing you
anything, which looks like nothing happened. No project yet?
uvx marimo edit opens whichever folder you are in.
·
Jupyter version, which
reads without being run but has no working controls.
Collections and APIs
Open the notebook first load takes about 25 secondsThe notebook is loading. The first load takes about 25 seconds.
Or run it on your own machine, which is how this course works from the second session on.
- Download
03-collections-and-apis.pyinto your project'snotebooks/folder. -
Run
uv run marimo editfrom the project, with no file name. You get a page listing every notebook in it.
uv run 03-collections-and-apis.py runs the cells and exits without showing you
anything, which looks like nothing happened. No project yet?
uvx marimo edit opens whichever folder you are in.
·
Jupyter version, which
reads without being run but has no working controls.
TL;DR#
Tonight the agent in VS Code got its rules from AGENTS.md, and we watched it install a package with uv, commit and push. Then records read by name, a table as a list of records, and two more collections: tuples and sets. Choosing a data structure and a first request to an API will come next Tuesday. The habit worth keeping: when something breaks, ask the agent in VS Code first. It can read your terminal and your files, which a chat window in the browser cannot.
What we covered#
AGENTS.md holds your project's rules for the agent. The agent in VS Code reads it with every request. When asked to install requests, it ran uv add requests and left pip alone, because the file says so. To confirm it was read, expand References under the agent's answer and find AGENTS.md in the list. marimo's panel does not read the file; the Custom Rules you pasted into marimo do the same job there.
An agent can act on your whole project. It can run commands in the terminal, edit files, commit and push. While you are learning, keep the default permissions and read each command before you allow it.
A short message costs tens of thousands of tokens. The agent sends its own instructions and your AGENTS.md along with every message, so even "hi" is large.
An if/elif chain stops at the first true branch. With total = 250, if total >= 100 is true, so the elif total >= 200 branch never runs. Put the larger threshold first. If no branch is true, a variable that only the branches set never exists, and the next line that uses it stops with NameError. Add a final else, or give the variable a starting value before the if.
Indentation decides what belongs to a loop. total = 0 inside the loop runs on every pass, so the total starts over each time. Put it above the loop to keep a running total.
Text compares character by character. max(["9.50", "16.75", "22.25"]) returns "9.50", because "9" comes after "1" and "2". Every character has a number, its Unicode code point, and text is compared by those numbers. Convert with float() before you compare values that should be numbers.
append adds one item; extend adds each item. order_lines.append(["stapler", "tape"]) adds a single item, which is itself a list, so the order has three lines. order_lines.extend(["stapler", "tape"]) adds two, for four.
Assigning to the loop variable leaves the list as it was. In for name in names:, name points at each item in turn. name = name.title() makes a new string and points name at it; the list still holds the old strings. To get the changed values, build a new list:
titled_names = []
for name in names:
titled_names.append(name.title())Some characters do not show when printed. len("hi\n") is 3, because the line break counts. Text read from a file often carries a trailing line break or spaces. .strip() removes spaces, tabs and line breaks from both ends.
A record is read by name. first_order["ShipCountry"] gives 'France'. A record has no positions, so first_order[0] stops with KeyError: 0, and keys are case sensitive, so first_order["freight"] stops too. A for loop over a record goes through its keys, .values() gives the values, and for key, value in first_order.items(): gives both.
A dictionary keeps the order its keys were added in. Python has guaranteed this since version 3.7. A loop over first_order visits OrderID first because it was written first.
The outermost bracket tells you what you have. orders starts with [, so it is a list; each item starts with {, so each is a record. A list of records is a table. type(orders) and type(orders[0]) confirm it.
.get() asks without stopping. goog_quote.get("Volume") returns None when the key is missing, and goog_quote.get("Volume", 0) returns 0. A missing key in one record then does not stop a loop over thousands of them.
A tuple is a list that cannot change. first_holding = ("GOOG", 100, 131.36) reads by position like a list, and first_holding[1] = 75 stops with TypeError. Because it cannot change, a tuple can be a dictionary key and a list cannot: {(12, 25): "Christmas"} works, and {[12, 25]: "Christmas"} stops with unhashable type: 'list'. A function that returns two values hands them back as a tuple; we will write functions next week.
A set holds each value once. set(client_trades) turns seven trades into four distinct tickers. a & b is what is in both, a - b is what is in a and not in b, and a | b is everything in either.
A tool worth bookmarking#
Python Tutor runs a few lines of Python one step at a time and draws each variable as it changes. We used it for the running total that kept starting over and for the loop that did not change the list. Paste the code, click Visualize Execution, and step through it.
Setting up the agent#
The Babson model in VS Code is set up in Your AI Tools and Babson Key, section Add the Babson model. When it still does not answer, email me the exact error message.
Going further#
Going further in a notebook is optional. Skip it and you lose nothing. In Mini Project 1 it is required, in any direction you choose, and it does not have to work.
Public links#
- Session page, where
03-collections-and-apis.pyis, under the 03 · Collections and APIs tab. The Downloads page does not have it - Mini Project 1, the full instructions and the starter notebook
- Your AI Tools and Babson Key, including
AGENTS.md - Python handbook, Collections, for lists, dictionaries, tuples and sets
Before next session#
Checkpoint 2 closes Sunday 10/04, and it now includes tonight's work:
- Finish notebook 2,
02-lists-and-records.py, including section 6, The Portfolio, and your One row is... sentence in section 5. - Work through sections 1 to 3 of notebook 3,
03-collections-and-apis.py: more on dictionaries, tuples and sets. Section 4 onward is next week's. - Commit
AGENTS.mdin the root folder of your repository, next toREADME.md. Copy the text from Your agent's rules, then ask the agent in VS Code to install therequestspackage, so that it appears in yourpyproject.toml. - Commit and push as you go. Nothing counts until it is on github.com.
Mini Project 1 is due Sunday 10/11. Choose a project and write your plan in words before you ask your agent anything. The instructions and the starter notebook are on the Mini Project 1 page.
In the handbook
Reference for this session. None of it is assigned.