
Basic Python in Jupyter
We must open a Python section of our Notebook to use Python coding. So, start your Notebook, and then in the upper-right menu, select Python 3:

This will open a Python window to work in:
As mentioned in the previous chapter, the new window shows an empty cell so that you can enter Python code.
Let's give the new work area a name, Learning Jupyter 5, Chapter 2. Autosave should be on (as you can see next to the title). With an accurate name, we can find this section again easily from the Notebook home page. If you select your browser's Home tab and refresh it, you will see this new window name being displayed:
Note that it has an Notebook icon versus a folder icon. The extension that's automatically assigned is .ipynb (Python Notebook). And, since the item is in a browser in a Jupyter environment, it is marked as running. There is a file by that name in your directory on the disk as well:
If you open the .ipynb file in a text editor, you will see the basic contents of a Jupyter node (as mentioned in the Notebook structure section in Chapter 1, Introduction to Jupyter). We have one empty cell and metadata about the Notebook:
{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }
We can now enter Python coding into the cells. For example:
- Type in some Python in the first cell
- Add another cell to the end (using the Insert Cell Above or Insert Cell Below menu command):
name = "Dan" age = 37
- And in the second cell, enter the Python code that references the variables from the first cell:
print(name + ' is ' + str(age) + ' years old.')
- We will then have this display:
It's important to note that Jupyter color codes your Python (just as a decent editor would), and that we have these empty braces to the left of each code block.
If we execute Run All, the results are displayed inline:
We now have the braces filled in with cell numbers, and the output of cells is appended to the bottom of each cell. It's important to note that cell two was able to reference variables that were declared in cell one.
If we either wait for autosave to kick in or hit the save icon (the leftmost icon of a diskette), we will update the .pynb file on the disk with our results:
{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = \"Dan\"\n", "age = 37" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(name + ' is ' + str(age) + ' years old.')" ] } ], "metadata... as above
It's interesting that Jupyter keeps track of the output last generated in the saved version of the file. You can also clear the output using the Cell| All Ouput | Clear command.
If you were then to rerun your cells (using Cell | Run All), the output would be regenerated (and saved via autosave). The cell numbering is incremented if you do this – Jupyter is keeping track of the latest version of each cell.
Similarly, if you were to close the browser tab, refresh the display in the Home tab, find the new item we created (Learning Jupyter 5, Chapter 2.pynb) and click on it, the new tab (as created previously) will be displayed, showing the outputs that we generated when we last run it.
If you open the server command-line window (where the Jupyter service is running), you will see a list of the actions that we have made during our session:
The logging entries are at a high level. There may be a way to increase the logging level if there is some difficulty being encountered.