Intro to notebooks

Intro to notebooks#

The old way of programming is via a text editor. You write code, turn the code into a program, and then run the program in your command line. Jupyter notebooks make programming easier: They combine code, code outputs (plots, tables), and text descriptions in a single file.

They are a great way for working with python on small projects.

Cells#

Notebooks are composed of so-called cells. They come in two types:

  • Code cells for adding python code.

  • Markdown cells for adding text.

You can add cells using the “+” button in the toolbar.

You can edit existing cells by double clicking them.

You can “run” a cell by clicking “Shift+Enter” or by clicking the play (run) button in the toolbar above. This will execute the python code or format the markdown code.

Code Cells#

Code cells contain python code. Running code cells will execute the code. An asterisk (*) will indicate that the cell is running. Code cells do not have to be run in order - you can flexibly run and re-run cells in any order you want. The result of the last line in a cell will be printed automatically. We will learn about more ways of printing results in a bit.

5+5
10
a = 10
b = 20
a + b
30

Markdown cells#

Markdown cells allow you to write normal text but come with extra options to format text:

  • single underscores _italic_: italic

  • double underscores __bold__: bold

  • 1 or more hash tags # Header: Headers at different levels

  • back ticks print(x) for inline code: print(x)

  • mathematical equations with $y = ax + b$: \(y = ax + b\)

You can create formatted code blocks using triple back ticks “``` … ```”

x = 10
print(x)