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.
.mean()
method. (★☆☆)¶
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.
Try to explain the differences between what you see.
%xmode Plain
by_zero.div_by_zero(3)
%xmode Context
by_zero.div_by_zero(3)
%xmode Verbose
by_zero.div_by_zero(3)
%pdb on
to print out the value of y
(inside div_xy
) just before the division by zero. (★★★)¶import by_zero
by_zero.div_by_zero(3)
%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):
def sums_all(l):
pass
import numpy as np
long_list = np.arange(1024)
%timeit np.sum(long_list)
%timeit # your function
%prun
to profile the function you wrote in exercise 5. (★★★)¶import numpy as np
long_list = np.arange(32256)