Lesson 3 — Keeping many things
Step 1: a row of values, reached by number
Real programs rarely keep one thing; they keep rows of things. A list is a row of values between [ ], separated by commas. Items are numbered from 0 — the first item is list[0] — and list[i] reaches one item. Here is a garden, kept as a list of its rows:
The book says it answers:
beans lettuce ["beans", "kale", "lettuce"]
Reaching a shelf inside the row works with the same = as any shelf: rows[1] = "kale" replaced the second item. Reaching past the end of a list is refused in plain words that say how many items there are — try say rows[9] in your own copy and read the refusal; it is your first, and it is kind.
Your turn. Keep a list of three colors. Say the last one — remember, the counting starts at 0, so the last of three is number 2.
The worked answer — try yours first, then peek
The book says it answers:
blue
Step 2: the toolbox
The toolbox is the set of tools a program calls by name, like functions that were always there. Each is handed values in parentheses and hands one value back. Hand a tool the wrong kind of value and it refuses in plain words, naming the tool and showing how it is called. You have already used two: length, which counts, and append, which adds one item to the end of a list:
The book says it answers:
4 ["beans", "collards", "lettuce", "scallions"]
Your turn. Start with the list [10, 20], append 30 to it, and say how many items it holds now.
The worked answer — try yours first, then peek
The book says it answers:
3
Step 3: text, taken apart and put together
Three tools do most of the everyday text work. sorted hands back a new list in order (the list it was given is left as it was). join speaks every item of a list and joins the spoken forms with a separator between them. split is join's way back — it cuts text at every separator and gives back the pieces:
The book says it answers:
["apples", "beans", "rice"] rice, beans, apples beans
Your turn. Cut the text "sun-moon-stars" at its dashes with split, and say the middle piece.
The worked answer — try yours first, then peek
The book says it answers:
moon
Two smaller tools finish the set. find gives the character number where a piece of text first appears, counting from 0 — or -1 when it is not there at all. part takes a piece of text: part(text, start, count):
The book says it answers:
3 -1 gar
Your turn. Use part to take the last two characters of "Tyler" — they start at character 3.
The worked answer — try yours first, then peek
The book says it answers:
er
Step 4: a card of facts
An object is the other shape of keeping: values with named fields, written between { }, each field a name, a colon, and a value. plant.name reaches a field, and a field can be given — or given for the first time — with the same dot:
The book says it answers:
yardlong beans
{ name: "yardlong beans", watered: false }
["name", "watered"]Fields keep the order they were first given, as the spoken form shows, and fieldnames lists them. A list answers "what stands in the row?"; an object answers "what do we know about this one thing?"
Your turn. Make an object for a dog: a name field and an age field. Say the dog's name, then give the dog a brand-new field, good, set to true, and say the whole object.
The worked answer — try yours first, then peek
The book says it answers:
Sadie
{ name: "Sadie", age: 4, good: true }The ledger grows: gifts with names
Now the amounts from Lesson 2 get their names. Each gift becomes an object — who it went to, how many cents — and the book becomes a list of those objects. A list of objects is how most real keeping is done:
The book says it answers:
Feeding America: 620 cents Barry Cunningham: 500 cents Feeding America: 1342 cents
The total is missing — on purpose. Adding gifts and totalling them is work the ledger will do again and again, and work done again and again deserves a tool of its own. That is the next lesson.
Your turn
Here are three tools out of order: "shovel", "hoe", "rake". Keep them in a list, print the list sorted, and then print the second tool in the sorted order.
The worked answer — try yours first, then peek
The book says it answers:
["hoe", "rake", "shovel"] rake
You can now…
- keep a row of values in a list, reach items by
number from 0, and replace them;
- grow a list with
appendand count it with
length;
- sort with
sorted, join withjoin, cut text with
split, search with find, and take pieces with part;
- keep a card of named facts in an object, reach and
add fields with the dot, and list them with fieldnames;
- weave the two together: a list of objects.
---