Skip to content

Intro

Introduction to Python and Using Jupyter notebooks

The Jupyter notebook is a platform for using and writing code in a dynamic way that allows users to combine cells of code snippets that are executed with a persistent namespace and kernel alongside markdown text for facilitating readability and visualization.

In this notebook, we cover some basics of Jupyter functionality along with a discussion of some details of how you can use python in this environment and elsewhere.

Installation

For this workshop, we've constructed an online environment for everyone to use in order to smooth out any platform-dependent installation issues, but you'll probably want to install the tools we use today locally on your own machine. To do this, we recommend Anaconda, which is an effective tool for python package management that can create virtual environments, comes with a pre-installed IDE, and includes all of the Jupyter functionality that you'll see here. The Anaconda installer should be detailed on the page linked above, but here's another resource for installation that might be helpful.

Code vs. Markdown

Jupyter notebooks are broken down into "cells" which might contain either code or markdown. If you select a cell with your mouse, it should be highlighted with a green border indicating that you are in "edit," mode and can edit the contents of the cell. If text reading In [ ]: is on the left hand side of the cell, it's a "code" cell. For example, type the following and press "Shift + Enter"

print("Hello world!")
Hello world!

Pressing "Shift + Enter" executes the code in the cell, prints the output below the cell, and creates a new cell below that one. In addition to code cells, you can also write your own markdown cells by converting a cell using either the dropdown menu in the toolbar or pressing "Esc + m". In general, pressing escape enters "command mode" for which you can issue a number of commands, including

  • f - find and replace
  • m - convert to markdown
  • y - convert to code
  • h - open the help menu

Right now, trying typing "Esc + h" to open the help/shortcut menu and peruse it. Spend a minute testing out some of the shortcuts.

Note that markdown cells are quite flexible and can basically do anything wikipedia does, including adding \(\LaTeX\)-formatted equations.

\(\hat{H}\psi = E \psi\)

Shell commands, magic, and where to learn more

Jupyter notebooks can also issue commands to the shell, which can be achieved using the ! symbol at the beginning of the cell:

!ls .
0 - Introduction and Jupyter Use.ipynb Exercises.ipynb
1 - Python Primer.ipynb                inflammation-01.csv
Exercise_Solutions.ipynb

!date
Mon Jul 27 14:57:00 PDT 2020

In addition, certain things can be achieved in Jupyter notebooks using what are called "magic" commands, which are demarcated using the % sign. The most common of these are the magic function to enable inline plotting:

%matplotlib inline

and to invoke the debugger in a particular cell on an error:

%pdb
Automatic pdb calling has been turned ON

These functions set up special functionality in the notebook.

Lastly, note that Jupyter notebooks are becoming increasingly popular as tools to supplement publication. As a computational researcher, you can provide explicit documentation of your methods with embedded code that actually works for a person who wants to understand better what you're working on. In my own work, I've begun making all my plots and collecting all of my data in Jupyter notebooks to provide as supporting info for each of my recent papers. It's a bit more work, but you'll find that having this level of organization and being this transparent about your methods goes a long way.

There are great resources for IPython notebooks online, and here are a few of them: