![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS371 Cognitive Science / 2016-Fall |
By Your-Name-Here
You can delete the rest of this cell, after you read it.
This is a Markdown cell. You can make bulleted lists like this:
You can make numbered lists like this:
Goals for this notebook are:
We will be using the Jupyter notebook for many activities this semester. Every notebook has an associated language called the "kernel". We will be using in the Python 3 kernel from the IPython project.
For more information on how to use the notebook, please read the following (which is also a notebook written by a Bryn Mawr student):
You only need do this once:
%%file ~/nbgrader_config.py
c = get_config()
c.NbGrader.course_id = "cs371"
c.TransferApp.exchange_directory = "/opt/nbgrader/exchange/"
The above cell uses the %%file
metacommand of the notebook to create a file named "/home/YOURID/nbgrader_config.py". This will allow you to see the assignments, fetch them, and turn them in. (In a slight dependency issue, you can't see this notebook until you fetch it. But I'm also making it available off of the syllabus.)
After you execute the above cell, you'll see the "Assignments" tab on the desktop.
You can read more about IPython magics here:
http://ipython.readthedocs.io/en/stable/interactive/magics.html?highlight=magics
Python is fairly easy to learn as it has been designed to be intuitive. But it takes some work to get familiar with these intuitions. These links might be useful for learning Python, and referencing parts of the language. We will be using Python, version 3.5.
Pick any of the following, if you want to learn more about Python:
Getting Started with Python:
Learning Python in Notebooks:
This is handy to always have available for reference:
Python Reference:
In the next section, there will be a series of problems to solve with Python.
In the next cell, assign 42 to a variable x
:
# YOUR CODE HERE
raise NotImplementedError()
This cell will be autograded:
assert x == 42, "x should be equal to 42"
In the next cell, define the following function:
def celsius_to_fahrenheit(celsius):
# YOUR CODE HERE
raise NotImplementedError()
assert celsius_to_fahrenheit(0) == 32
assert celsius_to_fahrenheit(37.5) == 99.5
assert celsius_to_fahrenheit(100) == 212
Python has two very useful data structures built into the language:
Try out some test code exploring dictionaries and lists. You can add as many cells as you would like below.
"List comprehension" is the idea of writing some code inside of a list that will generate a list.
Consider the following:
[x ** 2 for x in range(10)]
This is equivalent to the following:
temp_list = []
for x in range(10):
temp_list.append(x ** 2)
temp_list
But list comprehension is much more concise.
As part of our explorations, we will need to visualize data. One common desire is to make graphs. We will use the library matplotlib
for this.
First, we must tell matplotlib that we will be using the notebook via a magic:
%matplotlib notebook
After the magic, we then need to import the matplotlib library:
import matplotlib.pyplot as plt
Python has many, many libraries. We will use a few over the course of the semester.
To create a simple line plot, just give a list of y-values to the function plt.plot().
plt.plot([5, 8, 2, 6, 1, 8, 2, 3, 4, 5, 6])
But you should never create a plot that doesn't have labels on the x and y axises, and should always have a title. Read the documentation on matplotlib and add labels and a title to the plot above:
http://matplotlib.org/api/pyplot_api.html
Another commonly used library (especially with matplotlib is numpy). Often imported as:
import numpy as np
You can read more about numpy here:
In the cell below, create a plot of the first 20 integers, squared.
# YOUR CODE HERE
raise NotImplementedError()
Random is another library that we will be using quite a bit:
import random
random.random()
random.choice([1, 2, 3, 4, 5])
Try using the random library in a program. For example, can you make a game? Maybe tic-tac-toe? If you want to get input from the user, use the input("Prompt> ")
function.
The reflection cell is the most import cell in this lab... in all labs. In your reflection, you should:
For this first lab, please also add reflections on meta-topics:
YOUR ANSWER HERE