Unit 1:   Introduction to Pyret

imageUnit 1Introduction to Pyret
Unit Overview

Students write simple programs in Pyret, and learn about Numbers, Strings, Expressions, Operations, and Functions.

Product Outcomes:
    Length: 80 minutes
    • arguments: the inputs to a function; expressions for arguments follow the name of a function

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

    • 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

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

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

    • evaluate: perform the computation in an expression, producing an answer

    • expression: a computation written in the rules of some language (such as arithmetic, code, or a Circle of Evaluation)

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

    • operator: a symbol that manipulates two Numbers and produces a result

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

    • string: any sequence of characters between quotation marks (examples: "hello", "42", "this is a string!")

    • type: refers to a general kind of data, like Number, String, Image, or Boolean

    • value: a specific piece of data, like 5 or "hello"

    • variable: something that changes

      • Show students the opening questions, either as a handout or on posters set up around the room.





      Learning Objectives

        Evidence Statements

          Product Outcomes



              • Show students the opening questions, either as a handout or on posters set up around the room.

              Introduction (Time 15 minutes)

              • Welcome to Bootstrap: DataScience! Take 30 seconds to look at the opening questions we have prepared for you, and choose a topic that interests you.
                • Once you’ve selected your topic, break into groups of no more than 4 and choose a question you’d like to answer.

                • Spend one minute discussing your answer, and explaining why you answered the way you did. Does everyone in your group have the same answer? Why or why not?

                • What kind of measurement would you use to determine if your answer is right or not? What data would you need?

                • Take 5 minutes to complete Page 1 in your Student Workbook, by copying down the question, your answer, and what you discussed with your group.

                Have students self-select into 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? The best quaterback? 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 hundereds 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:

              • In order to ask questions from data, we’ll use a programming language. Just like any human language (English, Spanish, French), 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 to programming and data science, so some of the questions students want to answer may be out of scope. However, this course will give students a foundation to answer their more complicated questions later in their data science education.

              • Begin by opening a web browser, and going to The Pyret Editor. Hit the "log in" button, and sign in with your Google account information, then choose "Start Coding".

                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 in their program, while the Interactions Area allows them to experiment with those values and functions. This is analogous to writing a series of function definitions on a blackboard, and having student evaluate expressions using those function on scrap paper. As students are not yet defining values of their own, it is not important that students understand this distinction right now. For now, we will work only with the Interactions area.

              Numbers, Strings & Operators


              Learning Objectives

                Evidence Statements

                  Product Outcomes



                        Numbers, Strings & Operators (Time 15 minutes)

                        • Begin by typing 4 into the Interactions area, then hit Return. You should see the value 4 appear on the next line in the Interactions area.

                          Congratulations, you’ve written your first (very simple) program! When a program is run, the computer will run that program to produce a value. Values are how the computer represents data. In this case, the computer’s job is easy because the program is already a value! When we hit Run, we evaluate the program, which is another way of saying "give me the value that is produced by this program".

                          • Type 10 in the Interactions area and hit "Return". Now the value 10 appears in the Interactions area.

                          • Try evaluating numbers, like 10345017, or negative numbers, like -2. Is there a limit to how big a number can be? What happens if you write a decimal? What happens when you click on a decimal, like 0.75? You get a new type of number, a fraction, like 3/4.

                          The editing environment evaluates all fractions and returns them as decimals by default. This can be surprising to students at first, so you may want to take a moment to explain what’s going on, and show them that these decimals can be converted back to fractions just by clicking on them. The environment uses standard annotations for repeating, non-terminating decimal expressions and properly handles expressions like . If you want to work with those kinds of numbers in your class, enter them to get familiar with how they appear in the Interactions area.

                        • Every value has a Type. Each of the values produced by the programs you just wrote are Numbers, but there are other types as well. One of these types is called a String. A String is a sequence of characters (letters, numbers, symbols) in between a pair of quotation marks.

                          • Begin by typing "Ahoy, World!" into the Interactions area, then hit Return. You should see the value "Ahoy, World!" appear on the next line in the Interactions area.

                          • Try to type your name within a pair of quotation marks, then hit Return.

                          • What do you notice about the way Pyret displays Strings on the screen?

                          • Type this program into the Interactions area: "4". Is this a String or a number?

                          • What happens when you leave off the second quotation mark? Type this code into the Interactions area and hit Return: "I love writing programs

                          The program "4" is a String. Even though this string only contains the character for the number 4, it is a String because it is between quotation marks. Data scientists care about this distinction, because we often run into data that is represented in a String but should be considered a number by Pyret. This will become more relevant later in the course, for now stress to the students that anything within quotations is a String. Pyret is helpful in detecting strings because it highlights them green.

                        • Notice when you leave off the second quotation mark, "I love writing programs is NOT printed on the next line; instead, we get a big red box. This big red box is an error message. The editor gives us error messages when a program can’t be properly evaluated.

                          Error messages a way for the computer to help you find small mistakes in your code. In the case above, without the second quotation mark the computer can’t figure out when the end of the String is, which makes evaluation impossible.

                          Fix the error described in the error messages by typing "I love writing programs" and hitting the Return key.

                          It is crucial to encourage students to read error messages and debug their code. Often, when a student encounters an error message for the first time, they will throw their hand up and tell the teacher "I did something wrong". When helping students with this, make sure to ask questions about the answer rather than fixing code for them.

                        • We’ve successfully written and evaluated programs, but writing programs that only just repeat values would be boring. Luckily, Pyret allows us to compute values using expressions. One of the great things about Pyret is that these expressions are similar to the ones you’ve seen in math classes.

                        • Type 2 + 5 into the Interactions area. Notice the spaces between the numbers and the plus sign! Then hit Return. You should see the value 7 printed on a new line.

                          Some students may encounter syntax errors because they did not put white space between the values and the operator. We address this error in the next point.

                        • Let’s break this down. 2 + 5 is an expression, and this expression is made up of values and an operator. Here the operator is +, which you’ve seen in math classes: adding two values to create a new value.

                          Now try leaving out the spaces around the plus sign. We get an error! This error happened because Pyret needs you to write spaces between numbers and operators.

                          Pyret requires this whitespace for code readability, and to remove ambiguity when dealing with negative numbers. For example, without the white space rule, the program 5+-2 is hard to understand.

                        • What other operations can we use? Type each of these programs into the Interactions area. If you get an error message, read it out loud and see if you can figure out what it means.

                          • 3 - 8

                          • 1.5 * 3

                          • 100 / 5

                          • 8 + -2

                          • 2.3 * 7

                          • 6 / 0

                          • 2 + "hello"

                          Each of these programs should compile and execute correctly except for the last two, which should raise errors. Possible errors for the other programs should be whitespace/syntax related.

                        • Notice that the last two programs give errors. We know that Pyret gives errors whenever it can’t evaluate a program.

                          • 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.

                          • In 2 + "hello", we’re trying to add a String to a Number. This doesn’t make sense to us, and it doesn’t make sense to Pyret either; Pyret can only add Numbers to Numbers with the + operation.

                        • By now you’ve seen 4 different kinds of errors: What are they caused by?

                          • Leaving off quotation marks for String values

                          • Missing whitespace between operators and values

                          • Division by zero

                          • Adding non-Numbers to Numbers.

                        • Error messages are the computer’s way of giving you a hint on what went wrong. The most important part of using an error message is reading the message editing the program to fix the errors.

                          Turn to Page 2 in your student workbook, and identify whether the expressions you see will produce an error or a value. In either case, write the resulting value or error messages that you think the computer will respond with.

                        • +, -, *, / all work the way they do in math class. Parentheses work pretty much the same way, but with one important exception!

                          Type (2 * 3) + 5 into the Interactions area, and hit Run. It should produce 11.

                        • Parentheses allow you to write more complex expressions. In this example, Pyret evaluates what is inside the parentheses: 2 * 3. Then, it uses this value in the larger expression. So (2 * 3) + 5 becomes 6 + 5, which is evaluated to 11. Note: There is one difference between the way parentheses work in math class, and the way they do in Pyret! In Pyret, any time you have more than one operation, you must include parentheses.

                        Defining Variables


                        Learning Objectives

                          Evidence Statements

                            Product Outcomes



                                  Defining Variables (Time 20 minutes)

                                  • In Pyret, you can define variables with the = sign, just like in math class. You’re probably used to seeing variables x, y, z, etc. In Pyret, you can name values to them easier to remember and easy to change.

                                  • Suppose you want to throw a pizza party for a class of 16 students. We need to buy one of each of these per student:

                                    • 1 Slice of Pizza : $2.50

                                    • 1 Plastic Plate : $0.20

                                    • 1 Soda : $0.98

                                    What code would you write, to compute the total cost of the party?

                                    • The particular items for the party do not matter, feel free to change them as long as they are supplies where each student gets one of each item, or change the number of students to be the number of students in your class.

                                  • Now suppose that you want to include the teacher in the pizza party (teachers like pizza too!) How would you rewrite this program to compute the cost of paying for supplies for 16 students and 1 teacher?

                                    Rewrite your program to get the new cost of a pizza party for 17 people.

                                    Force students to manually change each 16 to a 17.

                                  • Add this line of code to the top of your definitions area:   Then, substitute all of the instances of 17 with party-people in your expression to compute the cost of the party.

                                    Make students compute the cost of throwing a pizza party for 20 people. Ask them which line they need to update for this party size, and highlight the ease of editing this program vs. the previous one.

                                  • Use identifiers to program the following word problem: A train is moving at a constant speed of 65mph. How far has it gone in 3.75 hours?

                                    Students should use identifiers for speed and time.

                                  • Turn to Page 3 in your workbook. For each expression, write down the value the computer will return. If the expression will result in an error, write down what you think the error will say.



                                  Learning Objectives

                                    Evidence Statements

                                      Product Outcomes



                                            Functions (Time 25 minutes)

                                            • So now you know about Numbers, Strings, Operators and Variables - all of which behave just like they do in math. But what about functions? Pyret has lots of built in functions, which we can use to write more interesting programs than are possible with just operators and variables.

                                              Let’s explore a new function, num-min. Type each of these lines of code into the interactions area and hit enter.

                                              • num-min(0, 1)

                                              • num-min(-5, 2)

                                              • num-min(8, 6)

                                              The values that we give to a function are called it’s arguments. How many arguments are we giving to num-min in the three examples above? What are the types of these arguments? What type of value does this function output? How does this output relate to the two inputs?

                                            • This is your first example of using a function in Pyret. To use a function, a programmer writes:

                                              • The name of the function. This function’s name is num-sqr

                                              • The argument(s) we want to give to the function, wrapped in parentheses and separated by commas. In this case, there is one argument: The number we want to square. However, different functions can have differents numbers of arguments.

                                              Each function also outputs a value. The num-sqr function outputs a number that is equal to the square of the argument. What do you think the function num-sqrt does?

                                            • For each of these lines of code, try to guess what the problem is based on the error messages.

                                              • num-min(2, 3

                                              • num-min(1 9)

                                              • num-min("hi", 2)

                                              • num-min(1, 4, 6)

                                              Explanations for each error message:

                                              • Pyret needs both parentheses around the arguments, so that it knows exactly where function call begins and ends. This is similar to the error with Strings needing both quotation marks.

                                              • For a similar reason, Pyret needs a comma between each argument so that it can tell how many arguments there are.

                                              • num-min returns the minimum between the two numbers that are given as arguments. It can’t compare a String "hi" to a number, so an error is raised.

                                              • Functions need to be called with the exact number of arguments it is expecting. num-min takes two arguments, so calling it with 1, 3, 4, 5 etc. will give an error.

                                            • Every successful application of num-min will take 2 Numbers as input, and evaluate to one Number. For each function we learn, we need to keep track of what arguments it takes, and what type of value it returns.

                                            • We do this by writing contracts. Contracts are comments in code that give the programmer instructions on what functions take in and output. Below is the contract for num-min:


                                              The first part of a contract is the function name. This function is num-min, so that part is easy. num-min takes two arguments of type Number, so we write Number Number. Finally, after the arrow goes the type of the function’s output, which in this case is Number.

                                              Write the contract for num-min in the back of your workbook. What do you think the contract would be for num-max? Write it down, and then try using it in the Interactions area.

                                            • There are also plenty of functions you know from math class. Let’s write the contract for num-sqrt together.

                                              • What is the function name?

                                              • How many arguments are there? What are the types of each?

                                              • What is the type of the output?

                                                On your own, write the contract for num-sqr.

                                            • Turn to the last page in your workbook. For each set of function calls, write the complete contract for the function, as well as an example of how to use it.

                                            • Pyret has many, many more functions. Some of these functions are defined as part of the language, and others are defined in extra files that we have to load by hand. Fortunately, including an external file is really easy! Try typing in the following code at the top of the Definitions Area:   This includes a file called plot-list, which defines a lot of extra functions for drawing charts, graphs and plots. When you click Run, Pyret will read that file and become aware of all those plotting functions.

                                            • Two functions imported by this file are called function-plot and draw-plot:   function-plot consumes any function that maps from Numbers to Numbers (xs to ys, for example), and returns a series representing the graph of that function. draw-plot consumes a title and that series, and produces an Image. Make sure you write these down in your contracts page!

                                            • We can define identifiers for both the series and the plot:   Review: once I’ve defined an identifier, I can see its value for by clicking Run, then typing in the identifier in the Interactions Area and hitting Enter.

                                              Looking at your contracts page, do you see any other functions that we could plot?



                                            Learning Objectives

                                              Evidence Statements

                                                Product Outcomes



                                                      Closing (Time 5 minutes)

                                                      • One 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?

                                                      • By learning to use values, variables, 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 the next units, by writing programs to answer data science questions.

                                                        Make sure to save your Pyret program as Pyret Practice into your Google Drive, so you can go back to it later.