Print to console

In [1]:
print "Hello World"
Hello World
In [2]:
print "Hello", "World"
Hello World
In [3]:
print "Hello"
print "World"
Hello
World
In [4]:
print "Hello",
print "World"
Hello World
In [5]:
print("Hello World")
Hello World
In [6]:
print("Hello", "World")
('Hello', 'World')

Division

In [7]:
2 / 3
Out[7]:
0
In [8]:
2 / 3.0
Out[8]:
0.6666666666666666
In [9]:
2.0 / 3.0
Out[9]:
0.6666666666666666
In [10]:
2.0 / 3
Out[10]:
0.6666666666666666

Text

In [11]:
"Hello World"
Out[11]:
'Hello World'
In [12]:
u"Hello World"
Out[12]:
u'Hello World'
In [13]:
"Hello World".decode("ascii")
Out[13]:
u'Hello World'
In [14]:
u"Hello World".decode("ascii")
Out[14]:
u'Hello World'
In [15]:
"Hello World".encode("ascii")
Out[15]:
'Hello World'
In [16]:
u"Hello World".encode("ascii")
Out[16]:
'Hello World'
In [17]:
print "Hello\nWorld"
"Hello\nWorld"
Hello
World
Out[17]:
'Hello\nWorld'
In [18]:
print r"Hello\nWorld"
r"Hello\nWorld"
Hello\nWorld
Out[18]:
'Hello\\nWorld'
In [19]:
print repr("Hello \U0001F40D")
print "Hello \U0001F40D"
print repr(u"Hello \U0001F40D")
print u"Hello \U0001F40D"
print repr(u"Hello \U0001F40D".encode('utf8'))
print u"Hello \U0001F40D".encode('utf8')
print repr("Hello \xF0\x9F\x90\x8D")
print "Hello \xF0\x9F\x90\x8D"
print repr("Hello \xF0\x9F\x90\x8D".decode('utf8'))
print "Hello \xF0\x9F\x90\x8D".decode('utf8')
'Hello \\U0001F40D'
Hello \U0001F40D
u'Hello \U0001f40d'
Hello 🐍
'Hello \xf0\x9f\x90\x8d'
Hello 🐍
'Hello \xf0\x9f\x90\x8d'
Hello 🐍
u'Hello \U0001f40d'
Hello 🐍
In [20]:
for c in "Hello World":
    print c
H
e
l
l
o
 
W
o
r
l
d
In [21]:
for c in "Hello World":
    print ord(c)
72
101
108
108
111
32
87
111
114
108
100

Generators/Iterators

In [22]:
range(2)
Out[22]:
[0, 1]
In [23]:
xrange(2)
Out[23]:
xrange(2)
In [24]:
map(str, [1, 2, 3])
Out[24]:
['1', '2', '3']
In [25]:
from itertools import imap
imap(str, [1, 2, 3])
Out[25]:
<itertools.imap at 0x7f1ec4fde510>
In [26]:
d = {'a': 1, 'b': 2, 'c': 3}
d.items()
Out[26]:
[('a', 1), ('c', 3), ('b', 2)]
In [27]:
d.iteritems()
Out[27]:
<dictionary-itemiterator at 0x7f1ec195ea48>
In [28]:
def high_memory(runs):
    from time import time
    print "Computing %d iterations" % runs
    start = time()
    for i in range(runs):
        pass
    print "Long running computation finished 1 after %f sec" % (time() - start)
    
from multiprocessing import Process
p = Process(target=high_memory, args=(int(5e8),))
p.start()
print "Process", p.name, "started"
p.join()
Process Process-1 started
Computing 500000000 iterations
Long running computation finished 1 after 12.696136 sec
In [29]:
def low_memory(runs):
    from time import time
    print "Computing %d iterations" % runs
    start = time()
    for i in xrange(runs):
        pass
    print "Long running computation finished 2 after %f sec" % (time() - start)

from multiprocessing import Process
p = Process(target=low_memory, args=(int(5e8),))
p.start()
print "Process", p.name, "started"
p.join()
Process Process-2 started
Computing 500000000 iterations
Long running computation finished 2 after 5.910757 sec

Exception Handling

In [30]:
try:
    raise IOError('Foo')
except IOError as e:
    raise
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-30-85daad0e7b60> in <module>()
      1 try:
----> 2     raise IOError('Foo')
      3 except IOError as e:
      4     raise

IOError: Foo
In [31]:
try:
    raise IOError('Foo')
except IOError as e:
    raise RuntimeError('Bar')
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-31-ccffd5b484a4> in <module>()
      2     raise IOError('Foo')
      3 except IOError as e:
----> 4     raise RuntimeError('Bar')

RuntimeError: Bar