Students choose their dataset, and begin to explore. In the process, they learn how to write their own definitions, first defining static values and then complete functions. They are also introduced to the Design Recipe: a structured approach to solving word problems and defining functions.
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.
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
given a Contract and a Purpose Statement, write multiple examples or test cases
identifying correct and incorrect test cases for a function, based on its contract
BS-PL.3: The student is able to use the syntax of the programming language to define values and functions
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. On line 12, you can see the names we are giving to each of the columns, called name, species, gender, age, fixed, legs, pounts and weeks.
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?
How could you get row for the animal named "Toggle"?
How could you get the age of the animal named "Toggle" from that row?
How could you get the species of the animal named "Fritz"?
How could you get the number of legs of the animal named "Mittens"?
Use the last four questions to review get-row and row-accessors (introduced in Unit 1) before proceeding. Review with the whole class.
Turn to Page 7 in your Student Workbook, and fill in the table in Question 2.
Are cats more popular than dogs? Do older animals take longer to get adopted? What are some questions someone might have about this dataset? Write down three questions you have on Page 7.
Defining Values
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
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 color, whose value is an Image drawn in your favorite color
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 get-row function from yesterday to define values. Type the following lines of code into the Definitions Area and click "Run":
What happens when you evaluate mittens in the Interactions Area?
Select two pets from the animals-table that you would most like to adopt, and two more than you would least like to adopt (don’t worry, those animals will find homes too!). What rows are they? Define values for each of them, using the pet’s names and Rows.
Defining Functions
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
Defining Functions(Time 40 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:
When was this animal born?
Has it been fixed?
Is it an adult?
Is it a puppy?
Have students brainstorm additional questions!
Suppose I want to know what year an animal was born. For each Row, I could subtract the age column from the current year. We know lots of functions in Pyret that can handle Numbers, Strings, Images and so on, but none that can handle animals! We need to build our own.
To build our own functions, we’ll use something 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 8 in your Student Workbook
Step 1: Contract and Purpose
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 the year an animal was born, consumes an animal (represented by a Row in our table), and produces a Number representing the year. A Purpose Statement is just a description of what the function does:
Step 2: Write Examples
You already know how to write examples for built-in functions, where we write the answer we expect to get back. Just as we did before, we 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.
While both examples here are correct, we want to use the second one that shows our work.
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) sasha["fixed"] came from our purpose statement, and the label also came from the variable name in our contract.
When testing functions we write ourselves, we don’t just want to put down the answers. We want to show our work, to make sure we have a clear sense for how the function will do it’s job. To find out mittens’ birth year, we had to access the age column and subtract it from the current year.
Make sure students see that the re-written examples are equivalent, and that the new code accurately represents what the students themselves did to seek out the values in the columns: access the age row, then subtract it from the current year.
When we show our work, we start to see patterns emerge. Most of the code for these examples is exactly the same, and only a small bit is changing: mittens and fritz.
Circle all of the parts in your example block that are changing.
What does the stuff you circled represent? Are mittens and fritz years? Legs? No - they are animals! Let’s label them...
Step 3: Define the Function
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 pet1, 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 had a typo in our function definition, and added instead of subtracted:
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!
Now that you’ve walked through the Design Recipe once, it’s time to get some practice! This time, you’ll have to write the Contract, Purpose Statement, and first example yourself!
Define a function called sentence, which consumes a Row of the animals-table and produces a String containing the animal’s name, the string " the ", and the species of the animal. (For example, "Nori the dog").
What kind of animal would you adopt? Is there a maximum or minimum age? Do you care if the animal has been fixed or not? Write a function called adopt, which consumes a Row of the animals-table and produces true if it’s an animal that you would adopt.
Choose Your Dataset
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
Choose Your Dataset(Time 25 minutes)
Choose Your DatasetThroughout this course, you’ll be analyzing this dataset and writing up your findings. As you learn new tools for data science, you’ll continue to refine this analysis, answering questions and raising new ones of your own!
Take 5 minutes to look through the following datasets, and choose one that interests you:
Or find your own dataset, and use this (Blank Starter file) for your project.
Once you’ve found a Starter file for a dataset that interests you, click "Save a Copy" and save the project to your own account.
Take 5 minutes to fill in your name, and complete Page 11.
Closing
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
Closing(Time 5 minutes)
Closing Building functions is a powerful technique, which you’ll use throughout the course. Today, you learned how to write functions that work on one row of a table at a time. In the next lesson, you’ll learn how to use those functions to loop over an entire table, letting us extend, filter, and sort our animals-table
Make sure to save your work. Hit the Save button in the top left. This will save your program in the code.pyret.org folder within your Google Drive.
If your students are working in pairs/groups, make sure that each student has access to a version of the program. The student who saved the program to their Google Drive can share their program with anyone by hitting the Publish button in the top left, choosing "Publish a new copy", then clicking the "Share Link" option. This will allow them to copy a link to the program, then send to their partners in an email/message.