S3P Lab1: Jupyter Notebooks

Introduction

In this lab we will learn about Jupyter Notebooks and get a brief introduction to programming in Python.

We have created a virtual environment that you will need to invoke each time you want to start working on the labs for this class. This environment includes Python, Jupyter Notebooks, and a robot simulator called Jyro. In a terminal window type:

source /usr/swat/bin/s3penv

Your terminal prompt should now have the prefix (s3p). To exit the virtual environment type:

deactivate

Your terminal prompt should return to normal. Try activating and deactivating this virtual environment at least once. Be sure to re-activate the environment before moving on.

Jupyter Notebooks

A notebook is a document that can contain both executable code and explanatory text. It is a great tool for documenting and illustrating scientific results. A notebook is made up of a sequence of cells. Each cell can be of a different type. The most common types are Code and Markdown. We will be using Code cells that are running Python3, though there are many other possibilities. Markdown cells allow you to format rich text.

In your terminal window, where you have activated the virtual environment, type the following to start a notebook:

jupyter notebook

A web browser window will appear with the Jupyter home screen. On the right-hand side, go to the New drop down menu and select Python3. A blank notebook will appear with a single cell.

Using Python in Notebooks

In the cell, let's try a simple arithmetic expression:

  (1+2)*(10/2)

When you are ready to execute the cell, press Shift-Enter, this will run the code in that cell and print the output below it.

Let's try writing a simple program to greet someone by name.

  def hello(name):
     print("Hi", name)

  hello("Chioma")
  hello("Dariel")
We will go over the basic syntax and semantics of Python during the lab.
Using Markdown in Notebooks

Now let's try creating a Markdown cell. In the tool bar of the notebook you'll see a drop down menu where you can choose the cell's type. By default, each cell will be a Code type. With your cursor in a new cell, select Markdown from this menu.

Check out this Markdown cheat sheet to see all of the text formatting options available in Markdown. Try to make a Markdown cell that looks like the one below.

Things to do in the Science Center:

You need to execute Markdown cells using Shift-Enter just as you do with Code cells.

Naming and Saving a Notebook

To name your notebook, double click on the default name, Untitled, at the top. Let's call it FirstNotebook. To save a notebook, use the File menu and choose Save and Checkpoint. Explore the other menu options in the notebook. Figure out how to insert and delete cells, which are common commands you'll need to know.

Exiting a Notebook

To exit a notebook, save it first, then from the File menu choose Close and Halt. In the terminal window where you started the notebook, you'll also need to type CTRL-C to shut everything down.

Use the unix command ls to list the contents of your directory. You'll see that you now have a file called FirstNotebook.ipynb.