Overview
By the end of the last part, your study log could answer real questions. It totalled your minutes by subject, found your heaviest day, tracked your streak. But look closely at the code that does this work and you will keep seeing the same pattern:
for subject, minutes, day in sessions: if subject == wanted_subject: total += minutesThe names subject, minutes, day only exist because the loop header unpacks them. The data underneath is still a plain list, and the only thing that makes minutes mean “minutes” is that it happens to sit in position 1. Every function that touches a session has to know that subject is row[0], minutes is row[1], and the date is row[2]. That knowledge lives in your head, not in the data.
In this part, you will learn ways to put that knowledge into the data itself. You will give your data structure, so that the meaning of each field is clear from the way it is stored. You will learn to use dictionaries for named lookup, JSON for storing named data, and classes for defining your own record types.
The structure of this part falls into three groups of chapters, following the three new tools:
Dictionaries:
- Dictionaries: Key-Value Thinking introduces the core mechanics of dictionaries.
- Dictionaries for Counting and Grouping shows how to use dictionaries to build summaries by category.
- Nested Structures shows how to combine dictionaries and lists to represent more complex data.
Records and JSON:
- From Rows to Records walks through the process of giving the session data structure, and refactoring the code to use it.
- Reading and Writing JSON introduces the
jsonmodule for storing and loading named data in files.
Classes:
- Defining Your Own Types: Classes introduces the class syntax for defining your own record types.
- Adding Behavior: Methods shows how to add methods to your classes, so that they can do things as well as hold data.
- Objects Meet Files: Serialization shows how to write methods that convert your objects to and from JSON, so that you can save them in files.
The three groups are followed by the AI-collaboration chapter and project checkpoint:
- Critiquing Data Designs is the AI-collaboration chapter, where you will critique the data design of a small program written by an AI assistant.
- Refactoring the Study Log with Classes is the project checkpoint, where you will refactor your study log to use classes for its session records.
Finally, the recap and feedback chapter summarizes what was covered and asks for feedback.
Recommended Order of Work
- read the regular chapters in order, running the examples as you go,
- complete the project checkpoint in Refactoring the Study Log with Classes,
- and finish with the recap and feedback.
The three tools build on each other, so the order matters more than usual this part. Dictionaries make records natural; records make JSON natural; the limitations of loose dictionaries make classes natural. Read in sequence and each tool will feel like the answer to a question the previous one raised.