Python (Fall 2026) Module 14-7: Using Classes in a Program
Watch on YouTube →
Overview
Bill Moseley demonstrates how Python classes and lists work together in a vehicle inventory program. The program stores car instances, lets users buy, list, and sell cars, uses numbered choices with `enumerate` and `pop`, and could preserve its inventory between runs by serializing the list with `pickle`.
Key takeaways
- A Python list can hold instances of a class, allowing a program to manage many objects of the same type in one collection.
- Instantiate each car before appending it to `liCars`; appending a make string alone does not provide the car object's methods or formatted output.
- For user-facing selections that start at 1, subtract 1 before using the choice as a Python list index.
- `list.pop(index)` both removes an item and returns it, making it useful for updating inventory and reporting which car was sold.
- Python's `pickle` can serialize a list of class instances so an inventory can be saved to a file and restored in a later run.
- The same object-list technique applies to game entities: a program can iterate through enemy objects and test whether any touch the player.
Chapters
- Build a menu for buying, selling, and listing cars, then store car objects in the `liCars` list.
- Instantiate a car with its make instead of appending a plain string, so the list contains objects with the car class's behavior.
- Loop through `liCars` to display each car; the class's string method supplies formatted details such as the Toyota's two doors.
- Display the inventory with `enumerate` so users can identify a car by a human-friendly number starting at 1.
- Convert the selected number to an integer and subtract 1 to match Python's zero-based list indexes.
- Remove the selected car with `liCars.pop(index)`; its return value can be printed to confirm which car was sold.
- Test the workflow with a Toyota and Honda already in the inventory, sell the Toyota, and verify that only the Honda remains.
- Add more menu actions, such as driving a selected car, using the same list of car instances.
- Use Python's `pickle` module to save the entire car list to a file and load it on a later run.
- Apply the same list-of-objects pattern to a video game, where enemy objects can be checked against a player object for collisions.
Transcript excerpt
0:02 So, how might I use this in a program? So, I'm going to create a little program here. Which will allow me to manage my fleet of vehicles. So, I'm going to say strMenu = input, buy a car,
0:55 sell a car, list cars. Okay, so there's three things in my menu choice. I'm going to make a choice here, and I'm going to say if strMenu == 1. If it equals one, I'm going to buy a car. So, what do I want to do? I want to ask strMake = input, what kind of car? And then I'm going to add that to the, to the list of cars. So, I'm going to say liCars.append, and I'm going to append the make that I just gave it. Okay. Elif strMenu, I'm going to jump to the menu option, which is list cars,
2:02 before I do sell a car, because that's going to be the, the last one that we do. So, I'm going to say, list the cars is for obCar in liCars. Print obCar. Okay, so let's just run this. I'm going to buy a car. Buy a car. What kind of car? Toyota. Okay. Oops. I didn't do the right thing. All I did was append the list, the, the make to that list. I actually need to create the car with the make as a, there we go. So, I'm actually instantiating a car. Before that, I was just adding the string to the list. Silly
2:57 me. All right. So, buy car. What clued me in is that it didn't say this, you bought a Toyota. So, let's buy another car. I'm going to buy a Ford. Now, if I want to list my cars, there's my list of cars. So, that's pretty easy. But, how do we sell a car? Because
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.