This notebook is meant to provide an introduction to using jupyter notebooks. By the end of this notebook you should:
The Jupyter Notebook is a powerful tool for interactively developing and presenting data science projects. It allows you to use a web browser (as opposed to a terminal or other software) to interact with a computer language interpreter (typically Python) to send commands and receive outputs. It then displays the outputs in HTML below the executed cells, and allows users to add rich text comments or annotations around code cells using markdown.
For this class, we will be using notebooks that run on a server in the cloud. This is convenient because you will never have to install software on your own computer and can just connect to your browser to complete assignments or exercises. It does not matter whether you are using Mac, Windows, Linux, Chrome, etc.
For your own future reference, you can install jupyter notebooks onto your own computer to use as a research tool. There are many resources online to learn more about this but it is outside the scope of this class.
Double click on the text below that says "Hello world". When you do you should see the text open up into an interactive cell. This cell is set up to execute a type of code called Markdown, which is a language for formatting text, much like HTML (which is called a MarkUp language). Markdown is much easier to learn than HTML, and for that reason has become a very popular tool for simple formatting and styling of text. To transform the cell back into rendered text you must execute it, which can be done by pressing the 'Run' button in the notebook tool bar while the cell is selected, or, by holding down the control key and pressing enter. We recommend that you learn the hotkey commands, since it will make you a more efficient notebook user. You can click on the keyboard icon in the toolbar to find a list of hotkey commands, or find many resources online (like this one).
My first executed Markdown cell
The text on the line above this is a level-3 header.
In the menu and tool bar you can find options to change the type of cells. The two main types we are interested in are Markdown cells, as you just saw above, and Code cells, which are used to execute code. Most of the time code cells will produce some kind of output which is returned when the code is executed. In jupyter notebooks the returned code will be saved in the output area below the executed cell.
As an example, let's execute the cell below which contains two Python commands, one storing an integer as a variable x
, and a second command to print the variable's value. The code also includes lines that start with hash marks (#) which are called comment lines, these are not executed and are meant for leaving notes about the code.
In [1]:
# store value to the variable x
x = 3
# print the variable x
print(x)
The default language in Jupyter notebooks is Python, but we can execute some other languages as well. The bash language is one of several related languages used in a command line terminal to execute programs. We can use it in Jupyter notebooks by simply appending %%bash
to the top of the cell. You can see that the syntax to perform the same task we did above in Python is similar but a little different in bash. Learning the idiosyncracies of different coding languages takes time, and it is easiest to learn by example.
In [2]:
%%bash
# store value to a variable x
x=3
# print the variable
echo $x
It is OK if you are unfamiliar with these coding languages. You will not need to write your own code for the purposes of this course, but simply to try to interpret or modify written code so that you can learn about how modern evolutionary biologists use code.
from IPython.display import HTML def success_message(): msg = "Great job, you completed the tutorial" fmt = "
{}
" return HTML(fmt.format(msg))success_message()
Jupyter notebooks auto-save every few minutes, which helps to keep you from losing your progress if you disconnect from the notebook, or accidentally delete something important. You can always restart a notebook from the toolbar on top if you somehow crash it. When you reach the end of a notebook you should look back over the entire document to make sure you have answered all of the required questions or actions. If you have, then save the document one last time by clicking on Save and Checkpoint
in the File Menu (or click on the disk in the upper left).
When you are finished with a notebook and ready to submit it we ask that you download an HTML version of the notebook to submit. To do this, go to the 'File Menu', then 'Download as', and select HTML. Upload the HTML notebook to Courseworks to submit your assignment.