# 01.06 Jupyter Exercises 2¶

Exercises rating:

★☆☆ - You should be able to do it based on Python knowledge plus the text.

★★☆ - You will need to do extra thinking and some extra reading/searching.

★★★ - The answer is difficult to find by a simple search, requires you to do a considerable amount of extra work by yourself (feel free to ignore these exercises if you're short on time).

Note: Some of the exercises in this section are way beyond what we cover. Instead, the exercises are a review of your general python programming knowledge. You can safely ignore these exercises, they are not necessary for what we learn later.

#### 1. Write a Python class that is instantiated with a list of numbers and has a .mean() method. (★☆☆)¶

In [ ]:



#### 2. Save the following into a file called by_zero.py. (★★☆)¶

def div_xy(x, y):
return x / y

def div_by_zero(x):
return div_xy(x, 0.0)



import the module and execute the div_xy function to divide 10 by 2.

P.S. You do not need to worry about an __init__.py file if the module is in the same directory.

In [ ]:


In [ ]:



#### 3. What is the difference between the magic in the following cells? (★★☆)¶

Try to explain the differences between what you see.

In [ ]:
%xmode Plain

by_zero.div_by_zero(3)

In [ ]:
%xmode Context

by_zero.div_by_zero(3)

In [ ]:
%xmode Verbose

by_zero.div_by_zero(3)


#### 4. Use %pdb on to print out the value of y (inside div_xy) just before the division by zero. (★★★)¶

In [ ]:
import by_zero
by_zero.div_by_zero(3)


#### 5. Use %timeit to time a function that sums the elements of a list, compare it with np.sum (★★☆)¶

You can use np.arange(1024) to create a list of the first 1024 integers. Here is a start (this exercise ought to take a while to run):

In [ ]:
def sums_all(l):
pass

In [ ]:
import numpy as np
long_list = np.arange(1024)
%timeit np.sum(long_list)

#### 6. Use %prun to profile the function you wrote in exercise 5. (★★★)¶
import numpy as np