44. Jupyter notebooks and Python#
The exercises and demos in this book correspond to Jupyter notebooks that you can run yourself (rather than just looking at the static version shown in the book). You are highly encouraged to download the source files, as described in Using git for cloning the book repository, such that you can perform the exercises interactively.
A Jupyter notebook is a powerful tool for interactively developing and presenting data and computational science projects. Jupyter notebooks integrate markdown text notes, mathematical equations, interactive code cells and their output into a single document. They can be displayed on a web browser running on a computer, on a tablet (e.g., IPad), or even on your smartphone.
This integrated interface promotes fast, iterative development since each output of your code will be displayed right away. That’s why notebooks have become very popular in data science and for explorative computational projects.
For serious code development projects, however, the Jupyter notebook format does have some serious drawbacks, such as lack of version control, and should be avoided.
44.1. What you should know about Python and using Jupyter notebooks#
Know how to switch between
CodeandMarkdowncells and how to run them (shift-ReturnorRunbutton)Basics of Python evaluation and printing
string concatenation
exponentiation with
**use of fstrings
Importing numpy and basic functions (sqrt, exp, sin, \(\ldots\))
numpy arrays using
arange(min, max, step)
Getting help via Google (or alternative): StackOverflow and manuals
Defining functions in Python
def my_function(x):\(\longleftarrow\) semicolon and then indented linesposition vs. keyword argument and defaults
shift + tab + tabto reveal definitions
Plotting with Matplotlib
Standard sequence for plot: data \(\longrightarrow\) make figure \(\longrightarrow\) add subplots \(\longrightarrow\) make plot
dressing up and saving a plot
names for the figure and axis objects are our choice
Numpy linear algebra
defining a matrix and finding its shape or particular elements
matrix (or vector) operations: multiplying them with @, transpose, trace, inverse, eigensolution