Unit 2:   Defining Values and Functions

imageUnit 2Defining Values and Functions
Unit Overview

Students are introduced to the Animals dataset, practice making some charts of the data, and consider the kinds of questions that can be asked of a dataset. They also learn to define values, and to define functions using a structured approach to problem solving called the "Design Recipe". They then use these functions to filter the animals dataset.

English

add translation

Product Outcomes:
  • Students define several row values from the animals table

  • Students define several functions over rows from the animals table

Standards and Evidence Statements:

Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.

  • Data 3.1.1: Use computers to process information, find patterns, and test hypotheses about digitally processed information to gain insight and knowledge. [P4]

    • Computers are used in an iterative and interactive way when processing digital information to gain insight and knowledge.

    • Digital information can be filtered and cleaned by using computers to process information.

  • Data 3.1.3: Explain the insight and knowledge gained from digitally processed data by using appropriate visualizations, notations, and precise language.

    • Visualization tools and software can communicate information about data.

    • Tables, diagrams, and textual displays can be used in communicating insight and knowledge gained from data.

  • BS-DR.1: The student is able to translate a word problem into a Contract and Purpose Statement

    • given a word problem, identify the domain and range of a function

    • given a word problem, write a Purpose Statement (i.e. - rewrite the problem in their own words)

  • BS-DR.2: The student can derive test cases for a given contract and purpose statement

    • BS-PL.3: The student is able to use the syntax of the programming language to define values and functions

      • defining and retrieving values

      • writing test cases

      • defining and using functions

    Length: 90 Minutes

    Materials:
      Preparation:
      • Computer for each student (or pair), with access to the internet

      • Student workbooks, and something to write with

      Types

      Functions

      Values

      Number

      num-sqrt, num-sqr

      4, -1.2. 2/3

      String

      string-repeat, string-contains

      "hello" "91"

      Boolean

      true false

      Image

      triangle, circle, star, rectangle, ellipse, square, text, overlay

      imageimage

      Table

      .row-n



      The Animals Dataset

      Overview

      Learning Objectives

        Evidence Statementes

          Product Outcomes

            Materials

              Preparation

              • Computer for each student (or pair), with access to the internet

              • Student workbooks, and something to write with

              The Animals Dataset (Time 15 minutes)

              • The Animals DatasetNow, let’s take a look at a real dataset!
                • Open the Animals Spreadsheet in a new tab. Take a moment to look around. What do you think this table is for?

                • This is some data from an animal shelter, listing animals that have been adopted. We’ll be using this as an example throughout the course, but you’ll be applying what you learn to a dataset you choose as well.

                • Open up the Animals Dataset starter file in a new tab. Click "Connect to Google Drive" to sign into your Google account, and then click the "Save as" button. This will save a copy of the file into your own account, so that you can make changes and retrieve them later.

              • This file contains some new code that you haven’t seen before. As with our Shapes file, we have a few include lines which import useful libraries for our course. This time, we also include a library that lets us work with Google Sheets:  

              • On line 9, the load-spreadsheet function is used to load our animal shelter spreadsheet from Google Drive, and give that file a name: shelter-sheet. After that, we see the following code:   Just as you saw with our shapes example, this code defines a new table. This time, it’s called animals-table, and it’s loaded from our Google Sheet. You can see the names we are giving to each of the columns, called name, species, gender, age, fixed, legs, pounts and weeks. (We could use any names we want for these columns, but it’s always a good idea to pick names that make sense!)

                Have students look back at the column names in the Google Sheet, and in the load-table function. Point out that they refer to the same columns, even though they have different names!

              • Click "Run", and type animals-table into the Interactions Area to see what this table looks like.

                • How many columns does this table have?

                • For each column, is the data quantitative or categorical?

                • For each column, what datatype is being used? Numbers? Strings? Images? Booleans?

              • Turn to Page 9 in your Student Workbook, and fill in the table for Question 2.

              • You already know about the .row-n method, which consumes a number and produces a row from a table. But tables have other methods too! The .order-by method consumes a String (the name of the column you want to order) and a Boolean (true for ascending, false for descending). Try typing the following expressions into the Interactions Area. What do you get?  

              • What is the Domain of .order-by? What is the Range? Find the contract for this method in your contracts table, and make sure it makes sense!

              • Make at least one bar-chart and at least one pie-chart, using columns from the animals-table. What do you notice? What do you wonder?

                Have students share their noticings and wonderings. Ideally, at least one student will wonder if there’s a way to make charts using only a subset of the data!

              • You may already be wondering if there’s a way to make charts using only a subset of the rows in a table. For example: what if we want to make a bar chart showing only the ages of the cats at the shelter? To do this, we need to learn how to write functions of our own, which can tell us if an animal is a cat or not, fixed or not, and so on.

              Defining Values

              Overview

              Learning Objectives

              • Students learn about value definitions in Pyret

              Evidence Statementes

                Product Outcomes

                • Students define several row values from the animals table

                Materials

                  Preparation

                  • Computer for each student (or pair), with access to the internet

                  • Student workbooks, and something to write with

                  Defining Values (Time 10 minutes)

                  • Defining ValuesAs you’ve seen, Pyret allows us to define names for values using the = sign. In math, you’re probably used to seeing definitions like , which defines the name x to be the value 4. Pyret works the same way, and you’ve already seen two names defined in this file: shelter-sheet and animals-table. We generally write definitions on the left, in the Definitions Area. You can add your own definitions, for example:  

                    With your partner, take turns adding definitions to this file:

                    • Define a value with name food, whose value is a String representing your favorite food

                    • Define a value with name year, whose value is a Number representing the current year

                    • Define a value with name likes-cats, whose value is a Boolean that is true if you like cats and false if you don’t

                  • Each row of our animals-table represents a single animal in our shelter. We can use the row-n method to define values. Type the following lines of code into the Definitions Area and click "Run":   What happens when you evaluate animalA in the Interactions Area?

                    Define at least two additional values to be animals from the animals-table, called animalC and animalD.

                  Question Types

                  Overview

                  Learning Objectives

                  • Students learn about different categories of questions

                  Evidence Statementes

                    Product Outcomes

                      Materials

                        Preparation

                        Question Types (Time 20 minutes)

                        • Question TypesOnce we have a dataset, we can start answering questions! But how do you know what questions to ask? There’s an art to asking the right questions, and good Data Scientists think hard about what kind of questions can and can’t be answered.

                          Turn to Page 9 in your Student Workbook, and write down your own questions under section 3.

                          Have students brainstorm some questions they might ask of the animals table.

                        • Most questions can be broken down into one of three categories:

                          • Lookup questions - These can be answered simply by looking up a particular value in the table and reading it out. Once you find the value, you’re done! Examples of lookup questions might be "is Sunflower fixed?" or "How many legs does Felix have?"

                          • Compute questions - These can be answered by computing an answer across an entire row or column. Examples of computing questions might be "what is the heaviest animal?" or "What is the average age of animals at the shelter?"

                          • Analyze questions - These ones take the most work, because they require looking for patterns and trends across multiple rows or columns. Examples of analysis questions might be "Do cats tend to be adopted faster than dogs?" or "Are older animals heavier than young ones?"

                          Have students come up with lots of questions under each category.

                        • To the right of the questions you wrote on Page 9, fill in the the type of question you are asking. Is it a Lookup, Compute, or Analyze question?

                          Have students share their findings with the class. Allow time for discussion!

                        • Lookup questions are easy to answer: just find the right row and column, and read out the answer! You learned how to do lookups in Pyret in Unit 1. This time, let’s practice lookups using value definitions, where each row is defined separately.

                          Complete the activity on Page 10.

                        Defining Functions

                        Overview

                        Learning Objectives

                        • Students learn how to define functions using the Design Recipe

                        Evidence Statementes

                          Product Outcomes

                          • Students define several functions over rows from the animals table

                          Materials

                            Preparation

                              Defining Functions (Time 30 minutes)

                              • Defining FunctionsSuppose you work at the animal shelter, taking care of all the animals who live there. You want to make sure they’re healthy, happy, and find good homes. For each animal, you might want to ask certain questions:

                                • What kind of animal is it?

                                • Has it been fixed?

                                • When was it born?

                                • Is it a kitten?

                                Have students brainstorm additional questions!

                              • Lookups are easy, but they can get really repetitive!

                                In the Interactions Area, type the code that will look up if animalA is fixed or not. Then type the code to look up if animalB is fixed or not. Repeat for animalC and animalD. Suppose I wanted to do this for every animal in the table?

                                This seems really repetitive, doesn’t it? We keep typing the same thing over and over, but all that’s really changing is the animal. Wouldn’t it be great if Pyret had a function called is-fixed, that would do this for us?

                                Have a student act out the is-fixed function. You give them an animal, and they tell you what they would type to find out if it is fixed.

                                • What is the name of our function?

                                • What type of data did we have to give it?

                                • What type of data did it give us back?

                                Unfortunately, Pyret doesn’t have a function like this. But it has something even better: it lets us define new functions of our own!

                              • To build our own functions, we’ll use a series of steps called the Design Recipe. The Design Recipe is a way to think through the behavior of a function, to make sure we don’t make any mistakes with the animals that depend on us! The Design Recipe has three steps, and we’ll go through them together for our first function.

                                Turn to page Page 11 in your Student Workbook.

                              • The first thing we do is write a Contract for this function. You already know a lot about contracts: they tell us the Name, Domain and Range of the function. Our function tells us if an animal is fixed or not, so we’ll call it is-fixed. It consumes an animal (represented by a Row in our table), and look up the value in the fixed column. A Purpose Statement is just a description of what the function does:  

                                Be sure to check students’ contracts and purpose statements before having them move on!

                              • Examples are a way for us to tell the computer how our function should behave for a specific input. We can write as many examples as we want, but they must all be wrapped in an examples: block and an end statement. Examples start with the name of the function we’re writing, followed by an example input. Let’s use some two pets we defined earlier for our first example.  

                                Make sure students understand (1) that is-fixed came from the Name in our contract, (2) that sasha and fritz came from the Domain in our contract, that (3) animalA["fixed"] came from our purpose statement, and the label also came from the variable name in our contract.

                              • Why didn’t we just write true or false after the is? In programming, just like in any other class, it’s more important to show your work than to just write down the answer. This helps programmers organize their thinking, and can be a useful way of fixing bugs later on. (If you never wrote down what you were thinking, how will you know where you went wrong?)

                                • In the examples where we show our work, do you notice a pattern? Most of the code for these examples is exactly the same, and only a small bit is changing: animalA and animalB.

                                • Circle all of the parts in your example block that are changing.

                                • What does the stuff you circled represent? Are animalA and animalB years? Legs? No - they are animals! Let’s label them...

                              • After having written our examples, this part is easy! The part of the examples before is tells us how to begin. We start with the fun keyword (short for "function"), followed by the name of our function and a set of parentheses. This is exactly how all of our examples started, too. But instead of writing mittens, we’ll use the label that we gave it. Then we add a colon (:) in place of is, and continue to follow our examples, replacing anything we circled with the label. Finally, we finish with the end keyword.  

                              • Now that we’ve defined our function, we can click "Run" and actually use it!

                                After you’ve clicked run, try typing in the following expressions, and see what happens:  

                              • Our examples: block is a helpful way to check our work, so we don’t make mistakes. Suppose we made a mistake in our function definition, and accessed the wrong column:   When we click "Run", the computer will tell us that our examples don’t match the definition! It will literally check your work for you!

                              • For practice, try solving the word problem at the bottom of Page 11.

                              • So far, our functions have been pretty simple: they consume a row, and they produce one column from that row as-is. But supposed we want to compute with that value, to find out specifically whether or not an animal is a cat, or whether it’s young? Let’s walk through a more complex Design Recipe together, by turning to Page 12.

                              • Define a function called is-cat, which consumes a row from the animals-table and returns true if the animal is a cat.

                                • Is this a lookup, compute or analyze question?

                                • What is the name of this function? What are it’s Domain and Range?

                                • Is Sasha a cat? What did you do to get that answer?

                                Have students explain their thinking carefully, step-by-step. Repeat this with other animals.

                              • To find out if an animal is a cat, we look at the species column and check to see if that value is equal to "cat". This gives us out first example:  

                                Add a second example.

                                Have students share their examples. Point out that the code is the same for all examples, aside from the name of the animal being tested.

                              • Just we’ve done before, let’s look at our examples and circle the things that are change from one to the other.

                                • Do all our examples use is-cat?

                                • Do all our examples use the same inputs?

                                • Do all our examples look at the same column?

                                • Do all our examples compare that column value to "cat"?

                                What label should we use here?

                                Make sure students realize that the label is specified in the Domain.

                              • As before, we’ll use the pattern from our examples to come up with our definition.

                                • What is the function name?

                                • What is the name of the variable(s)?

                                • What do we do in the body in the function?

                                 

                              • Type this definition - and its examples! - into the Definitions Area, then click "Run" and try using it.

                              • For practice, try solving the word problem at the bottom of Page 12.

                              Filtering Tables

                              Overview

                              Learning Objectives

                                Evidence Statementes

                                  Product Outcomes

                                    Materials

                                      Preparation

                                      Filtering Tables (Time 10 minutes)

                                      • Filtering TablesYou already know about the .row-n and .order-by methods. But suppose you want to get a table of only cats? Or a table of only animals that have been fixed? Try typing these expressions into the Interactions Area. What do you get?  

                                        If time allows, ask students to explain what they think is going on.

                                      • Find the contract for .filter in your contracts page. The .filter method is taking in a function, calling it on every row in the table, and producing a new table with only the rows for which it returns true.

                                        Try using the gender function to filter. What happens?

                                        Notice that the Domain for .filter says that test must be a function (that’s the arrow), which consumes a Row and produces a Boolean. If the function we pass in produces anything else, we’ll get an error.

                                        If time allows: have them make a pie chart using a table of only cats, or a bar chart of only the animals that have been fixed.

                                      Closing

                                      Overview

                                      Learning Objectives

                                        Evidence Statementes

                                          Product Outcomes

                                            Materials

                                              Preparation

                                              Closing (Time 5 minutes)

                                              • ClosingCongratulations! You’ve explored the Animals dataset, chosen your own and begun to think critically about threats to validity and how questions and data shape one another. For the rest of this course, you’ll be learning new programming and Data Science skills, practicing them with the Animals dataset and then applying them to your own.

                                                Have students share which dataset they chose, and pick one question they’re looking at.

                                              Additional Exercises: