Unit 1:   Introduction to Pyret

imageUnit 1Introduction to Pyret
Unit Overview

Students are introduced to Tables in Pyret, and learn about Numbers, Strings, Types, Operations, Contracts, and Function Application.

English

add translation

Product Outcomes:
  • Students add rows to a Pyret table

  • Students add columns to a Pyret 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.

  • 6.SP.1-3: The student develops an understanding of statistical variability.

    • Recognize a statistical question as one that anticipates variability in the data related to the question and accounts for it in the answers

  • BS-PL.1: The student is familiar with declaring values and applying built-in functions using the programming language

    • representing (numeric, string, boolean, image, etc) values in the programming language

    • interpreting a function application and identifying its arguments

  • BS-PL.2: The student is comfortable using and writing Contracts for built-in functions

    • representing a function’s input and output using a contract

    • using a function by refering to its contract

Length: 90 Minutes
Glossary:
  • arguments: the inputs to a function; expressions for arguments follow the name of a function

  • categorical data: kind of data with a value that has a fixed number of possible values

  • contract: a statement of the name, domain, and range of a function

  • data row: an individual, structured piece of data in a dataset

  • data science: The study of using data to answer questions about the world

  • definitions area: the text box in the Editor, where definitions for values and functions are written

  • domain: the type of data that a function expects

  • editor: software in which you can write and evaluate code

  • error message: information from the computer about errors in code

  • header: the titles of each column of a table, usually shown at the top

  • interactions area: the text box in the Editor, where we enter expressions to evaluate

  • method: a function that is only associated with an instance of a datatype, which consumes inputs and produces an output based on that instance

  • programming language: a set of rules for writing code that a computer can evaluate

  • quantitative data: data with values that measure some amount or quantity; may not have a fixed number of possible values

  • range: the type of data that a function produces

  • table: A data structure that stores data as rows, with entries in particular columns

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

    • Student workbooks, and something to write with

    Types

    Functions

    Values



    Introduction

    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

            Introduction (Time 10 minutes)

            • IntroductionTake a minute to look at the opening questions we have prepared for you, and choose a topic that interests you.
              • Once you’ve selected your topic, choose a question you’d like to answer.

              • Spend one minute discussing your answer, and explaining why you answered the way you did. Do other students agree with you?

              • What could you measure to determine if your answer is right or not?

              Have students work in groups (no larger than 4), with each group choosing an Opening Question (or writing their own). After they’ve had time to discuss, have a few students share back what they talked about.

            • What’s the greatest movie of all time? Is Climate Change real? Who is the best quarterback? Is Stop-and-Frisk racially biased? These questions quickly turn into a discussion about data - how you measure it and how you interpret the results. In this course, you’ll learn how to use data to ask and answer questions like this. The process of learning from data is called Data Science. Data science techniques are used by scientists, business people, politicians, sports analysts, and hundreds of other different fields to ask and answer questions about data.

              You can motivate relevance of data science by using additional examples that relate to student interests. Here are a few:

            • We’ll use a programming language to investigate these questions. Just like any human language, programming languages have their own vocabulary and grammar that you will need to learn. The language you’ll be learning for data science is called Pyret.

              Set expectations for the class. This course is an introduction data science, so some questions will be out of reach!

            • Open up the Shapes Table starter file. 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.

              Each student (or pair of students) should have a Google Account.

            • image This screen is called the editor, and it looks something like the diagram you see here. There are a few buttons at the top, but most of the screen is taken up by two large boxes: the Definitions Area on the left and the Interactions Area on the right.

              For now, we will only be writing programs in the Interactions area.

              The Definitions Area is where programmers define values and functions that they want to keep, while the Interactions Area allows them to experiment with those values and functions. This is like writing function definitions on a blackboard, and having student use those functions to compute answers on scrap paper.

            • When you click "Run", Pyret reads what’s written in the Definitions Area on the left, and allows us to use those definitions on the right. The first lines of code on in the Definitions Area load our Data Science library, which has some useful code that will help us in the course, as well as some libraries for working with Tables and making Images. The rest of the program defines a Table called shapes.

            • Now that we’ve clicked "Run", we can play with our shapes Table. Type shapes into the Interactions Area and hit Enter...

            Exploring Tables

            Overview

            Learning Objectives

              Evidence Statementes

                Product Outcomes

                • Students add rows to a Pyret table

                Materials

                  Preparation

                    Exploring Tables (Time 10 minutes)

                    • Exploring TablesWhat comes back is called a Table. Pyret allows us to define names for values, and in this case the name shapes has been defined as the table you see here. Every table has as header, which names each column in the table. The shapes table has two columns: the name of the shape, and the number of corners.

                      name

                      corners

                      triangle

                      3

                      square

                      4

                    • After the header, tables can have data rows. Each data row has values for every column (nothing can be left empty!). A table can have any number of data rows, including zero:

                      name

                      corners

                    • It’s important to remember that tables are only an approximation of the real thing: this table describes some shapes we’ve observed, but obviously it isn’t the shapes themselves!

                      Add rows to this table for circle, ellipse, star, and rectangle. Pay close attention to how you use commas and colons, since these are part of the program! When you’re done, click "Run" and print out your new-and-improved shapes table.

                      You may need to walk through these carefully with students, pointing out where the punctuation marks are so that they’re aware of them before they start coding.

                    • Before we dive into all of the cool things you can do with tables, we need to understand the two different kinds of data that come up in Data Science: Categorical and Quantitative. Quantitative Data is used to measure an amount of something, or to compare two pieces of data to see which is less or more. If we want to ask "how much" or "which is most", we’re talking about Quantitative Data.

                      "Who is the tallest student?" is an example of a question that is answered with Quantitative Data. What are some other questions you can come up with?

                      Have students come up with columns that are quantitative (height, age, wealth, etc...)

                    • Categorical Data is used to describe different categories. Categories don’t make sense to compare or measure - if we had a column for eye color, for example, we couldn’t ask if "blue" is more than "brown". We use Categorical Data to ask "which one"? When you look at a whether forecast, temperature is quantitative but whether it’s snowing or raining is categorical.

                      For the two columns in the shapes table, which is categorical? Which is quantitative?

                      Have students come up with examples of columns that are categorical (gender, race, diet, etc...).

                    • It doesn’t make sense to ask whether "square is more than triangle", so most of the time we’d use the name column as categorical data. But if we wanted to sort the table in alphabetical order, suddenly we do care whether square comes before triangle.

                      For each of the following questions, determine whether the data being used is quantitative or categorical.

                      • We’d like to sort a phone book by the "last name" column.

                      • We’d like to find out which car is the most expensive.

                      • We’d like to find out which cars are red.

                      • We’d like to find out which puppy is the youngest.

                      • We’d like to find out which kitten is a Tabby.

                      • We want to know which people have a ZIP code of 02907.

                      The big idea here is that some data can be both categorical and quantitative – what matters is how we use it!

                    Values and Operators

                    Overview

                    Learning Objectives

                    • Students learn about different types of values, and operators on those values.

                    Evidence Statementes

                      Product Outcomes

                      • Students add columns to a Pyret table

                      Materials

                        Preparation

                          Values and Operators (Time 20 minutes)

                          • Values and OperatorsPyret lets us use many different kinds of data. In this table, for example, you can see Numbers (the number of corners) and Strings (the name of the shape). Let’s get some practice playing with both Datatypes.

                            With your partner(s), go through the questions on Page 3. Talk about the answers to each question, and write down your answers when required.

                            Give students time to experiment, and then debrief as a group.

                          • By now you’ve discovered a number of important things about our programming language:

                            • Numbers and Strings evaluate to themselves.

                            • Anything in quotes is a String, even something like "42".

                            • Strings must have quotation marks on both sides.

                            • Operators like +, -, *, and / need spaces around them.

                            • Any time there is more than one operator being used, Pyret requires that you use parentheses.

                            • Types matter! We can add two Numbers or two Strings to one another, but we can’t add the Number 4 to the String "hello".

                          • You’ve also seen a few error messages here. Error messages are a way for Pyret to tell you what went wrong, and are a really helpful way of finding mistakes! You’ve seen errors for missing spaces around operators, missing quotation marks, and mismatched operators without parentheses. What other errors do you think there are?

                            • In 6 / 0 we know that you can’t divide any number by 0! In this case, Pyret obeys the same rules as humans, and gives an error.

                            • An unclosed quotation mark is a problem, but so is an unmatched paren. (2 + 2, for example, will give you an error too!

                          • As you’ve seen, operators like + and - behave exactly the way in Pyret that they do in math class: they add and subtract Numbers, and produce new Numbers! But what about operators like < and >?

                            • To sort the table by age, we need to know if one person’s age is less than someone else’s.

                            • To filter the table to show only young people, we need to know if one person’s age is less than 25.

                          • Those come in handy when comparing quantitative data, so how do they work in Pyret?

                            With your partner(s), complete Page 4. Talk about the answers to each question, and write down your answers when required.

                            Have students share back. Point out that all the same rules about parentheses, spaces, and types still applies!

                          • By using and and or, we can combine tests. For example, we might want to ask if a character in a videogame has run out of health points and if they have any more lives. We might want to know if someone’s ZIP Code puts them in Texas or New Mexico. When you go out to eat at a restaurant, you might ask what items on the menu have meat and cheese. We’ll use these Boolean operators in a lot of our Data Science work later on.

                            Have students play "true or false", in which they stand if you say something true, and sit if you say something false. Start simple ("I am wearing a hat"), and gradually get complex ("I am wearing a hat, and I am standing on one leg").

                          • You’ve already gotten some practice adding rows to the table. But what if we want to add a column, to track whether or not a shape has any corners or not? Which shapes have corners and which don’t?

                            Look closely at the pattern for the columns that are already in the table. Once you see the pattern, add a new column called is-round, which has Booleans for each row indicating whether or not that shape has corners. Is this column made up of qualitative or categorical data?

                          Applying Functions

                          Overview

                          Learning Objectives

                          • Students learn about Contracts, and how they are used in function applications

                          Evidence Statementes

                            Product Outcomes

                              Materials

                                Preparation

                                  Applying Functions (Time 35 minutes)

                                  • Applying FunctionsSo now you know about Numbers, Strings, Booleans and Operators - all of which behave just like they do in math. But what about functions? You may remember functions from algebra: .

                                    • What is the name of this function?

                                    • What will the expression evaluate to? ?

                                    • The values that we give to a function are called its arguments. How many arguments does expect?

                                    "Arguments" are the values passed into a function. This is subtley different from variables, which are the placeholders that get replaced with those values!

                                  • Pyret has lots of built-in functions, which we can use to write more interesting programs. They also work pretty much the same way they do in algebra! Let’s explore one of Pyret’s function, called num-sqrt. Type this line of code into the interactions area and hit Enter.  

                                    • What is the name of this function?

                                    • What did the expression num-sqrt(16) evaluate to?

                                    • Does the num-sqrt function produce Numbers? Strings? Booleans?

                                    • How many arguments does num-sqrt expect?

                                  • Of course, functions on a computer can do a lot more than make Numbers! Type this line of code into the interactions area and hit Enter.  

                                    • What is the name of this function?

                                    • What did the expression evaluate to?

                                    • How many arguments does triangle expect?

                                    • Does the triangle function produce Numbers? Strings? Booleans?

                                  • You’ve just created an example of a new Datatype, called an Image.
                                    • What are the types of the arguments triangle was expecting?

                                    • How does this output relate to the inputs?

                                    • Try making different triangles. Change the size and color! Try using "outline" for the second argument.

                                  • The triangle function consumes a Number and two Strings as input, and produces an Image. As you can imagine, there are many other functions for making images, each with a different set of arguments. For each of these functions, we need to keep track of three things:

                                    • Name - the name of the function, which we type in whenever we want to use it

                                    • Domain - the data we give to the function (names and Types!), written between parentheses and separated by commas

                                    • Range - the type of data the function produces

                                    Domain and Range are Types, not specific values. As a convention, we capitalize Types and keep names in lowercase. triangle works on many different Numbers, not just the 20 we used in the example above!

                                  • Can you see what is wrong with each of these expressions? Try copying them into Pyret, one at a time, and reading the error messages aloud.

                                    • triangle(20, "solid", "red"

                                    • triangle(20 "solid" "red")

                                    • triangle("20", "solid", "red")

                                    • triangle(20, "solid", "red", "striped")

                                    Explanations for each error message:

                                    • Pyret needs both parentheses around the arguments, so that it knows exactly where the expression begins and ends.

                                    • Arguments must be separated with a comma.

                                    • triangle expects the first argument to be a Number. "20" is a String.

                                    • triangle takes exactly three arguments. Functions must be called with the correct number of arguments.

                                  • These three parts make up a contract for each function. Let’s take a look at the Name, Domain, and Range of num-sqrt and triangle:   The first part of a contract is the function’s name. In this example, our functions are named num-sqrt and triangle.

                                    The second part is the Domain, or the names and types of arguments the function expects. triangle has a Number and two Strings as variables, representing the length of each side, the mode, and the color. We write name-type pairs with double-colons, with commas between each one.

                                    Finally, after the arrow goes the type of the Range, or the function’s output, which in this case is Image.

                                    Turn to the back of your workbook. We’ve given you the contracts for many Image-producing functions (as well as quite a few others!). Try using some of these contracts to make shapes.

                                  • Turn to the back of your workbook, and get some practice reading and using contracts! Make sure you try out the following functions:

                                    • text

                                    • circle

                                    • ellipse

                                    • star

                                  • Here’s the contract for another new function. Can you figure out how to use it in the Interactions Area?  

                                    The string s is printed n times, written as a single String.

                                  • Here’s an example of another function. Type it into the Interactions Area to see what it does. Can you figure out the contract, based on the example?  

                                    The contract is string-contains :: (s :: String, search :: String) -> Boolean. Be sure the names students come up with for the variables make sense!

                                  • We can extend our shapes table even further, by adding a column called sample. Then, for each row, add an expression that will create an example of that shape. For example:  

                                    Complete the sample column in the shapes table by applying the other relevant functions.

                                  • Can you think of a situation when you’d want to consume a Table, and use that to produce an image? Have you ever see any pictures created from tables of data?

                                    Give the class a minute to brainstorm.

                                  • The library included at the top of the file includes some helper functions that are useful for Data Science, which we will use throughout this course. Here is the contract for a function that does just that, and an example of using it:  
                                    • What is the Name of this function?

                                    • How many inputs are in its Domain?

                                    • Type the example into the Interactions Area.

                                    • What comes back?

                                  • In the Interactions Area, type pie-chart(shapes-table, "name", "corners") and hit Enter. What happens? What happens when you hover over a slice of the pie? These plots are interactive! This allows us to experiment with the data before generating the final image.

                                    Hovering over a pie slice or bar reveals the value or percentage of the whole, and the label.

                                  • The function pie-chart consumes a Table of data, along with the names of two columns in that table. The first name tells the computer where to look to label each pie slice. The second tells the computer where to look to find out how big each pie slice should be. In this example, we used our shapes table as our dataset, and made a pie chart showing the distribition of corners across the table.

                                    Why aren’t there any pie slices for circle or ellipse?

                                  • Here is the contract for another function:  

                                    Use this function to make a bar chart showing the number of corners for each shape in the table.

                                  • Do you think we could use any column for our labels? Could we use any column for our data?

                                    Experiment with these two functions, passing in different column names for the label and data columns. If you get an error message, read it carefully! What do you think are the rules for what kinds of columns can be used by bar-chart and pie-chart?

                                  Row and Column Lookups

                                  Overview

                                  Learning Objectives

                                  • Students extend their understanding of function application

                                  Evidence Statementes

                                    Product Outcomes

                                      Materials

                                        Preparation

                                          Row and Column Lookups (Time 10 minutes)

                                          • Row and Column LookupsTables have special properties, called Methods, 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 method:   Note: data rows start at zero!

                                            For practice, in the Interactions Area, use the row-n method to get the second and third data rows.

                                          • Pyret also has a way for us to get at individual 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 two examples that use row accessors to get at different columns from the first row in the shapes table:  

                                            How would you get the name column out of the second row? The third?

                                          • Let’s get some pratice playing with the row-n method, and row-accessors!

                                            Complete the exercises on page Page 5.

                                          Closing

                                          Overview

                                          Learning Objectives

                                            Evidence Statementes

                                              Product Outcomes

                                                Materials

                                                  Preparation

                                                    Closing (Time 5 minutes)

                                                    • ClosingOne of the skills you’ll learn in this class is how to diagnose and fix errors. Some of these errors will be syntax errors: a missing comma, an unclosed string, etc. All the other errors are contract errors. If you see an error and you know the syntax is right, ask yourself these two questions:

                                                      • What is the function that is generating that error?

                                                      • What is the contract for that function?

                                                      • Is the function getting what it needs, according to its Domain?

                                                    • By learning to use values, operations and functions, you are now familiar with the fundamental concepts needed to write simple programs. You will have many opportunities to use these concepts in this course, by writing programs to answer data science questions.

                                                      Make sure to save your work, so you can go back to it later!