Recitation 5 -- Lists
Skills: 3
Reading: 5.1, 5.2
Objectives
- List literals
- Math operations on lists: max, sum, mean
- Transforming lists with map & lambda
I. Introduction (5 minutes)
Overview: Explain that students will learn to work with lists—ordered collections of data that can be extracted from table columns or created directly. They'll use built-in mathematical operations and transformations to analyze cafe sales data and other real-world scenarios.
II. Extracting Lists from Tables (15 minutes)
A. The Cafe Sales Dataset
Present students with this cafe tracking data:
cafe-data =
table: day, drinks-sold
row: "Mon", 45
row: "Tue", 30
row: "Wed", 55
row: "Thu", 40
row: "Fri", 60
end
Discussion: Explain that we often want to ask statistical questions like:
- What's the maximum number of drinks sold?
- What's the average sold per day?
- What's the total sold this week?
B. Extracting Columns as Lists
Show students how to extract data for analysis:
import math as M
import statistics as S
sales = cafe-data.get-column("drinks-sold")
Check Understanding: Ask, "What does the extracted list look like? How is it different from a table?"
C. Mathematical Operations on Lists
Demonstrate basic operations:
M.max(sales) # Find maximum sales day
M.min(sales) # Find minimum sales day
S.mean(sales) # Calculate average daily sales
M.sum(sales) # Calculate total weekly sales
III. Creating List Literals (10 minutes)
A. Direct List Creation
Show students how to create lists without tables:
test-scores = [list: 85, 92, 78, 96, 88, 91]
empty-list = [list: ]
names = [list: "Alice", "Bob", "Carol"]
Check Understanding: Ask, "What types of data can lists contain?"
B. Practice with Basic Operations
Have students work with their test scores:
highest = M.max(test-scores)
average = S.mean(test-scores)
total = M.sum(test-scores)
IV. Transforming Lists with Map (10 minutes)
Introduction to Map
Introduce the concept of transforming every element:
import lists as L
temps-celsius = [list: 0, 10, 20, 30, 40]
fun celsius-to-fahrenheit(c):
(c * 9/5) + 32
end
temps-fahrenheit = L.map(celsius-to-fahrenheit, temps-celsius)
Check Understanding: Ask, "What does map do to each element in the list?"
V. Combining Operations (15 minutes)
A. Working with Student Grade Data
Present a more complex scenario:
raw-scores = [list: 78, 85, 92, 67, 88, 94, 73, 81, 90, 86]
# Apply a 5-point curve to all scores
curved-scores = L.map(lam(score): score + 5 end, raw-scores)
letter-grades = L.map(lam(score):
if score >= 90: "A"
else if score >= 80: "B"
else if score >= 70: "C"
else if score >= 60: "D"
else: "F"
end
end, curved-scores)
Check Understanding: Ask, "How many students got each letter grade?"
B. Filtering and Distinct Operations
Introduce additional list operations (filter
and distinct
):
# Find unique letter grades assigned
unique-grades = L.distinct(letter-grades)
# Filter to find only passing scores
passing-scores = L.filter(lam(score): score >= 60 end, curved-scores)
# Count how many students passed
num-passing = passing-scores.length()
VI. Recap and Wrap-Up (5 minutes)
Key Points Discussion:
- Lists vs Tables: Lists are single-column, ordered collections
- Mathematical Operations: Built-in functions for statistical analysis
- Map Transformations: Apply functions to every element
- Filter Operations: Remove elements that don't meet criteria
- Distinct Operations: Remove duplicates to find unique values in a list
Reflection Questions:
- "When would you use a list instead of a table?"
- "What real-world data could you analyze using these list operations?"