2: Runnng and Creating Jupyter Notebooks#

Learning Objectives:#

In this class you will be creating and using Jupyter Notebooks. You will be writing or modifying code in jupyter notebooks and then using Markdown language to describe the code. You will also be using an AI tutor that generates responses in markdown language, and so the goal of this chapter is to introduce you to running code and provide a reference for writing content in markdown cells.

Topics Covered#

  1. Types of Cells

  2. Keyboard Shortcuts

  3. Running Multiple Notebooks

  4. Code Cells

  5. Markdown Cells

  6. Unicode

  7. LaTex

1. Starting Jupyter Lab#

Jupyter Lab is actually a web application that runs on your browser. Jupyter Lab allows you to run multiple Jupyter notebooks concurrently.

  1. Jupyter Lab - starts the Jupyter Lab server, which is a Python application. This will provide you with a url on your local host that starts with something like http://localhost:8888/lab/tree…

  2. Paste the URL into the address bar of your browser and open the page.

  3. If this is the first time it will ask you to pick a kernel. When you pick the kernel:

    • Jupyter starts a new Python process (interpreter) in the background.

    • This interpreter is initialized with the specific environment you selected.

    • The kernel acts as a bridge between this Python interpreter and your Jupyter notebook. **Note: the kernel shows in the upper right corner of your notebook. If it is not the kernel you wish to use you should click on it and choose the one you want

image.png picking a new kernel if the default one is not the one you want. Note, in this image of a Jupyter Lab there are 5 notebooks running, one in each tab. You can actually run different notebooks in the same lab on different kernels, and you need to be sure the kernel you are running has the packages you need.

1. Types of Cells#

A Jupyter notebook has three types of cells and we will be using the first two types

  • Code Cells: (can run python or multiple other scripts)

  • Markdown Cells (a text formatting language we will use)

  • Raw Cells (these can contain code that is not executed, and are involved in converting notebooks to other types. Unless you get into advanced documentation you will probably not use these during this class)

2. Keyboard Shortcuts#

The easiest way to run a Jupyter notebook is to use a keyboard shortcut. Here are several useful keyboard shortcuts

  • shift-enter run cell and move to next cell (creates one if there is none below it)

  • ctrl-enter run cell and stay in same cell

  • alt-enter run cell and create new cell below it

  • esc-m convert cell to markdown

  • esc-y convert cell to code

3. Running Multiple Notebooks#

It is easy to open multiple notebooks, simply

  • locate the notebook you want in the “File Browser” of any notebook (top icon of left frame)

  • right click and choose Copy Shareable Link

  • Paste link into new browser window.

    image.png

Warning be careful not to have the same notebook open at the same time in different windows. If you do, Jupyter will give you a warning if you try and save a file that has been altered in the other browser. image.png

4. Code Cells#

These allow you to run code in your notebook. In this class we will be running python, but there are other options. If you look at the upper right corner of the notebook you can see the python kernel that is being run

image.png

Show Line Numbers#

The easiest way to show line numbers is go to the main menu and choose View > Show Line Numbers , and this will toggle them on or off.

The following cell is a code cell, and when you press shift enter it will execute the code and move to the next cell. This will be the most common way we will run cells. In this cell we are printing a string of words (the material in parenthesis of the print function), and performing an arithmetic operation twice. Note, a Jupyter notebook automatically outputs the last expression in a cell, and so 2_3 was output, while 2+2 was not. If we want to output the 2+2 we need to print it, as is done in the subsequent cell.

# A "#" makes a line a comment line and is not executed
print("Hello World") # We will discuss the print function when we get to the python chapter.
2+2
2+3
Hello World
5
# A "#" makes a line a comment line and is not executed
print("Hello World")
print(2+2)
2+3
Hello World
4
5

5. Markdown Cells#

Markdown Formatting#

