A IPython notebook

IPython notebook is great for:

  • Sharing code!

  • Combining code with text, plots, mathematical expressions, videos, images, and more!

  • Show-off a bit!

  • Interactive teaching (live code)

And.. It has great features such as:

  • tab-completion

  • syntax highlighting

  • %-prefixed commands

  • Many more...

Check it out

Check out Ipython notebook here: http://ipython.org/notebook.html

Or see that web page embedded right here in the notebook:


In [ ]:
<iframe src=http://ipython.org/notebook.html width=1000 height=400></iframe>

Markdown

Python code

This code finds the factorial of a series of number in one line:


In [8]:
#this is a terrible and awesome way to do this:
factorial = (lambda k: reduce(int.__mul__, range(1,k+1),1))

In [25]:
map(factorial, range(1,9))


Out[25]:
[1, 2, 6, 24, 120, 720, 5040, 40320]

Magic functions

This shows the use of builtin timeit magic using the following %-prefix command:


In [2]:
import random
%timeit a = [random.random() for x in range(1000)]


10000 loops, best of 3: 178 us per loop

The builtin magics include:

  • Functions that work with code: %run, %edit, etc.

  • Functions to run code in other languages: %%bash %%perl %%ruby %%javascript, etc.

Display images

You can display images stored locally like this:


In [1]:
from IPython.display import display, Image
display(Image(filename='misc-you-dont-say-l.png'))


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-1-a6ad369a43b2> in <module>()
      1 from IPython.display import display, Image
----> 2 display(Image(filename='misc-you-dont-say-l.png'))

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/core/display.py in __init__(self, data, url, filename, format, embed, width, height, retina)
    729         self.height = height
    730         self.retina = retina
--> 731         super(Image, self).__init__(data=data, url=url, filename=filename)
    732 
    733         if retina:

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/core/display.py in __init__(self, data, url, filename)
    384         self.filename = None if filename is None else unicode_type(filename)
    385 
--> 386         self.reload()
    387         self._check_data()
    388 

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/core/display.py in reload(self)
    751         """Reload the raw data from file or URL."""
    752         if self.embed:
--> 753             super(Image,self).reload()
    754             if self.retina:
    755                 self._retina_shape()

/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/IPython/core/display.py in reload(self)
    402         """Reload the raw data from file or URL."""
    403         if self.filename is not None:
--> 404             with open(self.filename, self._read_flags) as f:
    405                 self.data = f.read()
    406         elif self.url is not None:

FileNotFoundError: [Errno 2] No such file or directory: 'misc-you-dont-say-l.png'

Or images stored on the web like this:


In [ ]:
<img src="http://cdn.alltheragefaces.com/img/faces/large/misc-you-dont-say-l.png" width=300 height=300/>