Jupyter

Jupyter#

We’ll be using Jupyter for all of our examples – this allows us to run python in a web-based notebook, keeping a history of input and output, along with text and images.

For Jupyter help, visit: https://jupyter.readthedocs.io/en/latest/content-quickstart.html

We interact with python by typing into cells in the notebook. By default, a cell is a code cell, which means that you can enter any valid python code into it and run it. Another important type of cell is a markdown cell. This lets you put text, with different formatting (italics, bold, etc) that describes what the notebook is doing.

You can change the cell type via the menu at the top, or using the shortcuts:

  • ctrl-m m : mark down cell

  • ctrl-m y : code cell

Some useful short-cuts:

  • shift+enter = run cell and jump to the next (creating a new cell if there is no other new one)

  • ctrl+enter = run cell-in place

  • alt+enter = run cell and insert a new one below

ctrl+m h lists other commands

A “markdown cell” enables you to typeset LaTeX equations right in your notebook. Just put them in $ or $$:

\[\frac{\partial \rho}{\partial t} + \nabla \cdot (\rho U) = 0\]

Warning

When you work through a notebook, everything you did in previous cells is still in memory and known by python, so you can refer to functions and variables that were previously defined. Even if you go up to the top of a notebook and insert a cell, all the information done earlier in your notebook session is still defined – it doesn’t matter where physically you are in the notebook. If you want to reset things, you can use the options under the Kernel menu.

Quick Exercise

Create a new cell below this one. Make sure that it is a code cell, and enter the following code and run it:


 print("Hello, World")
 

print() is a function in python that takes arguments (in the ()) and outputs to the screen. You can print multiple quantities at once like:

print(1, 2, 3)
1 2 3

Note that the default behavior in Jupyter is to print the return value from the last statement in a cell, so we don’t need to print if we just want the value of something like:

a = 10
a
10