Solutions

Your turn

In [9]:
"Hello world!"
Out[9]:
'Hello world!'
In [10]:
print(42)
42
In [11]:
7+1
Out[11]:
8
In [12]:
7.0+1
Out[12]:
8.0
In [13]:
5/3
Out[13]:
1.6666666666666667
In [14]:
5.0/3.0
Out[14]:
1.6666666666666667
In [15]:
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-15-9e1622b385b6> in <module>
----> 1 1/0
ZeroDivisionError: division by zero
In [16]:
0/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-16-9ab73e148374> in <module>
----> 1 0/0
ZeroDivisionError: division by zero
In [17]:
0.0/0.0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-17-362e28523176> in <module>
----> 1 0.0/0.0
ZeroDivisionError: float division by zero
In [18]:
1 + 0.000001
Out[18]:
1.000001
In [19]:
1 + 0.0000000000000001
Out[19]:
1.0

BODMAS

Like we were taught in primary school, computers evaluate calculation according to the order dictated by 'Brackets (Of) Division Multiplication Addition Subtraction':

In [20]:
3 + 4 / 2 + 1
Out[20]:
6.0
In [21]:
(3 + 4) / 2 + 1
Out[21]:
4.5
In [22]:
3 + 4 / (2 + 1)
Out[22]:
4.333333333333333
In [23]:
(3 + 4) / (2 + 1)
Out[23]:
2.3333333333333335

Use brackets where necessary but try to avoid using too many as this will make your code more readable.