Ever tried.
Ever failed.
No matter.
Try again.
Fail again.
Fail better.
-Samuel Beckett
In [ ]:
# Most importantly, these cells can contain runnable Python code.
for item in ['This', 'cell', 'was', 'just', 'executed.']:
print(item)
Jupyter is fairly intuitive, but you should be aware there are two modes through which you can interact with the notebook. These modes are the "Command Mode", and the "Edit Mode".
Escape
takes you to Command Mode, which is used for the notebook as a whole.Return
takes you to Edit Mode, which is used for editing individuals cells.Shift + Enter
or Control + Enter
runs a cell.H
key in Command Mode brings up the Help Screen.Control + Shift + P
key brings up command palette.Shift + Enter
.Click ONCE on the "cell to tinker with" above or use your arrow keys to navigate to that cell. A vertical blue bar should appear to the left of the cell because you have now selected the cell. This bar indicates that you are in Command Mode.
Command Mode is used to run cells or interact with the notebook as a whole. While the cell is selected and in Command Mode, press B
. This will insert a cell underneath it (you are not expected to memorize this ... know that pressing H
in command mode brings up a Help Screen). You can get into Command Mode from Edit Mode by pressing Enter
.
Click TWICE on the "cell to tinker with" above, or move or select the cell in Command Mode and press Enter
. A vertical green bar should appear to the left of the cell. This bar indicates that you are in Edit Mode. You can now change the contents of this cell, and when you have completed your edits press Shift + Enter
to input them and switch back to Command Mode. Try changing the "cell to tinker with" cell and changing its contents. Feel free to change it back and forth from markdown to code. If you're feeling dangerous, you can try running a code cell with Shift + Enter
.
In [ ]:
# We can type in one cell, and press Shift + Enter to run it.
a = 'The quick brown fox jumped over the lazy brown dog.'
In [ ]:
# We can then transform the result from that cell and run it.
b = a.upper()
In [ ]:
# And use the result. Note: comments start with # and aren't executed.
b
In [ ]:
# And if we don't like the result, we can go back and do something else
c = a.title()
In [ ]:
# Which we can do with a single cell without compiling or processing.
c
While this is a trivial example, this pipeline approach allows for massive productivity increases when operating on larger data sets.
Side note: the layout of the cells doesn't really matter. It's all about the order in which they are run. You can see the order of the inputs and the outputs in the boxes to the left of the cells. Anything on the last line of a cell will be inspected/printed, which helps you track your progress as you go along.
In [ ]:
from IPython.core.display import HTML
from IPython.core.display import display
# You can do useful things
HTML(
"""<script>
var code_show=true;
function code_toggle() {
$('div.prompt').hide();
if (code_show) {
$('div.input').show();
} else {
$('div.input').hide();
}
code_show = !code_show;
}
$( document ).ready(code_toggle);
</script>
<a href="javascript:code_toggle()">[Toggle Code]</a>
""")
In [ ]:
# You can do useful things ... or less than useful things.
def myspace_ify():
'''Do not run this function.'''
return HTML('<style type="text/css">'
' div#notebook {'
' background-image: url("./static/haters.gif");'
' background-repeat: repeat;'
' }'
'</style>')
myspace_ify()
In [ ]:
def de_myspace_ify():
return HTML('<style type="text/css">'
' div#notebook {'
' background-image: none;'
' background-repeat: none;'
' }'
'</style>')
de_myspace_ify()
In [ ]:
# Plotting is generally a lot simplier than this.
# First we import pyplot under an alias for brevity's sake
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('fivethirtyeight')
%matplotlib inline
import numpy as np
t = np.arange(0.0, 2.0, 0.05)
s = 1 + np.sin(2*np.pi*t)
fig = plt.plot(t, s, '--')
plt.xlabel('Time')
plt.ylabel('WAT')
plt.title('Important Information')
fig[0].axes.set_ylim([-1, 3])
plt.show()