1+2+3
6
The first thing you will have to do is download Jupyter, which will get you Python too (if you don't already have it). The easiest way to do this (regardless of operating system) is through the Anaconda distribution, which is a Python version specially geared for data science! Go and download a version if you haven't already!
Project Jupyter is an open source project was born out of the IPython Project in 2014 as it evolved to support interactive data science and scientific computing across all programming languages. Jupyter will always be 100% open source software, free for all to use and released under the liberal terms of the modified BSD license
(The above was taken from the Project Jupyter about page.)
Jupyter runs a persistent python environment in the background. This means you can do all the usual Python stuff. Note: Jupyter also works with (has kernels for) the Julia and Ruby programming languages (among others). However, we'll only use Python. Regardless, commands are entered in code cells:
1+2+3
6
import numpy as np
import re
import json
import datetime
## make some variables
mystring = "this is some text"
mylist = ["these", "are", "some", "words"]
mydict = {
"jake makes": "examples",
"which are": "boring"
}
## print and traverse a string
print(mystring)
print("")
for character in mystring:
print(character)
print("")
## print and traverse a list
print(mylist)
print("")
for myelement in mylist:
print(myelement)
print("")
## print and traverse a dcit
print(mydict)
print("")
for key in mydict:
print(key,mydict[key])
this is some text
t
h
i
s
i
s
s
o
m
e
t
e
x
t
['these', 'are', 'some', 'words']
these
are
some
words
{'jake makes': 'examples', 'which are': 'boring'}
jake makes examples
which are boring
mydict
{'jake makes': 'examples', 'which are': 'boring'}
mydict['jake makes']
'examples'
While comments in code help users and programmers to understand what's going on, Jupyter goes one step further by allowing you to break code down into separate, bite-sized pieces. As I am doing here, you can also intersperse markdown cells, which can be formatted to be much more readable. Also, you can do crazy stuff with LATEX and equations: $$\mu = \frac{\sum_{n=1}^N{x(x)}}{N}$$ and embed hyperlinks, for example, to the Jupyter/IPython docs. You can also make lists easily:
$\mu = \frac{\sum_{n=1}^n (x(n)}{N}$
Even though I wouldn't recommend writing your final, production-level code in Jupyter, it can definitely be helpful for a project when you are in the exploration and early-development stages. For all of its benefits, it's not hard to confuse yourself while developing in Jupyter, since the code execution is asynchronous. For example, you may create a variable, execute the cell, delete the cell, and continue to use the variable you've entered into memory. If you then shut down your notebook and try to run it all again a variable will be missing!
Jupyter's magic features are usually related to system control and navigation. These are generally run with a preceeded "%" character. For example, the "%whos" magic command neatly tells you everything that's in your namespace (workspace):
%whos
Variable Type Data/Info
-------------------------------
character str t
datetime module <module 'datetime' from '<...>b/python3.6/datetime.py'>
json module <module 'json' from '/Use<...>hon3.6/json/__init__.py'>
key str which are
mydict dict n=2
myelement str words
mylist list n=4
mystring str this is some text
np module <module 'numpy' from '/Us<...>kages/numpy/__init__.py'>
re module <module 're' from '/Users<...>nda/lib/python3.6/re.py'>
There's also "%lsmagic", which lists all magic commands:
%lsmagic
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
The double-percentage magic commands apply to a whole cell, for example the "%%writefile", which if entered as "%%writefile PATH/NAME.EXTENSION" at the top of a cell will write the cell's content to a file in the specified location. Also really cool are the language language-based magic commands that let you switch between programming languages. Note that these will not create persistent environments (kernels) for those languages. For example, we can use "%%html" to render a nice example website in Jupyter markdown:
%%html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1504.82">
<style type="text/css">
body {background-color: #f0f0f2}
p.p2 {margin: 0.0px 0.0px 12.0px 0.0px; line-height: 14.0px; font: 12.0px 'Helvetica Neue'; color: #000000; -webkit-text-stroke: #000000}
p.p3 {margin: 0.0px 0.0px 12.0px 0.0px; line-height: 14.0px; font: 12.0px 'Helvetica Neue'; color: #38488f; -webkit-text-stroke: #38488f}
span.s1 {font-kerning: none; background-color: #ffffff}
span.s2 {font: 12.0px 'Helvetica Neue'; font-kerning: none; color: #38488f; -webkit-text-stroke: 0px #38488f}
</style>
</head>
<body>
<h1 style="margin: 0.0px 0.0px 16.1px 0.0px; line-height: 29.0px; font: 24.0px 'Helvetica Neue'; color: #000000; -webkit-text-stroke: #000000"><span class="s1"><b>Example Domain</b></span></h1>
<p class="p2"><span class="s1">This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.</span></p>
<p class="p3"><span class="s1"><a href="http://www.iana.org/domains/example">More information...<span class="s2"></span></a></span></p>
</body>
</html>
This is ok, because you can use the Jupyter/IPython notebook viewer facility:
All you have to do is get your notebook into a public location on the internet and then place its url in the specified location (in the website above). This will generate a link that people can visit, where the notebook can be viewed as html through ANY browser, regardless of whether or not they have Jupyter. In fact, supposing you have not downloaded Jupyter yet this is how you are viewing this introduction—here's the link for this very notebook!
Well, next week's will play with different kinds of data, but for now feel free to check out the offical gallery of interesting Jupyter notebooks:
For example, there is the xkcd-style plotifier:
Also, keep the Jupyter/IPython docs handy for reference: