In [24]:
# This is a python execution cell.
# Anything you could do in a python shell or script, you can do here.
# To execute a cell, type CTRL-Enter.
# You can also type SHIFT-Enter to execute and move to the next cell,
# and you can type OPTION-Enter to execute and insert a new cell below.
def foo():
print "IPython Notebook is Awesome!"
foo()
# The last expression in a cell is always displayed as the cell's output when
# it's executed.
[1,2,3,4]
Out[24]:
Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).
Markdown supports bulleted lists.
Reasons to like Markdown:
code()
formatting.You can embed arbitrary HTML
Such as, | for example, |
a table. |
It even has support for code blocks:
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
fail :: String -> m a
m >> k = m >>= \_ -> k
Most of the magics that work in the IPython
shell also work in the notebook. Additionally, there are some magics that only work in the notebook.
TAB
key part of the way through a variable name autocompletes that name.pandas.DataF<TAB> -> pandas.DataFrame
).
In [25]:
import pandas
class Point(object):
"""
A class-level docstring.
"""
def __init__(self, x, y=3):
"""
Constructor docstring. SHIFT+TAB will show you this first line.
SHIFT + two TABs will show you the entire docstring.
"""
self.x, self.y = x, y
long_variable_name = Point(3,4)
In [26]:
pan # Pressing TAB here will autocomplete pandas.
pandas. # Pressing TAB here will show you the top-level attributes of pandas.
pandas.D # Pressing TAB here will show you DataFrame, DatetimeIndex, and DateOffset.
pandas.DataFrame # Pressing TAB here will show you methods and attributes of DataFrame.
long_variable_name # Pressing TAB here will autocomplete long_variable_name
# Hold SHIFT and press TAB with your cursor in
x = Point(1,2) # the parentheses to see info on how to make a Point object.
<expression>?
shows the function signature and documentation for that expression.<expression>??
takes you to the source code for the expression.pinfo
and pinfo2
magics to get the same info.SHIFT+TAB
while hovering over an object to open in-line documentation for that callable.
In [27]:
pandas.DataFrame?
In [29]:
pandas.DataFrame.plot??
In [ ]:
pandas.DataFrame.|plot
In [ ]:
%%javascript
alert('foo')
In [31]:
%load_ext rpy2.ipython
In [32]:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,5), columns=['A','B','C','D','E'])
df
Out[32]:
In [33]:
# Push our DataFrame into R.
%Rpush df
In [34]:
%%R
# Despite also being valid python, this is actually R code!
col_A = df['A']
plot(col_A)
In [35]:
# We can also pull values back out of R!
%Rpull col_A
col_A
Out[35]:
In [36]:
import IPython.display
dir(IPython.display)[:18]
Out[36]:
In [37]:
from IPython.display import Math
Math(r'w_A = \frac{\sigma_B - Cov(r_A, r_B)}{\sigma_B^2 + \sigma_A^2 - 2 Cov(r_A, r_B)}')
Out[37]:
In [38]:
from IPython.display import HTML
HTML('''\
To learn more about IPython's rich display capabilities, click
<a href="http://ipython.org/ipython-doc/dev/config/integrating.html">here</a>.
''')
Out[38]:
In [39]:
from IPython.display import YouTubeVideo
YouTubeVideo("https://www.youtube.com/watch?v=B_XiSozs-SE")
Out[39]:
In [ ]:
class Table(object):
"""
A simple table represented as a list of lists.
"""
def __init__(self, lists):
self.lists = lists
def make_row(self, l):
columns = ''.join('<td>{value}</td>'.format(value=value) for value in l)
return '<tr>{columns}</tr>'.format(columns=columns)
def _repr_html_(self):
rows = ''.join(self.make_row(l) for l in self.lists)
return "<table>{rows}</table>".format(rows=rows)
In [ ]:
Table(
[
[1,2,3],
[4,5,6]
]
)