Students learn about functions for sorting and counting data in tables, as well as extracting rows.
Lesson Goals |
Students will be able to…
|
Student-facing Lesson Goals |
|
Materials |
|
🔗Functions for Tables 20 minutes
Overview
Building on their understanding of Contracts, students will be introduced to functions for working with tables, including: sort
, count
, first-n-rows
and row-n
.
Launch
-
Open the Animals Starter File and click "Run".
-
In the Interactions Window on the right type
animals-table
and hit "Enter" to see the the table.-
What do you Notice?
-
Sample response: We only see the first 10 rows of the table and would have to click to see the remaining 22 rows.
-
The animals aren’t listed in any particular order.
-
What do you Wonder?
-
Sample response: Is it possible to alphabetize the list, or sort it in other ways?
-
If this is the first time your students are seeing a table in code.pyret.org (CPO), you may also want to acknowledge lines 7-10 of the Definitions Area, where animals-table
is defined along with the names of the 8 columns.
Investigate
Turn to Sorting and Summarizing Tables and complete the first section: Ordering a Table with sort
.
-
What did these
sort
expressions do? -
They took in the
animals-table
, thename
column, and true or false… producing a new table, sorted alphabetically (A-Z fortrue
and Z-A forfalse
) -
What does it mean that the Range of
sort
is Table? -
The function produces a new table.
-
Type
animals-table
and hit "Enter". Did sorting our animals change the value of the table? -
No. It made a new one.
Let’s review what we know about Contracts. Every Contract has three parts:
-
Name — the name of the function, which we type in whenever we want to use it
-
Domain — the type(s) of data we give to the function
-
Range — the type of data the function produces
Contracts are the instruction manual for functions.
Let’s explore the Contract for another function that works with Tables: count
Complete the second section of Sorting and Summarizing Tables: Summarizing a Column with count
.
-
How did the
count
function summarize a column? -
It made a new table, with rows for all the unique values in that column and a count of how many times each value appeared.
-
Is the summary produced by the
count
function useful when summarizing thepounds
column? Why or why not? -
Since almost every animal has a unique weight, the summary is almost as long as the original table!
The
count
function produces a frequency table, which summarizes the data by showing how often - how frequently - specific values appear in a dataset.
Frequency tables are a commonly used tool in statistics because they give us a quick sense of the distribution and can help us to identify patterns.
Complete the first two sections of Functions for Tables (continued) to explore some new table functions: row-n
and first-n-rows
.
The second input to row-n
is a Number, called the index of the Row. The index tells Pyret how many data Rows to count from the first one in the table.
If we want the second Row in the table, Pyret needs to count one Row down from the beginning of the table. That’s why the index for the second Row is 1
.
-
What index number do we need to give
row-n
for Pyret to return the first row of the Table? -
0. Because Pyret is already at the beginning of the table and doesn’t need to count down any rows!
-
What is the index of the fifth Row in the table?
-
The index of the fifth row is
4
-
Complete the last section of Functions for Tables (continued).
-
If time allows see if you can put everything we’ve learned today together to solve the challenge problems!
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, an Image and a Row.
name = "Denny"
age = 16
logo = star(50, "solid", "red")
pet = row-n(animals-table, 3)
-
What are the names given in each of these definitions?
-
name, age, logo and pet
-
What are the values given to each of these names?
-
The String "Denny", the Number 16, an Image of a solid red star, and the 4th Row of the Animals Table.
-
Take a moment to look at the first challenge problem and think. How you would describe the table that
first-n-rows
is taking in? -
It’s taking in a table of the animals that has been sorted from lightest to heaviest.
Common Misconceptions
Students are likely to think that sort
changes the table, when instead it produces a new, sorted table. Encourage students to say out loud what they think they will get if they type animals-table
after evaluating sort(animals-table, "name", true)
. By testing their hypothesis, students who are surprised at the outcome are much more likely to remember the difference later on.
Students often struggle to remember that Row indices start at zero! You’ll want to practice this a few times.
Synthesize
-
How is the function of
count
different fromsort
? -
sort
made a new table that was reordered. -
count
made a new table that summarized the data from a column of the original table! -
How does the function
first-n-rows
work? -
It makes a new smaller table with the number of rows you type into the expression.
-
What is important to remember when using the
row-n
function? -
We need to give the function a number that is one less than the row we want.
-
Do you have any questions about the functions or expressions you’ve worked with today?
-
Where have you seen tables summarizing counts in the real world?
-
How else do journalists and newscasters visualize summaries of data besides in tables?
-
Ideally someone will say bar charts!
🔗Composing with Circles of Evaluation 15 minutes
Overview
Students learn to work with more than one function at once, by way of Circles of Evaluation, a visual representation of the underlying structure.
Launch
What if we wanted to see the ten youngest animals?
-
How could the
first-n-rows
andsort
functions work together? -
What order should we use the functions in?
Investigate
One way to organize our thoughts is to diagram what we want to do, using the Circles of Evaluation.
The rules for Circles of Evaluation are simple:
(1) Every Circle of Evaluation must have a single function, written at the top.
(2) The arguments of the function are written left-to-right, in the middle of the Circle.
If we want to see the first ten animals, our diagram would look like this.
Which translates to the following code:
first-n-rows(animals-table, 10)
(3) Circles can contain other Circles!
If we want to see the ten youngest animals, our diagram would look like this.
If we wanted to get extra fancy and see the species count for the youngest ten animals, we could add another layer to our Circle of Evaluation.
Note: Values like Numbers, Strings, and Booleans are still written by themselves. It’s only when we want to use a function that we need to draw a Circle.
To convert a Circle of Evaluation into code:
-
We start at the outside and work our way in.
-
After each function we open a pair of parentheses, and then convert each argument inside the Circle.
-
We close each pair of parentheses as we finish with the arguments in its Circle of Evaluation.
This diagram would translate to the code that follows.
And this diagram would translate to the code that follows.
-
Turn to Matching Descriptions to Circles of Evaluation: sort, count, first-n-rows
-
Match each scenario to the corresponding Circle of Evaluation.
-
When you’re done, practice translating the Circles of Evaluation into code.
Confirm that students have correctly identified the matching Circles of Evaluation and have each correctly translated at least one Circle of Evaluation into code. (Note: There is one extra Circle of Evaluation.)
-
Now turn to Circles of Evaluation: Count, Sort, First-n-rows and practice drawing the Circles of Evaluation yourself.
-
Before moving on to the second question, be sure you’ve translated your Circle of Evaluation into code and tested it out in Animals Starter File.
Synthesize
-
What did you Notice?
-
What did you wonder?
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.