Jupyter provides a nice web-based interface to Python. In a new notebook you can type a line of Python. For example, here we type in
a = 5
When we press SHIFT+Return
this Python line is interpreted interactively
a = 5
To see that this has worked, let's print the value of a
print(a)
You can type whatever Python you want, and it will be evaluated interactively. For example...
b = 10
print(a + b)
The ability to go back and change only small snippets of code is very useful, but also very dangerous form a coding point of view. If you edit a code cell and don't run all the code cells after it, then any cell that isn't re-executed is still using the old code. Jupyter allows you to keep track of this by numbering its input, In [3] for instance means this block was executed third.
If you get in a complete mess you can also clear all output, without removing the input and re-execute the code blocks in order.
All of the standard Python help is available from within the notebook. This means that you can type
help(something)
to get help about something
. For example, type and execute help(print)
to get help about the print function...
help(print)
You can set the name of the notebook by clicking on the name at the top, and entering the new name e.g episode1
(The file extension is ipynb
which means 'interactive python notebook'). You can save the notebook on your local machine using the File
menu at the top, using Download as
e.g. saving as a normal Python script, or as a PDF file, webpage or downloading the python notebook itself.
You can run this notebook on your own computer by installing Jupyter. More information is available at the Jupyter website.
Another cool thing is that you can mix documentation into your notebook. Do this by selecting a cell and using the dropdown menu to change the cell type to Markdown
.
You can now add documentation, using markdown formatting. For example, use the hash symbol for headers...
NB In what follows you do not need to include the line %% markdown
, this some magic we use to generate the teaching material. More on magic later ...
%%markdown
# This is a big header
## This is a subheading
### This is a sub-sub-heading
%%markdown
[link to github](https://github.com)