Students learn how to extract individual rows from a table, and columns from a row.

 Lesson Goals Students will be able to…​ Given a human-language request for a data display involving the entire Animals Dataset, break it down into parts and generate the display. Given a Table, use the `row-n` method to extract any Row from that table Given a Row, use the column lookups to extract the value of any column in the Row Student-facing Lesson Goals Let’s practice making data displays to answer our questions about the data and retrieving information from tables. Materials Preparation All students should log into code.pyret.org (CPO) and open their saved "Animals Starter File". If they don’t have the file, they can open a new one from Animals Starter File.
Glossary
data row

a structured piece of data in a dataset that typically reports all the information gathered about a given individual

method

a function that is only associated with an instance of a data type, which consumes inputs and produces an output based on that instance

## 🔗Row Lookups 30 minutes

### Overview

Students learn how to define values in Pyret, and practice by defining Numbers, Strings, and Images. They also learn how to define an individual row from a table in Pyret.

### Launch

Sometimes we have a value that we want to use again and again, and it makes sense to define a name for it. Every definition includes a name and a value. In the code below, we have definitions for a String, a Number and an Image.

``````name = "Flannery"
age = 16
logo = star(50, "solid", "red")``````
• What are the names given in each of these?

• name, age, and logo

• What are the values?

• the String "Flannery", the Number 16, and an Image of a solid red star

### Investigate

Remind students that examples of lookup questions include, “How many legs does Felix have?” or "What species is Sheba?"

• How many rows do we need to answer a Lookup question?

• Just one! Lookups can be answered just by finding the right row!

Make sure sure students understand we only need one row!

What code can we write, to let us define a single row? Tables have special functions associated with them, called Methods, which allow us to do all sorts of things with those tables. For example, we can get the first data row in a table by using the `.row-n` method: `animals-table.row-n(0)`

Don’t forget: data rows start at index zero!

• Open your saved Animals Starter File (or make a new Animals Starter File copy), and click “Run”.

• In the Interactions Area, use the `.row-n` method to get the second and third data rows.

• Find `.row-n` in your Contracts page. What is the Domain of `.row-n`? What is the Range?

• Its domain is Number. Its range is Row.

A table method is a special kind of function which always operates on a specific table. `A.row-n(0)` and `B.row-n(0)` will return different rows, if A and B are different tables.

How would you get the second row out of the animals table? The third?

The code below will define the first row from the animals table:

`sasha = animals-table.row-n(0)`

It’s often better to name our Row definitions according to the property we care about. In this case, the fact that this row is a cat is much more interesting than the fact that her name is Sasha:

`cat-row = animals-table.row-n(0)`

• In the Animals Dataset, there are subsets that we might want to analyze: dogs, cats, lizards, old animals, young ones, etc.

• Complete Defining Rows.

• When you’re done, open your saved Animals Starter File (or make a new Animals Starter File copy) and add these definitions after the definitions for `dog-row` and `cat-row`.

• The rows you added will be used later!

### Synthesize

• We named rows by a lot of different properties (e.g. - their species, sex, etc). What are some other properties of rows in this dataset that we could use?

• What are some properties of your dataset that you might want to define rows for?

## 🔗Column Lookups 25 minutes

### Overview

Students learn how to access a particular column from a row.

### Launch

We can also access columns of a Row, by using a Row Accessor. Row accessors start with a Row value, followed by square brackets, and the name of the column where the value can be found. Here are three examples that use row accessors to get at different columns from the first row in the animals-table:

``````animals-table.row-n(0)["name"]
animals-table.row-n(0)["age"]
animals-table.row-n(0)["fixed"]``````

And of course, we can use our defined name, substituting it in place of all the redundant code:

``````cat-row["name"]
cat-row["age"]
cat-row["fixed"]``````

### Synthesize

• Why is it important to be able to define individual rows?

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.