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 01a_hello.py
in this folder. Execute it in the terminal via python 01a_hello.py
.
Python execution via Jupyter Notebooks#
Furthermore, for academic purposes, Jupyter notebooks are often used to run Python code, to document what the code does 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
orSHIFT-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 hittingB
.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
List entry
Another list entry
More list entries
Exercise#
Add a new python code cell below and use the print
command to print your name.