1. Cognitive Science: Lab01

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:

  • turn on spell-checking by clicking the check button
  • you can make bold text like this
  • you can make italicized text like this

You can make numbered lists like this:

  1. an item
  2. another item
  3. press shift+enter to render text/evaluate code

1.1 Goals

Goals for this notebook are:

  • practice using a notebook
  • practice using Python
  • experience the lab fetch/submission process

1.2 Reference for Notebooks

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):

1.3 Gradebook Setup

You only need do this once:

In [ ]:
%%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

2. Learning Python

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.

3. Problems

In the next cell, assign 42 to a variable x:

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
This cell will be autograded:
In [ ]:
assert x == 42, "x should be equal to 42"

In the next cell, define the following function:

In [ ]:
def celsius_to_fahrenheit(celsius):
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
assert celsius_to_fahrenheit(0) == 32
assert celsius_to_fahrenheit(37.5) == 99.5
assert celsius_to_fahrenheit(100) == 212

3.1 Built-in data structures

Python has two very useful data structures built into the language:

  • dictionaries (hash tables): {}
  • lists: []

Try out some test code exploring dictionaries and lists. You can add as many cells as you would like below.

3.2 List comprehension

"List comprehension" is the idea of writing some code inside of a list that will generate a list.

Consider the following:

In [ ]:
[x ** 2 for x in range(10)]

This is equivalent to the following:

In [ ]:
temp_list = []
for x in range(10):
    temp_list.append(x ** 2)
temp_list

But list comprehension is much more concise.

3.3 Plotting

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:

In [ ]:
%matplotlib notebook

After the magic, we then need to import the matplotlib library:

In [ ]:
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().

In [ ]:
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:

In [ ]:
import numpy as np

You can read more about numpy here:

http://docs.scipy.org/doc/numpy/

In the cell below, create a plot of the first 20 integers, squared.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()

3.4 Random

Random is another library that we will be using quite a bit:

In [ ]:
import random
In [ ]:
random.random()
In [ ]:
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.

4. Reflection

The reflection cell is the most import cell in this lab... in all labs. In your reflection, you should:

  • Reflect! Think and comment on what you have learned this lab, and this week. Connect the ideas onto your own life, and experiences.
  • Feel free to comment on what you found challenging, and what you found intuitive.

For this first lab, please also add reflections on meta-topics:

  • Why are you taking Cognitive Science?
  • How could you connect this material onto your own interests?

YOUR ANSWER HERE