ubuntu2004
Coding in Jupyter Notebooks
Over the next five weeks you will learn to code in Python. Python is an easy-to-learn, but very powerful, programming language. As well as powering many of the apps on your phone, Python is widely used in many areas of science including the Biological and Medical Sciences.
You will use Jupyter Notebooks (like the one you're reading this in now) to type in Python code and immediately run it to see the output.
Jupyter Notebooks are widely used in biological data analysis as well as within the field of data science, partly owing to its ability to incorporate code and graphs all within a single notebook.
A Notebook consists of a series of cells. For example, this text is in a Markdown cell. The following cell is a Code cell in which Python code is written and executed.
You can tell the type of a cell by selecting the cell, and looking at the toolbar at the top of the page. For example, try clicking on this cell. You should see the cell type menu displaying Markdown. Also the left margin is empty.
In a code cell the cell type menu displays Code and the left margin contains In []:
Command mode and edit mode
In a notebook, there are two modes: edit mode and command mode. By default a notebook begins in command mode. In order to edit a cell, you need to be in edit mode.
Markdown cells
For example, a markdown cell might look like this in command mode (Note: the following few cells are not actually cells; they are images and just look like cells. This is for demonstration purposes only.)
Then, when you press enter or click on "Edit" on the right hand side of the cell, it will change to green edit mode:
Now, when we press escape, it will change back to blue command mode:
However, you'll notice that the cell no longer looks like it did originally. This is because Jupyter will only render the markdown when we tell it to. To do this, we need to run the cell by pressing ctrl-enter or ctrl-return, and then it will go back to looking like it did originally:
Code cells
For code cells, it is pretty much the same thing. This is what a code cell looks like in command mode (again, the next few cells look like cells, but are just images):
If we press enter, it will change to greem edit mode:
And pressing escape will also go back to blue command mode:
If we were to press ctrl-Enter or ctrl-return like we did for the markdown cell, this would run the code in the code cell:
Running code cells
Code cells can contain any valid Python code in them. When we run the cell, the code is executed and any output is displayed.
Try running the following cell and see what it prints out (don't worry about understanding the code for now):
You'll notice that the output beneath the cell corresponds to the print
statements in the code. Here is another example which only prints out the final total.
The Jupyter kernel
When we first start a notebook, we are also starting what is called a kernel. This is a special program that runs in the background and executes Python code. Whenever we run a code cell, we are telling the kernel to execute the code that is in the cell, and to print the output (if any).
Restarting the kernel
If you are sure your code is correct but it keeps giving you errors then restarting the kernel often helps.
The restart kernel button is the two circular arrow button in the toolbar (indicated by the red circle in this image).
Help with Jupyer Notebooks
There are many keyboard shortcuts for Jupyter notebooks. To see a full list of these click on HelpKeyboard Shortcuts. There is also help on many other topics