Working with Python in Jupyter notebooks

files needed = none

Our goal for this notebook is to learn about, and how to use, Jupyter notebooks:

  1. What is a Jupyter notebook?
  2. How do I open, save, and edit?
  3. What are markdown cells vs code cells?
  4. What is markdown?

What is a Jupyter notebook?

There are many ways to write and run Python code.

  1. Use the interactive interpreter. Open an Anaconda prompt and type python. It is hard to do anything too complicated this way.

  2. Write your code in a text file, save it, and run the file through an interpretor. This makes it easy to rerun your code is typically how complex programs are written. This method separates the code from the output.

  3. We will take advantage of Jupyter notebooks. A notebook combines input (code) with output (results). It also allows us to mix in text, which will let us provide lots of documentation about what our code is doing. The text is added in markdown which gives us even more options. We will discuss markdown a bit later.

A notebook runs a command window (look at your taskbar) that we do not need to bother with and a document displayed in a web browser. You are looking at the notebook right now!

The cell you are reading is a markdown cell. It is text.

The cell below this cell is a code cell. Notice that the cell has 'In [ ]:' in front of it. That means it is a cell for code input. Place your cursor in the cell and type 3*2 and hit control+enter to run the cell. What happened?

In [2]:
3*2
Out[2]:
6

You just computed! Notebooks are interactive. We can work on block of code and execute them using ctrl+enter or shift+enter. This will be very convenient. In between blocks of code, we can add blocks of text to describe what we are doing.

Use the insert menu to insert a block below this one. What kind of cell is it?

Go back to the cell you inserted and change it to a markdown cell.

Command mode versus edit mode

Notice that a cell can have two states: edit mode, where you type and command mode, where the result of the typed code is displayed.

  1. If the cell in in command mode, double click, or select it the cell and hit enter to change to edit mode.
  2. If a cell is in edit mode, run the cell (ctrl+enter or shift+enter) or hit esc to move to command mode.

Keyboard shortcuts

When we are in command mode, we can use keyboard shortcuts. Shortcuts are much quicker than taking our hands off the keyboard and moving the mouse around.

Let's try one. Make sure this cell is in command mode (esc). Then hit b. What happened? What kind of cell is this?

In [ ]:
 

Shortcuts take some time to memorize and use, but you will be a more efficient coder if you make them part of your workflow. You can find a list of shortcuts in the Help > Keyboard Shortcuts menu.

A few I use often:

  1. Command mode: esc
  2. Edit mode: enter
  3. Change markdown cell to code cell: y
  4. Change code cell to markdown cell:m
  5. Insert cell below: b

Remember, you need to be in command mode to use shortcuts.

Working with notebooks

You already have this workbook up and running, so we are on the right track. Let's review:

Starting Jupyter notebook

  1. Log into Winstat. See sscc winstat if you need a refresher
  2. From the search menu in the left of the task bar, start typing 'Jupyter notebook'. As you type, search results will appear. Click on the 'Jupyter Notebook' entry (not JupyterLab).
  3. This will open a console window (which we don't need to use) and a web browser. The web browser will show the contents of your user drive (U:) drive.

Opening a notebook

  1. Within the browser window, navigate to the folder that contains the workbook you would like to open. Reminder: notebooks end in 'ipynb' (short for IPython Notebook).
  2. Click on the notebook to open.

Creating a new notebook

Within the browser window, look in the upper right corner for new button. Choose 'Python 3' as the new notebook type.

Renaming a notebook

You can name and rename a notebook by changing the file name at the top of the notebook. This overwrites the notebook name in the file system. It does not make a copy.

Saving a notebook

Save from the file menu, the disk icon, or ctrl+s.

Closing a notebook

Close the tab in which the notebook is open. Note: close the tab, not the whole browser!

Practice: notebooks

Take a few minutes and try the following. Feel free to chat with those around if you get stuck. I am here, too.

  1. Create a new notebook and name it 'week_1_practice'
  2. Make the first cell of your notebook a markdown cell (try a shortcut!). Type your name in the cell and run the cell.
  3. Save the notebook and close it.

Now go back to the Jupyter notebook tab that shows your files. Can you see your notebook?

  1. Open your 'week_1_practice' notebook
  2. From the file menu, make a copy of the notebook and rename it 'week_1_practice_v2'
  3. In 'week_1_practice_v2', in the first cell (below your name), type out the names of three classes you are taking this semester.
  4. Save your notebook and close it.

Go back to the Jupyter notebook tab in your browser. Do you see both files?

Open your (U:) drive. Do you see the two files?

Markdown

Markdown is a simple 'markup' language. The point of markdown is to provide a very simple set of commands to format text. Nothing too fancy, but enough stuff like headers, lists, links, italics, bold, etc. to make our documentation easy to read. We can also write mathematical equations in markdown which is very helpful for documenting our code.

Read the markdown cheatsheet available here for a list of commands. (Look at this cell in edit mode (which shortcut?) to see the syntax for a link!)

Let's try some markdown. For each item below, insert a markdown cell and try it out. Remember to hit shift-enter to run the cell and see the result.

In the cell below, make three layers of headings. # text will generate the first-level heading that reads 'text'. How do we get the second-level and third-level headings?

(Notice that an empty markdown cell will read 'Type Markdown and LaTeX: $\alpha^2$' in command mode.)

Heading level one

Heading level two

Heading level three

In the cell below, try some **bold** and *italics*. I like ~~strikethrough~~ because I make a lot of mistakes. What happens in the cell as you type?

bold and spicy

I italicize book titles

another mistake!

Lists

An unordered list uses asterisks. Try

* Bacon
* Lettuce 
* Tomato

and run the cell (shift+enter).

  • Bacon
  • Lettuce
  • Tomato

Then try an ordered list

1. Wisconsin
2. Minnesota
3. Michigan

and run the cell (shift+enter).

  1. Wisconsin
    1. Madison
    2. Milwaukee
  2. Minnesota
    1. Minneapolis
    2. Ely
  3. Michigan
    1. Detroit
    2. Grand Rapids
    3. Kalamazoo

Now go back and add sub-items to the entries. It should look like

1. Wisconsin
  1. Madison
  2. Milwaukee
2. Minnesota
  1. Minneapolis
  2. Ely
3. Michigan
  1. Detroit
  2. Grand Rapids
  3. Kalamazoo

What happens when you run the cell? How do you add a third level to the list?

Code

It is often useful to write bits of code in your markdown. Use backticks (next to the 1 key on your keyboard) to format as code. In the cell below, try `x=2` . What happened?

x=2

Now try a block of code. Use a 'fence' of three backticks at the beginning and end of the code, like this:

```python

s = 10

print(s)

```

Try it in the block below. Try removing the word 'python' from the block of code. What changes?

s = 10
print(s)

Without the python keyword

s = 10
print(s)

Practice: Markdown

Take a few minutes and try the following. Feel free to chat with those around if you get stuck. I am here, too.

Open your 'week_1_practice_v2' notebook and

  1. Format your name as a level-1 header
  2. Format your list of classes into an unordered list
  3. Make the class you are most excited about in bold font
  4. Make the class you are least excited about in strikethrough

then...

  1. Insert a code cell below the block you just edited. Within the cell type
s = 'a string'
print(s)

and run the cell. Then...

  1. Insert a markdown cell below the code cell. Use the 'backtick fences' method to write a block of code with the two statements s='a string' and print(s)' within it.
In [1]:
# This should be in your week_1_practice_v2 notebook...

s = 'a string'
print(s)
a string
s = 'a string'
print(s)