This lesson is being piloted (Beta version)

Running Jupyter

Overview

Teaching: 40 min
Exercises: 20 min
Questions
  • How can I run Python programs?

Objectives
  • Launch the Jupyter Notebook, create new notebooks, and exit the Notebook.

  • Create Markdown cells in a notebook.

  • Create and run Python cells in a notebook.

  • Get help in Jupyter

Python programs are plain text files.

Use the Jupyter Notebook for editing and running Python.

Opening a Jupyter Notebook

  • Once you have installed Anaconda, search for Jupyter Notebook in the Windows search and click to start it.
  • Alternately you can open a shell and type:
    $ jupyter notebook
    

The Notebook has Control and Edit modes.

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.

Use the keyboard and mouse to select and edit cells.

Pause for a play.

The Notebook will turn Markdown into pretty-printed documentation.

Markdown does most of what HTML does.

*   Use asterisks
*   to create
*   bullet lists.
  • Use asterisks
  • to create
  • bullet lists.
1.  Use numbers
1.  to create
1.  numbered lists.
  1. Use numbers
  2. to create
  3. numbered lists.
# A Level-1 Heading

A Level-1 Heading

## A Level-2 Heading (etc.)

A Level-2 Heading (etc.)

Line breaks
don't matter.

But blank lines
create new paragraphs.

Line breaks don’t matter.

But blank lines create new paragraphs.

[Create links](http://software-carpentry.org) with `[...](...)`.
Or use [named links][data_carpentry].

[links]: http://datacarpentry.org

Create links with [...](...). Or use named links.

Getting HELP!!

Get help inside Python: the help() function

Knowing how to find and navigate the online documentation is vital, but there are times when looking something up inside an interactive Python session is also useful. The built-in help() function looks up the documentation for Python objects. For example:

>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

Get help easily in a Jupyter Notebook

In a Jupyter Notebook, there is another option for the help() function. Instead of calling help(), simply prepend a Python object with ? to display HTML formatted help in a separate panel. If you are using or have access to a Jupyter Notebook, compare the output from the following two statements:

help(print)

?print

The Jupyter Notebook has another way to get help.

  • Place the cursor inside the parenthesis of the function, hold down Shift, and press Tab.

Creating Lists in Markdown

Create a nested list in a Markdown cell in a notebook that looks like this:

  1. Get funding.
  2. Do work.
    • Design experiment.
    • Collect data.
    • Analyse.
  3. Write up.
  4. 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?

Mathematics

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?

Key Points

  • Python programs are plain text files.

  • Use the Jupyter Notebook for editing and running Python.

  • The Notebook has Control and Edit modes.

  • Use the keyboard and mouse to select and edit cells.

  • The Notebook will turn Markdown into pretty-printed documentation.

  • Markdown does most of what HTML does.

  • In Jupyter Notebooks, the ? command will display the same result as help() in a separate panel, with formatted text.

  • The official Python documentation is a good reference to the core Python language.