Using Jupyter (Need)

What to expect in this chapter

Jupyter is an interactive environment that allows you to have a (sort of) conversation with Python. I will show you how to get this to work. I will also show you features like Markdown, allowing you to ‘add value’ to your code.

Incidentally, Jupyter works so that you can ask it to do something, and it immediately shows you the result. In fact, the older name for Jupyter Notebooks was ‘IPython notebooks’, meaning ‘interactive Python notebooks’.

1 Getting ready

  1. Use the terminal to navigate to the folder Using Jupyter in your Learning Portfolio.

  2. Start Jupyter notebooks through the terminal. Please refer to the previous chapter if you have forgotten how to do this.

  3. Use the button New to create a new Python 3 (ipykernel) notebook.

  4. Click the Untitled at the top (next to the Jupiter logo) and rename the file to using-jupyter_need.

2 Getting started with Jupyter

2.1 It is all about running cells

The basic unit of a Jupyter Notebook is a cell. A cell can either accept Python code or text in a format called Markdown.

A cell ready to accept Python is called a code cell, while one ready for Markdown is called a Markdown cell. To ‘get things done’, we Run a cell by using the Run button or the keyboard shortcut CTRL + ENTER (or CMD + ENTER).

Running a code cell invokes Python while running a Markdown cell renders (i.e., makes it look pretty) your Markdown text. You will see how all this works in a bit, so don’t worry if things sound a tad cryptic at the moment.

2.2 A traditional start

Let’s follow a programming tradition and use Jupyter to print Hello World!.

  1. Select a cell and convert it into a code cell.
    Actually, all new cells are, by default, code cells.

  2. Then type the following and run the command (i.e., pass the instruction to Python) by pressing the run button or using the keyboard shortcut CTRL + ENTER (or CMD + ENTER).

    print('Hello World!')

    Surprise, surprise! You should see the words Hello World! as the output.

    Hello world!

    You will also see that the number before your code cell changes. This number is used to keep track of the history of the commands you have issued. It helps keep track of which input corresponds to which output.

3 All about Markdown

Markdown is a minimalist ‘language’ that helps you to format your writing. Despite being lightweight, it is very powerful and gives fantastic results. Because it is simple and distraction-free, I often prefer to use Markdown when I write, as it helps me focus on the content.

Let me now quickly take you through some basic Markdown, which will suffice for 73.

3.1 Rendering Markdown cells

To render your text:

  1. Select a cell and convert it to a Markdown cell (If you like, you can use the keyboard shortcut ESC + M),
  2. Input your text and
  3. Run.

To start, copy and paste the following (from the Guide1) and run the cell.

The ships hung in the sky in much the same way that bricks don’t.

You should see the text rendered.

3.2 Some basic syntax

The table shows some basic Markdown syntax to modify how some text is rendered.

Style Syntax
Bold ** ** or __ __
Italic * * or _ _
All bold and italic *** ***
Subscript <sub> </sub>
Superscript <sup> </sup>

Let’s use it to modify our previous text as follows. (By the way, no more copying and pasting!)

The ships *hung in the sky* in much the same way that **bricks don’t.**<sup>1</sup><sub>QUOTE</sub>

This will be rendered as:

The ships hung in the sky in much the same way that bricks don’t.1QUOTE

3.3 Headings

Markdown uses # for headings as follows.

# The largest heading

## The second-largest heading

### The third-largest heading

#### The smallest heading

3.4 Displaying Code

