Skip to main content

Practice 3 — Working with Tables

Skills: 3​

Reading: the Notes sections of Class 7, Class 8, Class 9, and Class 10​

Define a Table Constant​

Have students define a table containing information about several national parks:

PARKS = table(
["name", "state", "established", "area-sq-miles", "visitors-2024"],
[
["Yellowstone", "Wyoming", 1872, 3472, 4500000],
["Yosemite", "California", 1890, 1168, 3900000],
["Grand Canyon", "Arizona", 1919, 1904, 6400000],
["Zion", "Utah", 1919, 229, 4700000],
["Rocky Mountain", "Colorado", 1915, 415, 4300000],
["Acadia", "Maine", 1916, 198, 2500000],
["Great Smoky Mountains", "Tennessee", 1934, 816, 12100000],
["Bryce Canyon", "Utah", 1928, 56, 2900000],
],
)

Understanding Table Structure​

Point out that tables have:

  • Columns (name, state, established, area-sq-miles, visitors-2024)
  • Rows (each park is one row)
  • Headers that describe what each column contains

Accessing Individual Values​

gActivity:** Show students vhow to extract specific data using row and column access:

# Get the name of the first park
PARKS.row(0)["name"]

# Get the number of visitors to Yosemite (row 1)
PARKS.row(1)["visitors-2024"]

Answering Questions with Data​

Question 1: "Which park was established first?"

  • Students should examine the 'established' column.

Question 2: "What's the total area of parks in Utah?"

  • Students need to identify Utah parks and add their areas.

Filtering Tables​

Explanation: Introduce the filter method to find rows that meet certain criteria.

Have students write filters to answer specific questions:

  • Parks established before 1920.
  • Parks with more than 4 million visitors.

Ordering and Sorting Tables​

# Sort parks by area (smallest to largest)
parks_by_area = PARKS.order_by("area-sq-miles", ascending=True)

As an exercise, now sort by number of visitors.

Check Understanding: "What does ascending=True mean in order_by?"

Wrap-Up​

  • "Which operation was most useful for answering questions about the data?"
  • "What happens if you filter a table and get zero results?"