The easiest way to get started using Python, and one of the best for research data work, is the Jupyter Notebook.
In the notebook, you can easily mix code with discussion and commentary, and mix code with the results of that code; including graphs and other data visualisations.
### Make plot %matplotlib inline import math import numpy as np import matplotlib.pyplot as plt theta = np.arange(0, 4 * math.pi, 0.1) eight = plt.figure() axes = eight.add_axes([0, 0, 1, 1]) axes.plot(0.5 * np.sin(theta), np.cos(theta / 2))
« [<matplotlib.lines.Line2D at 0x2b8f972f05f8>]
We’re going to be mainly working in the Jupyter notebook in this course.
Jupyter notebooks consist of discussion cells, referred to as “markdown cells”, and “code cells”, which contain Python. The notebooks that accompany the readings were created using the Jupyter notebook, The main text of the notebook is entered into Markdown Cells.
print("This cell is a code cell")
« This cell is a code cell
Code cell inputs are numbered, and show the output below.
Markdown cells contain text which uses a simple format to achieve pretty layout, for example, to obtain:
**bold**, *italic* * Bullet > Quote
See the Markdown documentation at http://daringfireball.net/projects/markdown/
When working with the notebook, you can either be in a cell, typing its contents, or outside cells, moving around the notebook.
bto add a new cell below the cursor.
mto turn a cell from code mode to markdown mode.
enterto execute the code in the block.
hto see a list of useful keys in the notebook.
tabto suggest completions of variables. (Try it!)
These operations can also be accessed from the menu.
Supplementary material: Learn more about the notebook in the Reading More about Jupyter Notebooks
Data science experts tend to use a “command line environment” to work. You’ll be able to learn this at our “Software Carpentry” workshops, which cover other skills for computationally based research.
%%bash # Above line tells Python to execute this cell as *shell code* # not Python, as if we were in a command line # This is called a 'cell magic' python -c "print(2*4)"
Once you get good at programming, you’ll want to be able to write your own full programs in Python, which work just like any other program on your computer. Here are some examples:
%%bash echo "print(2 * 4)" > eight.py python eight.py
%%writefile fouteen.py #! /usr/bin/env python print(2*7)
« Writing fourteen.py
%%bash chmod u+x fourteen.py
We can write our own python libraries, called modules, which we can import into the notebook and invoke:
%%writefile draw_eight.py # Above line tells the notebook to treat the rest of this # cell as content for a file on disk. import math import numpy as np import matplotlib.pyplot as plt def make_figure(): theta = np.arange(0, 4 * math.pi, 0.1) eight = plt.figure() axes = eight.add_axes([0, 0, 1, 1]) axes.plot(0.5 * np.sin(theta), np.cos(theta / 2)) return eight
« Writing draw_eight.py
import draw_eight # Load the library file we just wrote to disk
There is a huge variety of available packages to do pretty much anything. For instance, try