Jupyter Notebooks#

Jupyter Info

Reminder, that on this site the Jupyter Notebooks are read-only and you canā€™t interact with them. Click the button above to launch an interactive version of this notebook.

  • With Binder, you get a temporary Jupyter Notebook website that opens with this notebook. Any code you write will be lost when you close the tab. Make sure to download the notebook so you can save it for later!

  • With Colab, it will open Google Colaboratory. You can save the notebook there to your Google Drive. If you donā€™t save to your Drive, any code you write will be lost when you close the tab. You can find the data files for this notebook below:

You will need to run all the cells of the notebook to see the output. You can do this with hitting Shift-Enter on each cell or clicking the ā€œRun Allā€ button above.

This is a Jupyter Notebook! Itā€™s a lot like Pythonā€™s interactive mode with two big differences:

  • You can put formatted text in a notebook (like this text right here!)

  • You break your interaction into various cells that you can go back and edit or run in any order!

Below, is a notebook cell that contains some Python code. You can run it by clicking on it and hitting Shift+Enter.

print('Hello world')
Hello world
1 + 1
2

Notice in the second cell, I didnā€™t have to print the result! This is because a Jupyter Notebook always displays the value of the last line of code in it for you! Here is a clearer example of that concept.

x = min(1, 2)
y = min(4, 5)
x
1

You might be wondering, is this a lot like Ed and itā€™s snippets? While they might look similar (code with text in between), they actually operate quite differently!

  • A snippet in Ed is its own independent program. When you run it, you get a blank slate.

  • A cell in a Jupyter Notebook are connected because they all share the same enviornment to store variables! This is sometimes called the notebookā€™s ā€œhidden stateā€ because behind the scenes it is storing the results of all the previous cells! For example, the next cell uses the x variable that was defined earlier in this notebook (this would not work with an Ed snippet).

x
1

āš ļøWarning: This behavior can be useful, but commonly does more harm than good! For example, we mentioned we can run cells in any order. That means if multiple cells read/update the same variable, now the behavior of your notebook depends on the order in which you ran the cells! A common trick is to always restart the notebook and run from the top, which you can do with the menu option up top Restart & Run All.

In CSE 163, we will commonly share notebooks for things like lecture notes and demos because it makes it easier to tell a narrative between code blocks (for example, when we donā€™t want to load in a dataset every single time). There is a lot more complexity to these Jupyter notebooks, but right now all we need is the notion of running these cells! šŸ˜Š