Chapter 2 · 6 levels
Lists, dictionaries and loops
Hold many values at once, look things up by key, and repeat work with loops and comprehensions, all on Harbor & Pine orders and stock.
StartOne price is a variable. A whole cart of prices is a list. A stock sheet where every product name maps to a count is a dictionary. And going through every order to add something up is a loop.
These three ideas are the foundation of pandas, the library you’ll use from chapter 4 onwards. A pandas column behaves a lot like a list, and a row behaves a lot like a dictionary. Learning them in plain Python first makes pandas feel natural instead of magical.
In this chapter you’ll slice a week of order counts, total a cart, update the stock sheet after a delivery, count orders by sales channel, and filter products with a single readable line. The boss level hands you a small batch of raw orders and asks for revenue by channel, which is exactly the kind of question you’ll answer with pandas later, done by hand.
Levels
- Update the stock sheet
from Debra Marshall, Warehouse Manager
dictionaries, keys values, get, update
- Filter products in one line
from Thomas Whitaker, Head of Merchandising
list comprehension, filtering, dictionaries
- Boss: revenue by channel, by hand
from Megan Collins, Head of Sales
list comprehension, dictionaries, nested data, accumulator