“Batteries included”: comes with lots of scientific libraries.
Once you have installed Python and the Jupyter Notebook requirements, open a shell and type:
$ jupyter notebook
This will start a Jupyter Notebook server and open your default web browser.
You can type code into the browser and see the result when the web page talks to the server.
This has several advantages:
You can easily type, edit, and copy and paste blocks of code.
Tab complete allows you to easily access the names of things you are using
and learn more about them.
It allows you to annotate your code with links, different sized text, bullets, etc.
to make it more accessible to you and your collaborators.
It allows you to display figures next to the code that produces them
to tell a complete story of the analysis.
FIXME: diagram
How It’s Stored
The notebook file is stored in a format called JSON.
Just like a webpage, what’s saved looks different from what you see in your browser.
But this format allows Jupyter to mix software (in several languages) with documentation
and graphics, all in one file.
The Notebook has Control and Edit modes.
Open a new notebook from the dropdown menu in the top right corner of the file browser page.
Each notebook contains one or more cells of various types.
Code vs. Text
We often use the term “code” to mean
“the source code of software written in a language such as Python”.
A “code cell” in a Notebook is a cell that contains software;
a “text cell” is one that contains ordinary prose written for human beings.
If you press “esc” and “return” alternately,
the outer border of your code cell will change from gray to green.
The difference in color is subtle.
These are the control (gray) and edit (green) modes of your notebook.
If you use the “esc” and “return” keys to make the surround gray
and then press the “H” key,
a list of all the shortcut keys will appear.
When in control mode (esc/gray),
The “B” key will make a new cell below the currently selected cell.
The “A” key will make one above.
The “X” key will delete the current cell.
All actions can be done using the menus,
but there are lots of keyboard shortcuts to speed things up.
If you remember the “esc” and “H” shortcut, you will be able to find out all the rest.
Use the keyboard and mouse to select and edit cells.
Pressing the “return” key turns the surround green to
signify edit mode and you can type into the cell.
Because we want to be able to write many lines of code in a single cell,
pressing the “return” key when the border is green moves the cursor to the next line in the cell
just like in a text editor.
We need some other way to tell the Notebook we want to run what’s in the cell.
Pressing the “return” key and the “shift” key together will execute the contents of the cell.
The Notebook will turn Markdown into pretty-printed documentation.
Like sticky notes rather than locations in memory.
Use var = value to do assignment.
The variable is created when a value is assigned to it.
age = 42
first_name = 'Ahmed'
Variable names:
cannot start with a digit
cannot contain spaces, quotation marks, or other punctuation
may contain an underscore (typically used to separate words in long variable names)
Underscores at the start like __alistairs_real_age have a special meaning
so we won’t do that until we understand the convention.
Use print to display values.
Built-in function print displays things as text.
print(first_name, 'is', age, 'years old')
Ahmed is 42 years old
print automatically puts a single space between items and a newline at the end.
Variables must be created before they are used.
If a variable doesn’t exist yet, or if the name has been mis-spelled,
Python reports an error.
Unlike some languages, which “guess” a default value.
print(last_name)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-c1fbb4e96102> in <module>()
----> 1 print(last_name)
NameError: name 'last_name' is not defined
The last line of an error message is usually the most informative.
Variables defined in one cell exist in all other cells once executed,
so the relative location of cells in the notebook do not matter
(i.e., cells lower down can still affect those above).
Python provides the usual atomic types and operators.
int, float, and str.
+, -, *, /, ‘%’, and ‘^’ on numbers.
int/int keeps the fractional part.
int//int is integer division.
+ and * on strings.
Which can be written using either single or double quotes.
And may include the usual C escape characters like \t and \n.
Index and slice to get information out of a string.
Locations are numbered from 0 rather than 1.
Negative indices count backward from the end of the string.
Slices include the lower bound but exclude the upper bound.
Cannot change the characters in a string after it has been created.
Python considers the string to be a single value with parts,
not a collection of values.
element[0] = 'C'
TypeError: 'str' object does not support item assignment
Indexing beyond the end of a collection is an error.
Python reports an IndexError if we attempt to access a value that doesn’t exist.
print('99th element of element is:', element[99])
IndexError: string index out of range
Creating Lists in Markdown
Create a nested list in a Markdown cell in a notebook that looks like this:
Get funding.
Do work.
Design experiment.
Collect data.
Analyze.
Write up.
Publish.
More Math
What is displayed when a Python cell in a notebook
that contains several calculations is executed?
For example, what happens when this cell is executed?
7 * 3
2 + 1
Change an Existing Cell from Code to Markdown
What happens if you write some Python in a code cell
and then you switch it to a Markdown cell?
For example,
put the following in a code cell:
x = 6 * 7 + 12
print(x)
And then run it with shift+return to be sure that it works as a code cell.
Now go back to the cell and use escape+M to switch the cell to Markdown
and “run” it with shift+return.
What happened and how might this be useful?
Equations
Standard Markdown (such as we’re using for these notes) won’t render equations,
but the Notebook will.
Create a new Markdown cell
and enter the following:
$\Sigma_{i=1}^{N} 2^{-i} \approx 1$
(It’s probably easier to copy and paste.)
What does it display?
What do you think the underscore _, circumflex ^, and dollar sign $ do?
Swapping Values
Draw a table showing the values of the variables in this program
after each statement is executed.
In simple terms, what do the last three lines of this program do?