My First Jupyter Notebook

This notebook is here to show you Jupyter Notebook works and what you can do with them.

Notebooks are separated into blocks called cells. The "+" button at the top inserts a new cell. Each cell can contain one of three different types of text:

  1. Code - This is text that is ran as Python code every time the notebook is ran. These cells are where you will put the commands you actually want the notebook to execute.
  2. Markdown - This is text that is displayed as text to read. It is not executed. Markdown cells support formatting, such as italics or bold.
  3. Raw - This displays text, but ignores all Markdown formatting. For example, this cell repeated below in raw text is:
**My First Jupyter Notebook** This notebook is here to show you Jupyter Notebook works and what you can do with them. Notebooks are separated into blocks called cells. Each cell can contain one of three different types of text: 1. *Code* - This is text that is ran as Python code every time the notebook is ran. These cells are where you will put the commands you actually want the notebook to execute. 2. *Markdown* - This is text that is displayed as text to read. It is not executed. Markdown cells support formatting, such as *italics* or **bold**. 3. *Raw* - This displays text, but ignores all Markdown formatting. For example, this cell repeated below in raw text is:

Running Code

To run code, make a new cell, label it as code, and add the commands you want to run. Run the cell by pressing the "play" button at top, which is the right-facing triangle. On many systems (such as Windows), the ctrl+enter key combination will run a cell too. Outputs are displayed below the cell.

An an example, remember "Hello World" from before:

In [1]:
print("Hello world!")
Hello world!

Like the interpreter, if you have just a value or variable with no command, Jupyter will display the output without you telling it to.

In [2]:
"Hello world!"
Out[2]:
'Hello world!'

Code cells can contain more than one line of code:

In [3]:
a = 1
b = 2
print(a+b)
3

The way Jupyter works is that each cell is run as if you ran the lines in the interpreter from before. So in our example above, the variables "a" and "b" persist for the rest of the Notebook. Here we can use a cell below to work with the values again:

In [4]:
print(a*b)
2

What do you think will happen if you run a cell more than once? Think about it for a bit and then try running this cell twice.

In [5]:
a = a + 1
print(a)
2

(continued below)
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .

If you run a cell more than once, Jupyter will use the updated values the second time. Jupyter will use whatever it happens to know about at the time you run a cell to evaluate it, which might not always be the cells above it. You can see that if you run the code two blocks above it will have a different result now as well.

This is generally something we want to avoid. When you make Jupyter projects, you might have to rerun cells over and over as you keep fixing errors that you made. If you press the double-triangle above, Jupyter will restart and run all of you code, starting from the top down. Unless your code takes a while, this is the best way of running it.

Markdown

Markdown is a simple but powerful way to make your text more readable. You'll be mostly writing the Markdown comments for yourself, but it's good to know how to use it in the future when other people will be reading your Notebooks.

The Markdown formatting will automatically display as you're typing. To finish typing and hide the formatting marks, press the same button you use to run code.

Important things to know:

  1. When you want to bold words you surround words with double asterisks like **this**. If you want to italicize words, you use one set like *this*. If you want to do both, use ***three***.
  2. If you just want asterisks themselves, you must type "\*". Similarly, to type a single backslash you need to type "\\". If you didn't know, adding a backslash before special characters will treat them as normal characters in most places.
  3. Use two enters to start a new paragraph. If you press enter once, Markdown will just ignore your new line. If you want to avoid this, end a line with two spaces.
  4. Markdown will automatically assume any list like there where you start a line with "#." is a numbered list. It will number it correctly for you.
In [ ]: