Starting to Program
@BootstrapWorld
#BootstrapCS
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
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
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
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
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
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
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
Introducing Pyret
@BootstrapWorld
#BootstrapCS
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
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
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
Numbers, Strings & Booleans
@BootstrapWorld
#BootstrapCS
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
Defining Values
With your partner(s), take turns adding definitions to this file:
@BootstrapWorld
#BootstrapCS
Defining Values
Why is it useful to be able to define values and refer to them by name?
@BootstrapWorld
#BootstrapCS
Additional Exercises
@BootstrapWorld
#BootstrapCS