Summary
In this lecture I explain what you need to know for the upcoming midterm and demonstrate how to work with CSV files using a real whale‑sighting dataset. I start by outlining the exam format—multiple‑choice, fill‑in‑the‑blank, and a short functional code critique—covering chapters up to functions, with ten questions per difficulty level. I note logistics: alternating seat assignments, pencils required, and TA office hours Thursday 8:45‑10:45.
I then review file‑I/O best practices: always open files with a with block, strip invisible characters, and use the csv module. I walk through reading a small CSV, separating the header, converting string numbers to int or float, and storing each row as a sighting record. I show how enumerate gives both index and value, and I stress defining constants for column indices to avoid magic numbers. Common pitfalls—especially treating numeric fields as strings—are highlighted.
For analysis, I count occurrences of each species, plot a bar chart of species frequency, and create a latitude/longitude scatter plot with species‑specific colors using Matplotlib. I then write a filtered file, whales_only.csv, with csv.writer. Helper functions keep the code tidy, and I briefly mention that pandas can simplify these steps.
Next, I expand the dataset from 18 to 6,018 rows, revealing a different distribution (Bryde’s, southern right, and sperm whales dominate). This underscores that real‑world data is messy and requires validation and cleaning before any modeling. I demonstrate robust exception handling with try‑except blocks for FileNotFoundError, ValueError, and a generic Exception, explaining how each prevents the program from crashing while still allowing processing to continue.
Finally, I assign an exercise: write a function that reads a CSV, skips the header, tallies sightings per species, and returns a list of (species, count) tuples. The implementation is left for you to complete after class. The next steps are to practice the file‑I/O patterns, apply the error‑handling template to your own datasets, and submit the exercise before the next session.