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", end=' ')
print("World")
Hello World
In [5]:
orig_print = print

def new_print(*args, **kwargs):
    orig_print("[*]", *args, **kwargs)
    
print = new_print
print("Hello World")
# restore old print function
print = orig_print
[*] Hello World

Division

In [6]:
2 / 3
Out[6]:
0.6666666666666666
In [7]:
2 / 3.0
Out[7]:
0.6666666666666666
In [8]:
2.0 / 3.0
Out[8]:
0.6666666666666666
In [9]:
2.0 / 3
Out[9]:
0.6666666666666666
In [10]:
2 // 3
Out[10]:
0
In [11]:
2.0 // 3
Out[11]:
0.0
In [12]:
2 // 3.0
Out[12]:
0.0
In [13]:
2.0 // 3.0
Out[13]:
0.0

Text

In [14]:
"Hello World"
Out[14]:
'Hello World'
In [15]:
b"Hello World"
Out[15]:
b'Hello World'
In [16]:
"Hello World".decode("ascii")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-e6e5a1470dc1> in <module>()
----> 1 "Hello World".decode("ascii")

AttributeError: 'str' object has no attribute 'decode'
In [17]:
b"Hello World".decode("ascii")
Out[17]:
'Hello World'
In [18]:
"Hello World".encode("ascii")
Out[18]:
b'Hello World'
In [19]:
b"Hello World".encode("ascii")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-19-332ae984618e> in <module>()
----> 1 b"Hello World".encode("ascii")

AttributeError: 'bytes' object has no attribute 'encode'
In [20]:
print("Hello\nWorld")
"Hello\nWorld"
Hello
World
Out[20]:
'Hello\nWorld'
In [21]:
print(r"Hello\nWorld")
r"Hello\nWorld"
Hello\nWorld
Out[21]:
'Hello\\nWorld'
In [22]:
print(repr("Hello \U0001F40D"))
print("Hello \U0001F40D")
print(repr(b"Hello \U0001F40D"))
print(b"Hello \U0001F40D")
print(repr("Hello \xF0\x9F\x90\x8D"))
print("Hello \xF0\x9F\x90\x8D")
print(repr(b"Hello \xF0\x9F\x90\x8D".decode('utf8')))
print(b"Hello \xF0\x9F\x90\x8D".decode('utf8'))
print(repr("Hello \U0001F40D".encode('utf8')))
print("Hello \U0001F40D".encode('utf8'))
'Hello 🐍'
Hello 🐍
b'Hello \\U0001F40D'
b'Hello \\U0001F40D'
'Hello ð\x9f\x90\x8d'
Hello 🐍
'Hello 🐍'
Hello 🐍
b'Hello \xf0\x9f\x90\x8d'
b'Hello \xf0\x9f\x90\x8d'
In [23]:
for c in "Hello World":
    print(c)
H
e
l
l
o
 
W
o
r
l
d
In [24]:
for c in b"Hello World":
    print(c)
72
101
108
108
111
32
87
111
114
108
100

Generators/Iterators

In [25]:
range(2)
Out[25]:
range(0, 2)
In [26]:
list(range(2))
Out[26]:
[0, 1]
In [27]:
map(str, [1, 2, 3])
Out[27]:
<map at 0x7fe3f8288dd8>
In [28]:
list(map(str, [1, 2, 3]))
Out[28]:
['1', '2', '3']
In [29]:
d = {'a': 1, 'b': 2, 'c': 3}
d.items()
Out[29]:
dict_items([('a', 1), ('c', 3), ('b', 2)])
In [30]:
list(d.items())
Out[30]:
[('a', 1), ('c', 3), ('b', 2)]
In [31]:
def high_memory(runs):
    from time import time
    print("Computing %d iterations" % runs)
    start = time()
    for i in list(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 14.911809 sec
In [32]:
def low_memory(runs):
    from time import time
    print("Computing %d iterations" % runs)
    start = time()
    for i in range(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 8.822207 sec

Exception Handling

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

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

OSError: Foo

During handling of the above exception, another exception occurred:

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

RuntimeError: Bar
In [35]:
try:
    raise IOError('Foo')
except IOError as e:
    raise RuntimeError('Bar') from e
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-35-dad2ef7730a6> in <module>()
      1 try:
----> 2     raise IOError('Foo')
      3 except IOError as e:

OSError: Foo

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-35-dad2ef7730a6> in <module>()
      2     raise IOError('Foo')
      3 except IOError as e:
----> 4     raise RuntimeError('Bar') from e

RuntimeError: Bar