Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

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.

# this is a code cell

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.

When you are in command mode, you can press enter to switch to edit mode. The left border of the cell you currently have selected will turn green, and a cursor will appear.
When you are in edit mode, you can press escape to switch to command mode. The left border of the cell you currently have selected will turn blue, and the cursor will disappear.

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.

We can execute cells with ctrl-enter or ctrl-return which will keep the cell selected, or shift-enter or shift-return which will select the next cell.

Try running the following cell and see what it prints out (don't worry about understanding the code for now):

print("Printing cumulative sum from 1-10:") total = 0 for i in range(1, 11): total += i print( f'Sum of 1 to {i} is {total}') print( 'Done printing numbers.' )
Printing cumulative sum from 1-10: Sum of 1 to 1 is 1 Sum of 1 to 2 is 3 Sum of 1 to 3 is 6 Sum of 1 to 4 is 10 Sum of 1 to 5 is 15 Sum of 1 to 6 is 21 Sum of 1 to 7 is 28 Sum of 1 to 8 is 36 Sum of 1 to 9 is 45 Sum of 1 to 10 is 55 Done printing numbers.

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.

total = 0 for i in range(1, 11): total += i print(total)
55

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 Help→\rightarrowKeyboard Shortcuts. There is also help on many other topics

To learn a little more about what things are what in the IPython Notebook, check out the user interface tour, which you can access by going to Help→\rightarrowUser Interface Tour.

Next Notebook