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.

Keypoints:

  • Python programs are plain text files.
  • Use the Jupyter Notebook for editing and running Python.
  • Use the keyboard and mouse to select and edit cells.
  • The Notebook will turn Markdown into pretty-printed documentation.

There are multiple ways to run Python code

  • Python programmes can be written as plain text files using a text editor
  • They have the .py extension to let everyone (including the operating system) know it is a Python program. This is convention, not a requirement. We will use plain text files for writing Python code towards the end of this course.
  • For this part of the course we are going to use [Jupyter Notebooks][jupyter].
  • The Notebook provides code completion and other helpful features.
  • Notebook files have the extension .ipynb to distinguish them from plain-text Python programs.

You can use the Jupyter Notebook for editing and running Python.

  • When we type jupyter notebook in a terminal (or Git Bash) we start a Jupyter Notebook server and open the default web browser.
  • The server runs locally on your machine only and does not use an internet connection.
  • 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. 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 source code, text, and images, all in one file.

Tip: You can view this page as a Notebook by clicking on either the Binder or Colab button at the top of this page.

Use the keyboard and mouse to select and edit cells.

  • You can create a new cell by clicking on Insert at the top of the Notebook.
  • Pressing the "shift" and the "enter" key together will execute the contents of the cell.

Use command mode to speed things up

  • You can also use keyboard shortcuts by entering command mode using the esc key.
  • Notice that the border of your cell turns blue.
  • Type a shortcut command - for example b to create a new cell.
  • The list of shortcuts can be found using h.
  • To exit command mode and start editing the cell press enter.

The Notebook will turn Markdown into pretty-printed documentation.

  • Notebooks can also render [Markdown][markdown] which is a simple plain-text format for writing lists, links, and other things that might go into a web page.
  • You can turn cell into a Markdown cell by selecting Markdown from the drop-down menu at the top of the Notebook
  • In [ ]: will disappear to show it is no longer a code cell and you will be able to write in Markdown.

Lists

    *   Use asterisks
    *   to create
    *   bullet lists.
  • Use asterisks
  • to create
  • bullet lists.
1.  Use numbers
2.  to create
3.  numbered lists.
  1. Use numbers
  2. to create
  3. numbered lists.
1.  You can
2.  also
    * create sub-lists
  1. You can
  2. also
    • create sub-lists

Headings

# A Level-1 Heading

A Level-1 Heading

## A Level-2 Heading (etc)

A Level-2 Heading (etc)

Whitespace

Line breaks
don't matter.

But blank lines
create new paragraphs.

Line breaks don't matter.

But blank lines create new paragraphs.

Basic formatting

You can easily:

  • use italics with *italics*
  • use bold with **bold**
  • Create links with [...](...).