1 of 17

Starting to Program

@BootstrapWorld

#BootstrapCS

2 of 17

Introducing Pyret

Open up the Animals Starter File in a new tab.

Click “Connect to Google Drive” to sign into your Google account. This will allow you to save Pyret files into your Google Drive.

Next, click the "File" menu and select "Save a Copy". This will save a copy of the file into your own account, so that you can make changes and retrieve them later.

@BootstrapWorld

#BootstrapCS

3 of 17

Introducing Pyret

The Definitions Area on the left is where programmers define values and functions that they want to keep.

The Interactions Area on the right allows them to experiment with those values and functions.

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

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.

@BootstrapWorld

#BootstrapCS

4 of 17

Introducing Pyret

This screen is called the Editor, and it looks

something like the diagram you see here.

Most of the screen is taken up by two large

boxes: the Definitions Area on the left and

the Interactions Area on the right.

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.

The first few lines in the Definitions Area tell Pyret to import files from elsewhere, which contain tools we’ll want to use for this course.

We’re importing a file called Bootstrap:Data Science, as well as files for working with Google Sheets, tables, and images.

include shared-gdrive("Bootstrap-DataScience-...")

include gdrive-sheets

include tables

include image

@BootstrapWorld

#BootstrapCS

5 of 17

Introducing Pyret

There’s also a line of code that defines shelter-sheet to be a spreadsheet. This table is loaded from Google Drive, so now Pyret can see the same spreadsheet you do!

# Load your spreadsheet and define your table

shelter-sheet = load-spreadsheet("19m1bUCQo3fCzmfnmsNI...")

This is the Google spreadsheet ID!

# starts a comment - the computer doesn’t read these.

@BootstrapWorld

#BootstrapCS

6 of 17

Introducing Pyret

After that, we see the following code:

# load the 'pets' sheet as a table called animals-table

animals-table = load-table: name, species, sex, age, fixed, legs

source: shelter-sheet.sheet-by-name("pets", true)

end

Here’s our sheet from earlier

List all the columns, giving them names

@BootstrapWorld

#BootstrapCS

7 of 17

Introducing Pyret

Header Row

Identifier Column

Data Rows

Data Rows

We can see what the table looks like by clicking run and typing “animals-table” into the interactions window.

@BootstrapWorld

#BootstrapCS

8 of 17

Introducing Pyret

In Data Science, every table is composed of cells, which are arranged in a grid of rows and columns. Most of the cells contain data, but the first row and first column are special. The first row is called the header row, which gives a unique name to each variable (or “column”) in the table. The first column in the table is the identifier column, which contains a unique ID for each row. Often, this will be the name of each individual in the table, or sometimes just an ID number.

Below is an example of a table with one header row and two data rows:

@BootstrapWorld

#BootstrapCS

9 of 17

Introducing Pyret

  1. Click run and type “animals-table” into the Interactions Area and hit “Enter” to see what comes back in Pyret. What is the same as the table you saw in google sheets? What is different?
  2. How many variables are listed in the header row? What are they called? What is being used for the identifier column in this dataset?
  3. Try changing a column name, and click "Run". What happens when you print out the table back in the Interactions Area?
  4. What happens if you remove a column from the list? Or add an extra one?

@BootstrapWorld

#BootstrapCS

10 of 17

Numbers, Strings & Booleans

Pyret lets us use many different kinds of data. In the animals table, for example, there are Numbers (the number of legs each animal has), Strings (the species of the animal), and Booleans (whether it is true or false the animal is fixed).

@BootstrapWorld

#BootstrapCS

11 of 17

Numbers, Strings & Booleans

Pyret has the usual arithmetic operators: +, -, *, /

Sometimes we need to compare values. To sort the table by age, we need to know if one animal’s age is less than another’s and should come before it. Or maybe we want to filter by only animals whose sex is equal to “male”.

Pyret has Boolean operators, too: ==, <, >, >=, <=

@BootstrapWorld

#BootstrapCS

12 of 17

Numbers, Strings & Booleans

With your partner(s), turn to Numbers & Strings (Page 7) in your workbooks, and follow the instructions there.

When you’re done, do the same for Booleans (Page 8).

@BootstrapWorld

#BootstrapCS

13 of 17

Numbers, Strings & Booleans

  1. Numbers and Strings evaluate to themselves.
  2. Anything in quotes is a String, even something like "42".
  3. Strings must have quotation marks on both sides.
  4. Operators like +, -, *, and / need spaces around them.
  5. If more than one operator is used, we need parentheses
  6. We can add two Numbers or two Strings to one another, but we can’t add the Number 4 to the String "hello".
  7. The <> operator means “not equal”.

@BootstrapWorld

#BootstrapCS

14 of 17

Defining Values

In math, you’re probably used to seeing definitions like x = 4, which defines the name x to be the value 4.

Pyret works the same way. We generally write definitions on the left, in the Definitions Area. You can add your own definitions, for example:

my-name = "Maya"

sum = 2 + 2

kittens-are-cute = true

@BootstrapWorld

#BootstrapCS

15 of 17

Defining Values

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

  1. Define a value with name food, whose value is a String representing your favorite food
  2. Define a value with name year, whose value is a Number representing the current year
  3. 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
  4. Define values of your own!

@BootstrapWorld

#BootstrapCS

16 of 17

Defining Values

Why is it useful to be able to define values and refer to them by name?

@BootstrapWorld

#BootstrapCS

17 of 17

Additional Exercises

  • Boolean Operators (link)

@BootstrapWorld

#BootstrapCS