Quiz 1 Practice Exercises

These questions relate to the learning objectives for the Quiz 1 (which is scheduled for Thursday of Week 2).

For each of these topics, we state the learning objective and then give one or more practice exercises.

Documentation

  • Extract basic information from Python documentation (such as the result of using help).

Exercise:

Use NumPy’s arange (look it up using help) function to create the NumPy array

array([3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. , 7.5])

What goes wrong if you try to make the list version using Python’s built-in range function?

Errors

  • Extract basic information from a Python error message.

Exercise:

There is a function called zeros built into NumPy. The documentation for this function shows the following:

Help on built-in function zeros in module numpy:

zeros(...)
    zeros(shape, dtype=float, order='C')
    
    Return a new array of given shape and type, filled with zeros.
    
    Parameters
    ----------
    shape : int or tuple of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    order : {'C', 'F'}, optional, default: 'C'
        Whether to store multi-dimensional data in row-major
        (C-style) or column-major (Fortran-style) order in
        memory.

If we evaluate np.zeros(3,5), we get the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-25c3f14ba1b8> in <module>
----> 1 np.zeros(3,5)

TypeError: Cannot interpret '5' as a data type
  • The user was trying to make a 3-by-5 array of zeros. Do you see what the user did wrong?

  • How should the code be corrected?

Data types

  • Choose from among list, tuple, range, set, NumPy array, depending on which is an appropriate data type for a particular task.

Exercise:

For each pair of data types, give an advantage/disadvantage between them.

Convert

  • Convert between different data-types using syntax like np.array(my_tuple).

Exercise:

What is the difference between the following? Give a specific example, and explain how the results are different. str(list(x)) vs list(str(x)). Is it possible for these two to produce the same output?

Replace for loops

  • Replace code written with a for-loop with code using list comprehension.

Exercise:

Rewrite the following using list comprehension. (What is an example of a value of my_list for which this code makes sense?)

new_list = []
for x in my_list:
    if len(x) > 2:
        new_list.append(x)

Do the same thing for the following.

new_list = []
for x in my_list:
    if len(x) > 2:
        new_list.append(x)
    else:
        new_list.append(0)

Do the same for the following. Use my_list[:5] to refer to the first 5 elements in my_list.

new_list = []
for i in range(5):
    new_list.append(my_list[i]**2)

Random

  • Generate random numbers (real numbers or integers) in NumPy using default_rng().

Relevant section in the course notes

Exercise:

Using NumPy’s default_rng() and the method random, make a length 10 NumPy array of random real numbers (not integers) uniformly distributed between 1 and 4. (To get full credit, you must use these items, and you must make the array in an efficient manner, not for example using a for loop or even a list comprehension.)

list comprehension sublist

  • Find elements in a list or NumPy array which satisfy a given condition using list comprehension.

Exercise:

For your random array made above, using list comprehension, get the sublist of elements which round to 3. (There is probably a better way to do this using NumPy methods, but the point of this is to practice with list comprehension. We haven’t discussed the round function which is a built-in function Python. You can look up its documentation or just try it out!)

Defining a function

  • Write code defining a function

Exercise:

Write code which takes as input a list my_list and as output returns:

  • the word yes if the length of my_list is strictly bigger than 3;

  • the word no if the length of my_list is less than or equal to 3;

  • the phrase not a list if my_list is not a list. Hints. Make sure that you are returning values, not printing values. Use isinstance to check if my_list is a list, like from discussion section on Thursday.

Counting in a NumPy array

  • Count elements in a NumPy array satisfying a given condition using a Boolean array and np.count_nonzero.

Relevant section in the course notes

Exercise:

For the random NumPy array you made above, count how many of its elements are strictly bigger than 3. Use the following strategy: first convert into a Boolean array which is True where the elements are strictly bigger than 3, and then use np.count_nonzero.

Random simulation

  • Estimate a probability using a random simulation and the formula successes/experiments.

Exercise:

Using a random simulation and NumPy, estimate the following: if you choose a random real number between 1 and 4, what is the probability that the number is strictly bigger than 3? (Of course we could instead find this probability exactly. Use enough “experiments” that your probability estimate is accurate to at least 3 digits.)

Created in deepnote.com Created in Deepnote