download notebook
view notebook w/ solutions
Working with Python in Jupyter notebooks
files needed = none
Our goal for this notebook is to learn about, and how to use, Jupyter notebooks:
- What is a Jupyter notebook?
- How do I open, save, and edit?
- What are markdown cells vs code cells?
- What is markdown?
How python works
- Write python source code (e.g., 3*2)
- The python interpreter reads the python source code and creates machine code
- The machine code is exectued and any results (e.g., 6) are returned
What is a Jupyter notebook?
There are many ways to write and run Python code.
-
Use the interactive interpreter. Open an Anaconda prompt and type
python
. It is hard to do anything too complicated this way. -
Write your code in a text file, save it, and run the file through an interpretor. This makes it easy to rerun your code, and is typically how complex programs are written. This method separates the code from the output.
-
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 front of it. That means it is a cell for code input. Place your cursor in the cell and type 3*2
and hit ctrl+enter
to run the cell. What happened?
You just computed! Notebooks are interactive. We can work on blocks 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 + button 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.
- If the cell is in command mode, double click, or select the cell and hit
enter
to change to edit mode. - If a cell is in edit mode, run the cell (
ctrl+enter
orshift+enter
) or hitesc
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 (hit esc
). Then hit b
. What happened? What kind of cell is this?
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 here.
A few I use often:
- Command mode:
esc
- Edit mode:
enter
- Change markdown cell to code cell:
y
- Change code cell to markdown cell:
m
- 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 lab
- Log into Winstat. See sscc winstat if you need a refresher
- From the search menu in the left of the task bar, start typing 'Jupyter lab'. As you type, search results will appear. Click on the 'Jupyter Lab' entry (not Jupyter Notebook).
- This will open a console window (which we don't need to use) and a web browser. The web browser will show the Jupyter Lab interface. The left-hand panel includes a file browser.
Opening a notebook
- Within the file browser (left panel), navigate to the folder that contains the workbook you would like to open. Reminder: notebooks end in 'ipynb' (short for IPython Notebook).
- Click on the notebook to open.
Creating a new notebook
File > New > Notebook
. When it asks for the kernel, choose "Python 3."
Renaming a notebook
You can name and rename a notebook using File > Rename Notebook
. This overwrites the notebook name in the file system. It does not make a copy.
Saving a notebook
File > Save Notebook
, the disk icon, or ctrl+s
.
Closing a notebook
File > Close and Shutdown Notebook
, or ctrl+shift+q
.
Closing Jupyter lab
File > Shut Down
, then follow the instructions and 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.
- Create a new notebook and name it 'week_1_practice'
- Make the first cell of your notebook a markdown cell (try a shortcut!). Type your name in the cell and run the cell.
- Save the notebook and close it.
Now look in the file browswer (left panel). Can you see your notebook?
- Open your 'week_1_practice' notebook
- From the
file
menu, make a copy of the notebook (File > Save Notebook As
) and name the copy 'week_1_practice_v2' - In 'week_1_practice_v2', in the first cell (below your name), type out the names of three classes you are taking this semester.
- Save your notebook and close it.
Check the file browser (left panel). 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.
Headings
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.)
Fonts
Insert a 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?
Lists
An unordered list uses asterisks. Try
* Bacon
* Lettuce
* Tomato
and run the cell (shift+enter
).
Then try an ordered list
1. Wisconsin
2. Minnesota
3. Michigan
and run the cell (shift+enter
).
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 3. Format your list of classes into an unordered list 4. Make the class you are most excited about in bold font 5. Make the class you are least excited about in strikethrough font
then...
- Insert a code cell below the block you just edited. Within the cell type
s = 'a string'
print(s)
and run the cell. Then...
- 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'
andprint(s)'
within it.
# This should be in your week_1_practice_v2 notebook...
s = 'a string'
print(s)
s = 'a string'
print(s)