Dražen's humble introduction to
What's IPython notebook? A web interface for a scientific Python shell.
Main features:
What's so good about Python?
Python is a dynamic, interpreted language...
1+1
2
... covering all the usual stuff, such as loops and branches...
text = 'We are the knights who say "ni"'
for word in text.split(' '):
if len(word)>3:
text = text.replace(word, word.upper())
print(text)
We are the KNIGHTS who say "NI"
... letting you choose your own paradigm, be it functional...
text = 'We are the knights who say "ni"'
make_big = lambda x : x.upper() if len(x)>3 else x
make_big("banana")
big_words = map(make_big, text.split(' '))
print(' '.join(big_words))
We are the KNIGHTS who say "NI"
... or object-oriented...
class Text():
def __init__(self, content):
self.content = content
def make_big(self):
for word in self.content.split(' '):
if len(word)>3:
self.content = self.content.replace(word, word.upper())
def __str__(self):
return self.content
text = Text('We are the knights who say "ni"')
text.make_big()
print(text)
We are the KNIGHTS who say "NI"
... or one-liners for the lazy...
print(' '.join([word.upper() if len(word)>3 else word for word in 'We are the knights who say "ni"'.split(' ')]))
We are the KNIGHTS who say "NI"
But most of all it's pythonic.
from googlemaps import GoogleMaps
gmaps = GoogleMaps()
directions = gmaps.directions('Argentinienstrasse 8', 'Ottakringer Brauerei')
#print(directions['Distance'])
for step in directions['Directions']['Routes'][0]['Steps']:
print(step['descriptionHtml'])
Head <b>east</b> on <b>Argonnenstraße</b> toward <b>Dolomitenstraße</b> Take the 1st right onto <b>Dolomitenstraße</b> Turn left onto <b>Aussiger Straße</b> Take the 2nd right onto <b>Sandgasse</b> Continue onto <b>Lechstraße</b> Continue onto <b>Vilsstraße</b> Turn left onto <b>Walhalla-Allee</b> Continue onto <b>Osttangente</b> Take the <b>B8</b> ramp to <b>Passau/<wbr/>Deggendorf/<wbr/>Straubing/<wbr/>Möbel-/<wbr/>Bauzentrum</b> Turn left onto <b>Straubinger Straße/<wbr/>B8</b><div class="">Continue to follow B8</div> Turn right onto <b>St2145</b> Merge onto <b>A3</b> via the ramp to <b>Passau</b><div class="google_impnote">Partial toll road</div><div class="google_impnote">Entering Austria</div> Continue onto <b>A8</b><div class="google_impnote">Partial toll road</div> Slight left onto <b>A25</b><div class="google_impnote">Toll road</div> At the interchange <b>Knoten Haid</b>, keep left and follow signs for <b>A1/<wbr/>E55/<wbr/>E60</b> toward <b>Wien/<wbr/>Linz/<wbr/>Ungarn/<wbr/>Tschechien</b><div class="">Continue to follow A1</div><div class="google_impnote">Partial toll road</div> Continue onto <b>Wr. Str./<wbr/>B1</b><div class="">Continue to follow B1</div> Turn left onto <b>Bräuhausbrücke/<wbr/>Stampferg.</b> Continue onto <b>Bergmillerg.</b> Turn right onto <b>Linzer Str.</b> Turn left to stay on <b>Linzer Str.</b> Turn right to stay on <b>Linzer Str.</b> Continue onto <b>Müller-Guttenbrunn-Straße</b> Continue onto <b>Kinkpl.</b> Turn left onto <b>Tinterstr.</b> Turn right onto <b>Flötzersteig</b> Continue onto <b>Joachimsthalerpl.</b> Continue onto <b>Wernhardtstr.</b> Continue onto <b>Spetterbrücke</b> Continue onto <b>Gablenzg.</b> Turn left onto <b>Possingerg.</b> Continue onto <b>Schuhmeierpl.</b> Continue onto <b>Wattg.</b> Turn right onto <b>Ottakringer Str.</b><div class="google_note">Destination will be on the right</div>
import Image
im = Image.open("data/lena.jpg")
im.show()
im.rotate(90).show() # if we want to rotate Lena to a horizontal position...
(or for servers...)
import Image
im = Image.open("data/lena.jpg")
#im.show() # <- this is what you would normally do, but to embed it on a server I make it a bit more complicated:
subplot(121) # 1 row, 2 columns, image 1
imshow(numpy.asarray(im)) # we convert an image to a numpy 2d array and plot it as an image
im2 = im.rotate(90) # if we want to rotate Lena to a horizontal position...
#im2.show()
subplot(122) # 1 row, 2 columns, image 2
imshow(numpy.asarray(im2))
show()
And the list of modules is endless!
For more on Python (with a touch of Monty Python inside), I recommend this guy's tutorial:
Basically Python with imported scipy libraries and better integration with your GUI and computational hardware. Some nice features...
Embedded plots
x = linspace(0, 2*pi)
y = sin(x)
plot(x,y)
show()
Auto-completion and documentation
a=range(5)
a.remove(3)
print(a)
y?
[0, 1, 2, 4]
Clean stack traces
def divide():
a = 1
b = 4
c = b / (a-1)
divide()
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-16-a8416233c05c> in <module>() 4 c = b / (a-1) 5 ----> 6 divide() <ipython-input-16-a8416233c05c> in divide() 2 a = 1 3 b = 4 ----> 4 c = b / (a-1) 5 6 divide() ZeroDivisionError: integer division or modulo by zero
Best of all - you can install it on your server to have a consistent environment
sudo apt-get install ipython
The ultimate lab notebook! Inside it you can use cool features such as:
Can you believe it? I used to code in Java!
private static int maxValue(char[] chars) {
int max = chars[0];
for (int ktr = 0; ktr < chars.length; ktr++) {
if (chars[ktr] > max) {
max = chars[ktr];
}
}
return max;
}
$E = mc^2 \ne \sum_{i \in N}e^i + \int_0^\infty e^{-x}dx$
from IPython.lib.display import YouTubeVideo
# I just learned about this very feature today from this video :)
YouTubeVideo('HaS4NXxL5Qc')
Now that we know what's available, let's try to do something realistic...
knowledge.np.matrix
knowledge.pandas.stocks
knowledge.sympy
Installing everything in Ubuntu is easy
sudo apt-get install ipython-notebook
In another OS it might give you some headache - better to install a Python distribution such as Enthought or Python(x,y)
Take a stroll to the cheese shop for the necessary packages
sudo pip install googlemaps PIL
It was a nice experience learning Python. A nice afternoon.
-- Donald E. Knuth