Use of backticks (`)#

Tips about backticks (below the tilde on your keyboard) in Jupyter notebooks and markdown in general:

  1. Single backticks (code) are used for inline code snippets within text.

  2. Triple backticks (```) are used for code blocks, allowing you to display multiple lines of code.

Headings#

Use # symbol followed by a space

# Heading 1
## Heading 2
### Heading 3

(note, if you look at the above code you will see that backticks were used to prevent the # from turning into a heading)

Unordered lists#

Use symbols -, * or + followed by a space, I tend to use - all the time

* item 1 
* item 2
    - indent 1
    - indent 2
+ item 3

gives the following, and it makes no difference which symbol is used.

  • item 1

  • item 2

    • indent 1

    • indent 2

  • item 3

Enumerated (ordered) Lists#

Simply use a number followed by a space.

1. First item
2. Second item
   1. indent
   2. indent
      1. second indent
      2. second indent  
3. Third item

The above code gives the following output

  1. First item

  2. Second item

    1. indent

    2. indent

      1. second indent

      2. second indent

  3. Third item

Tables#

Images#

*paste image in (ctrl-v), the text will look like:

![image.png](attachment:image.png)

**note: Images are not stored as separate file but embedded directly within the notebook file”. This means you can not move images from one notebook to another, but have to make a new copy of them, and paste that new copy.

6. Unicode#

Unicode characters work in code cells. You can copy the character and use it in either code or markdown cells

Table of unicode subscript and superscript characters#

Character

Subscript

Subscript Unicode

Superscript

Superscript Unicode

0

\u2080

\u2070

1

\u2081

¹

\u00B9

2

\u2082

²

\u00B2

3

\u2083

³

\u00B3

4

\u2084

\u2074

5

\u2085

\u2075

6

\u2086

\u2076

7

\u2087

\u2077

8

\u2088

\u2078

9

\u2089

\u2079

+

\u208A

\u207A

-

\u208B

\u207B

=

\u208C

\u207C

(

\u208D

\u207D

)

\u208E

\u207E

print("CH\u2083"+4*"(CH\u2082)"+"NH\u2084\u207A")  # Using unicode character
print("CH₃"+4*"(CH₂)"+"NH₄⁺")  # Copying text that was already subscripted
CH₃(CH₂)(CH₂)(CH₂)(CH₂)NH₄⁺
CH₃(CH₂)(CH₂)(CH₂)(CH₂)NH₄⁺

Table of unicode greek characters#

Greek Symbol

Lowercase Unicode

Uppercase Unicode

α (alpha)

'\u03B1'

'\u0391'

β (beta)

'\u03B2'

'\u0392'

γ (gamma)

'\u03B3'

'\u0393'

δ (delta)

'\u03B4'

'\u0394'

ε (epsilon)

'\u03B5'

'\u0395'

ζ (zeta)

'\u03B6'

'\u0396'

η (eta)

'\u03B7'

'\u0397'

θ (theta)

'\u03B8'

'\u0398'

ι (iota)

'\u03B9'

'\u0399'

κ (kappa)

'\u03BA'

'\u039A'

λ (lambda)

'\u03BB'

'\u039B'

μ (mu)

'\u03BC'

'\u039C'

ν (nu)

'\u03BD'

'\u039D'

ξ (xi)

'\u03BE'

'\u039E'

ο (omicron)

'\u03BF'

'\u039F'

π (pi)

'\u03C0'

'\u03A0'

ρ (rho)

'\u03C1'

'\u03A1'

σ (sigma)

'\u03C3'

'\u03A3'

τ (tau)

'\u03C4'

'\u03A4'

υ (upsilon)

'\u03C5'

'\u03A5'

φ (phi)

'\u03C6'

'\u03A6'

χ (chi)

'\u03C7'

'\u03A7'

ψ (psi)

'\u03C8'

'\u03A8'

ω (omega)

'\u03C9'

'\u03A9'

'\u03BB'
'λ'

Note you can not assign the unicode in a variable assignment (the following code gives an error), but you can copy the symbol from either a code or markdown cell and use that as your variable.

# uncomment the following line and you will see an error when you run it
#'\u03BB' = 560 # nm
λ = 560 # nm 
print(f'λ is {λ}.') 
print(f'\u03BB is {λ}. ') # this is using f-string formatting that we will cover later
print('h = λν')
λ is 560.
λ is 560. 
h = λν

7. Latex (markdown cells)#

LaTeX is a common typesetting system used in scientific and mathematical documents that allows you to put equations in the line of text, or on a unique line. A latex command is preceded by a \ (forward slash). In Jupyter notebooks a the latex command with the forward slash is surrounded by dollar signs. A single dollar sign puts the output in the line of text where it occurs, while double dollar signs put it on its own line.

  • \\(\lamda\\\) - puts \(\lambda\) in line of text

  • \\(\\\)\lambda\\(\\\) puts $\(\lambda\)$ on its own line

Note: if you look at the code you will see two forward slashes in front of the dollar signs, which is an escape sequence, and tells Jupyter not to process the dollar sign

Although there are ways you can use LaTex in the code cells you should avoid this because of code portability issues to standard IDEs (Interactive Development Environments). That is, if you use a “Magic Command” like %%latex in a code cell, you can use LaTex in that cell, but it will not work in many other coding environments.

Tables of common LaTex Commands#

Math Operators#

LaTeX

Symbol

Feature

\pm

±

Plus-minus

\times

×

Times

\div

÷

Division

\neq

Not equal

\approx

Approximate

\equiv

Equivalent

\not\equiv

Not equivalent

\geq

Greater than or equal

\leq

Less than or equal

\sim

Similar

Root Operators#

LaTeX

Symbol

Description

\sqrt{x}

√x

Square root

\sqrt[n]{x}

∛x

nth root

\surd

Square root symbol

\cuberoot{x}

∛x

Cube root (requires amsmath package)

\fourthroot{x}

∜x

Fourth root (requires amsmath package)

Calculus Operators#

LaTeX

Symbol

Description

\int

Integral

\iint

Double integral

\iiint

Triple integral

\oint

Contour integral

\partial

Partial derivative

\nabla

Nabla (del) operator

\lim

lim

Limit

\sum

Summation

\prod

Product

\frac{d}{dx}

d/dx

Derivative

\infty

Infinity

Frations and Matrices#

LaTeX

Description

Rendered Example

\frac{a}{b}

Simple fraction

$\(\frac{a}{b}\)$

\dfrac{a}{b}

Display fraction

$\(\dfrac{a}{b}\)$

\cfrac{a}{b}

Continued fraction

$\(\cfrac{a}{b + \cfrac{c}{d}}\)$

\binom{n}{k}

Binomial coefficient

$\(\binom{n}{k}\)$

\begin{pmatrix} a & b \ c & d \end{pmatrix}

Parenthesis matrix

$\(\begin{pmatrix} a & b \\ c & d \end{pmatrix}\)$

\begin{bmatrix} a & b \ c & d \end{bmatrix}

Bracket matrix

$\(\begin{bmatrix} a & b \\ c & d \end{bmatrix}\)$

\begin{vmatrix} a & b \ c & d \end{vmatrix}

Vertical bar matrix

$\(\begin{vmatrix} a & b \\ c & d \end{vmatrix}\)$

\begin{Vmatrix} a & b \ c & d \end{Vmatrix}

Double vertical bar matrix

$\(\begin{Vmatrix} a & b \\ c & d \end{Vmatrix}\)$

Code Portability Considerations#

I principle you should avoid using LaTeX in code cells, and only use it in markdown cells, or else it may not be portable to standard python IDEs

Acknowledgements#

This content was developed with assistance from Perplexity AI and Chat GPT. Multiple queries were made during the Fall 2024 and the Spring 2025.