If you want to mention code inline, you can enclose it between two backticks (`), like `print('Hello World')`. This will be rendered as print('Hello World!').

You can also have a block of code by using the following syntax.

```python
print('Hello World!')
```

This will be rendered as:

print('Hello World!')

3.6 Images

Markdown also lets you easily include images from your computer or the web. The syntax is similar to links, except there is a ! in front.

Let me show you by inserting the NUS logo from the University’s front page (You can get the link to any image by right-clicking and copying the image address).

The syntax is:

![](https://nus.edu.sg/images/default-source/base/logo.png)

This will give you:

You can also similarly include images on your computer. Just put the filename instead of the address. Again, you need to give enough information (full or relative path, see chapter os) for Jupyter to be able to find the file.

A fly in the ointment

Sometimes, you might have to copy the image file to the folder/directory containing the .ipynb to successfully include local images.

I think this happens mainly for Windows users. I am sorry2.

3.7 Tables

Markdown following syntax to create tables:

| A    |  B   |    C |
| :--- | :--: | ---: |
| a1   |  b1  |   c1 |
| a2   |  b2  |   c2 |
| a3   |  b3  |   c3 |

Which will be rendered as:

A B C
a1 b1 c1
a2 b2 c2
a3 b3 c3

In my experience, tables are the most annoying things to create and format in a document3. So I find it easier to organise the information in Excel or Sheets and then use a web tool like Table Generator to create the tables.

3.8 Lists

You can have numbered or unnumbered lists in Markdown. Below are a few examples. I have shown the code first and then what it will look like when rendered.

1. Master Yoda
1. Luke Skywalker
1. Anakin Skywalker
  1. Master Yoda
  2. Luke Skywalker
  3. Anakin Skywalker
- Master Yoda
- Luke Skywalker
- Anakin Skywalker
  • Master Yoda
  • Luke Skywalker
  • Anakin Skywalker
1. Master Yoda
   1. Was a Jedi
   1. Was a bit green
1. Luke Skywalker
   1. Was a Jedi
   1. Is Anakin's son.
1. Anakin Skywalker
   1. Was a Jedi then became a baddie
   1. Is famous for saying 'Luke, I am your father'
  1. Master Yoda
    1. Was a Jedi
    2. Was a bit green
  2. Luke Skywalker
    1. Was a Jedi
    2. Is Anakin’s son.
  3. Anakin Skywalker
    1. Was a Jedi then became a baddie
    2. Is famous for saying ‘Luke, I am your father’
1. Master Yoda
   - Was a Jedi
   - Was a bit green
1. Luke Skywalker
   - Was a Jedi
   - Is Anakin's son.
1. Anakin Skywalker
   - Was a Jedi then, became a baddie
   - Is famous for saying, _'Luke, I am your father'_.
  1. Master Yoda
    • Was a Jedi
    • Was a bit green
  2. Luke Skywalker
    • Was a Jedi
    • Is Anakin’s son.
  3. Anakin Skywalker
    • Was a Jedi then, became a baddie
    • Is famous for saying, ‘Luke, I am your father’.

3.9 Equations

Markdown can produce phenomenal math equations4.

  • If you want to have inline math use two $ signs like $\sqrt{b^2-4ac}$, which will be rendered as \(\sqrt{b^2-4ac}\).

  • If you want a math block, you can use the following:

    $$
    x = \dfrac{-b \pm \sqrt{b^2-4ac}}{2a}
    $$

    Which will be rendered as:

\[ x = \dfrac{-b \pm \sqrt{b^2-4ac}}{2a} \]

Here are some helpful math commands that you can use. For more details, refer to the mathematical documentation at Overleaf.

Input Rendered
Fraction $\dfrac{y}{x}$ \(\dfrac{y}{x}\)
Subscript $x_{a}$ \(x_{a}\)
Power $x^{(y+z)}$ \(x^{(y+z)}\)
Squareroot $\sqrt{a+b+c}$ \(\sqrt{a+b+c}\)
Sum $\sum_{n=1}^{n=\infty} x_n$ \(\sum_{n=1}^{n=\infty} x_n\)
Integral $\int_{x=1}^{x=\infty} f(x)dx$ \(\int_{x=1}^{x=\infty} f(x)dx\)
Not equal a \ne b \(a \ne b\)
Less than $a \lt b$ \(a \lt b\)
Less than or equal to $a \leq b$ \(a \leq b\)
Greater than $a \gt b$ \(a \gt b\)
Greater than or equal to $a \geq b$ \(a \geq b\)
Greek letters $\alpha, \beta, \gamma, \pi, \lambda$ \(\alpha, \beta, \gamma, \pi, \lambda\)
Back to top

Footnotes

  1. You and I will have problems if you do not know about The Hitchhiker’s Guide to the Galaxy.↩︎

  2. that you are using Windows.↩︎

  3. especially with doubly annoying software like Word↩︎

  4. Using a \(\LaTeX\) engine called MathJax↩︎