Python code and Jupyter notebooks#

Python execution via Python files#

Python code can be written in “.py” files, which can be executed from the command line. We can do this with the simple file hello.py in this folder.

You can execute the file in the terminal via python hello.py.

Python execution via Jupyter Notebooks#

Furthermore, especially for data analysis, Jupyter notebooks are often used to run Python code, to document what the code does, plot results, and add additional explanations and links to resources.

See also

Cell types#

Jupyter notebooks provide two types of cells:

  • Markdown cells for documentation, explanations, etc. (like this one)

  • Code cells to run Python code

To write our first small python program, we type in the Jupyter notebook code cell below. You can double-click in this cell, change the code and execute it by hitting CTRL+ENTER:

print("Hello world")
Hello world

Keyboard shortcuts#

  • When working with Jupyter notebooks, CTRL-ENTER or SHIFT-ENTER is the shortcut people use all the time.

  • If you want to “leave” a cell without executing it, you can also hit ESCAPE.

  • You can add a new cell above the current cell by hitting A and below by hitting B.

  • You can delete cells by hitting D.

There are more shortcuts. You can see them all in the menu above Help > Keyboard shortcuts or by hitting H.

Try it out!

Markdown documentation in Jupyter notebooks#

Markdown is a coding language for describing the formating of text.
To see the Markdown code which describes this text, double-click this cell in your Jupyter notebook.
Hit SHIFT+ENTER to return to the viewing mode.

  • Bullet point

  • More bullet points

    • Deeper bullet points

  1. List entry

  2. Another list entry

  3. More list entries

  4. Markdown cheatsheet

Exercise#

Add a new python code cell below and use the print command to print your name.