Lists
A list is a numbered row, and the numbering starts at 0
Items are reached by index, from 0. A slice takes a run of them.
When a script handles many things of one kind, they go in a list. Each item has a position, called its index, and the first index is zero, not one. Negative numbers count back from the end. A slice takes a run of items: from index one, up to but not including three. And a few operations cover most needs: how long is it, is this in it, add one to the end.
Containers · Going deeper
List, tuple, set, dict: choose by order, change and lookup
A tuple cannot change; a set holds each item once; a dict looks up by key.
Dictionaries
A dict finds a value by its key, not its position
Each key leads to one value. A missing key is a KeyError.
A list answers the question "which one?" by number. A dict answers it by name. This dict describes one server. Each key on the left leads to a value on the right, and a value can be anything, even a list. You fetch a value by giving its key in square brackets, and you can chain lookups. Ask for a key that is not there and you get a KeyError. The get method returns None instead.
Dictionaries · Going deeper
A list of dicts is a table
servers[1] is a row. 'port' picks the column.
Comprehensions
A comprehension filters and transforms in one line
What to keep, from where, and which ones.
Scripts constantly turn one list into another. A comprehension does it in a line with three parts: what to keep, where from, and which ones. The first record is production, so its name goes into the new list. So does the second. The third is a test server. The condition is false, and it is dropped.
Files
with opens a file and guarantees it is closed
with closes the file for you. Mode 'w' erases first.
Reading a file takes three lines. Open connects the name f to the file on disk. Looping over f hands you one line at a time, so even a huge log never has to fit in memory. And when the indented block ends, the with statement closes the file, whether or not something went wrong. One warning sits in the table below: opening an existing file for writing erases it first.
JSON and CSV files · Going deeper
json and csv do the parsing: never split the text by hand
json.load returns typed data. csv returns strings, every time.
Errors
An exception climbs the call stack until it is caught
An error raised deep down travels up to the nearest matching except.
Things go wrong at run time: a file is missing, a service does not answer. Python reports this by raising an exception. Here main calls load, and load calls open. The file does not exist, so open raises FileNotFoundError. The exception goes back to load. Nothing there catches it, so load is abandoned too. It reaches main, where the call sits inside a try block with a matching except. The error is caught, and the program carries on.
Errors · Going deeper · 1 of 2
Modules, pip and virtual environments
import what exists. pip install what does not. One venv per project.
try blocks · Going deeper · 2 of 2
try has four parts: except on error, else on success, finally always
except clauses are tried in order, so the specific class goes first.
Lesson complete
You finished “Python: data, files and errors”
Nicely done.