Students learn how to extract individual rows from a table, and columns from a row.
Lesson Goals |
Students will be able to…
|
Student-facing Lesson Goals |
|
Materials |
|
🔗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 = "Denny"
age = 16
logo = star(50, "solid", "red")
-
What are the names given in each of these definitions?
-
name, age, and logo
-
-
What are the values given to each of these names?
-
The String "Denny", the Number 16, and an Image of a solid red star.
-
Investigate
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 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, which allow us to do all sorts of things.
-
For example, we can get the first data row in a table by using the
row-n
function:-
row-n(animals-table, 0)
-
Remember: 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
function to get the second and third data rows. -
Find
row-n
on the Contracts Page. If you’re working with a printed workbook, the contracts pages are included in the back.
-
What is its Domain? Its Range?
-
Its domain is Number. Its range is Row.
-
The code below will define the first row from the animals table:
sasha = row-n(animals-table, 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 = row-n(animals-table, 0)
-
How would you get the second row out of the animals table? The third?
-
row-n(animals-table, 1)
will get the second row -
row-n(animals-table, 2)
will get the third row
-
-
In the Animals Dataset, there are subsets that we might want to analyze: dogs, cats, lizards, old animals, young animals, fixed animals, 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
andcat-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:
row-n(animals-table, 0)["name"]
row-n(animals-table, 0)["age"]
row-n(animals-table, 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"]
Investigate
-
Complete the exercises on Lookup Questions.
-
Complete More Practice with Lookups
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, 1738598, 2031479, and 1501927). 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.