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:
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:
print("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.
"Hello world!"
Code cells can contain more than one line of code:
a = 1
b = 2
print(a+b)
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:
print(a*b)
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.
a = a + 1
print(a)
(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: