Overview
Teaching: 15 min
Exercises: 0 minQuestions
How can I run Python programs?
Objectives
Launch the Jupyter Notebook, create new notebooks, and exit the Notebook.
Create Markdown cells in a notebook.
Create and run Python cells in a notebook.
.py
extension to let everyone (including the operating system)
know it is a Python program.
.ipynb
to distinguish them from plain-text Python programs.
Once you have installed Python, the Jupyter Notebook requirements, and the Software Carpentry data folder, open a shell and type:
$ cd ~/Desktop/swc-data/jupyter_notebooks
$ jupyter notebook
Screenshot of a Jupyter Notebook on quantum mechanics by Robert Johansson
How It’s Stored
- The notebook file is stored in a format called JSON.
- Just like a webpage, what’s saved looks different from what you see in your browser.
- But this format allows Jupyter to mix software (in several languages) with documentation and graphics, all in one file.
Code vs. Text
We often use the term “code” to mean “the source code of software written in a language such as Python”. A “code cell” in a Notebook is a cell that contains software; a “text cell” is one that contains ordinary prose written for human beings.
Control Vs. Edit
In the Jupyter notebook page are you currently in control or edit mode?
Switch between the modes. Use the shortcuts to generate a new cell Use the shortcuts to delete a cellSolution
Control mode has a grey or blue boarder and Edit mode has a green border Use “esc” and “Enter” to switch between modes You need to be in control mode (Hit “esc” if your cell is green). Type “B” or “A”. You need to be in control mode (Hit “esc” if your cell is green). Type “X”.
In [ ]:
will disappear to show it is no longer a code cell
and you will be able to write in Markdown.Italicize with _underscores_ or *asterisks*
Bold with two __underscores__ or **astrisks**
Italicize with underscores or asterisks
Bold with two underscores or astrisks
* Use asterisks
* to create
* bullet lists.
1. Use numbers
1. to create
1. numbered lists.
1. Use tabs
* to
* indent
1. lists.
# A Level-1 Heading
## A Level-2 Heading (etc.)
Line breaks
don't matter.
But blank lines
create new paragraphs.
Line breaks don’t matter.
But blank lines create new paragraphs.
[Create links](http://software-carpentry.org) with `[...](...)`.
Or use [named links][data_carpentry].
[data_carpentry]: http://datacarpentry.org
Create links with [...](...)
.
Or use named links.
A Little Math
What is displayed when a Python cell in a notebook that contains several calculations is executed? For example, what happens when this cell is executed?
7 * 3 2 + 1
Solution
Python returns the output of the last calculation.
3
Practice Your Markdown
Create a Markdown cell in a notebook that matches the following:
The basic steps of a career in science
- Get funding
- Do work
- Design experiment
- Collect data
- Analyze
- Write up
- Publish in a high impact journal
easy, right?
Solution
This challenge integrates both the numbered list and bullet list. Note that the bullet list is indented 2 spaces so that it is inline with the items of the numbered list.
1. Get funding. 2. Do work. * Design experiment. * Collect data. * Analyze. 3. Write up. 4. Publish in a [high impact journal](http://www.nature.com/)
Change an Existing Cell from Code to Markdown
What happens if you write some Python in a code cell and then you switch it to a Markdown cell? For example, put the following in a code cell:
x = 6 * 7 + 12 print(x)
And then run it with shift+return to be sure that it works as a code cell. Now go back to the cell and use escape+M to switch the cell to Markdown and “run” it with shift+return. What happened and how might this be useful?
Solution
The python code gets treated like markdown text. The lines appear as if they are part of one contiguous paragraph. This could be useful to temporarly turn on and off cells in notebooks that get used for multiple purposes.
x = 6 * 7 + 12 print(x)
Equations
Standard Markdown (such as we’re using for these notes) won’t render equations, but the Notebook will. Create a new Markdown cell and enter the following:
$\sum_{i=1}^{N} 2^{-i} \approx 1$
(It’s probably easier to copy and paste.) What does it display? What do you think the underscore
_
, circumflex^
, and dollar sign$
do?Solution
The notebook shows the equation as it would be rendered from latex equation syntax. The dollar sign
$
is used to tell markdown that the text in between is a latex equation. If you’re not familiar with latex, underscore (_
) is used for subscripts and circumflex^
is used for superscripts. Curly braces{
and}
group text together so that the statementi=1
becomes the the subscript andN
becomes the superscript. Similarly,-i
is in curly braces to make the whole statement the superscript for2
.\sum
and\approx
are latex commands for sum over and the approximate symbols.
Key Points
Python programs are plain text files.
Use the Jupyter Notebook for editing and running Python.
Closing a tab does not shut down the Notebook.
The Notebook has Control and Edit modes.
Use the keyboard and mouse to select and edit cells.
The Notebook will turn Markdown into pretty-printed documentation.
Markdown does most of what HTML does.