Worksheet

You may submit this worksheet in a group of 1-3 total students.

These worksheets are graded for effort, not for correctness.

Due date. Due at 5:00pm on Tuesday of Week 1.

Question 0:

Name(s):

UCI ID(s):

Practice with for-loops and lists of lists

We will introduce for-loops now, but throughout Math 10 we will seek more efficient (or at least more elegant or more “Pythonic”) ways of writing code.

Let’s represent the matrix

\[\begin{split} \begin{pmatrix} 0 & 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 & 9 \\ 10 & 11 & 12 & 13 & 14 \end{pmatrix} \end{split}\]

in Python. The best way to do that is with a two-dimensional NumPy array, but let’s practice with for-loops by first representing it as a list of lists.

We first make a template containing all zeros. Try reading through this next cell step-by-step and understanding what it does.

zero_list = []
for i in range(3):
    my_list = []
    for i in range(5):
        my_list.append(0)
    zero_list.append(my_list)
print(zero_list)
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Let’s make a copy of that zero_list, so that we can use zero_list again later if we want.

list_of_lists = zero_list.copy()
list_of_lists
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Question 1:

Fill in the correct values so we make the above matrix as the list of lists [[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]]. Do not change to a different strategy, even if you know a better one.

for i in range(3):
    for j in range(5):
        list_of_lists??? = ???
print(list_of_lists)

Question 2:

What changes if you indent the print(list_of_lists) line one level? What happens if you indent it two levels?

Answer:

Question 3:

Here’s a more elegant method, but still not as good as using NumPy. Use the following idea, but update it so better_list contains three lists, not three range objects, and so that better_list has the same numbers as what we produced above.

better_list = []
for i in range(3):
    better_list.append(range(0,5))
better_list
[range(0, 5), range(0, 5), range(0, 5)]

Practice with NumPy

As a preview, here is one of the most important libraries we will use in Math 10. Many aspects of it will remind you of Matlab.

import numpy as np

Here are two ways to learn about the method np.zeros. This method is part of NumPy, but the same sort of help is available also for functions in standard Python.

np.zeros?
help(np.zeros)

Question 4:

Make a zero array of the correct 3x5 shape:

A = np.zeros(???)
print(A)
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]

Question 5:

Now go through and fill in the numbers. Use syntax like A[1,3] = 21 to for example put the number 21 in the 1st row (by Python counting) and 3rd column.

for i in range(3):
    for j in range(5):
        A??? = ???
print(A)

You can also do this with a single for-loop, instead of nested for-loops. Before we make the final answer, we show a preliminary step as an example. Notice how we don’t have to convert the range object to a list object in this case, because NumPy does this for us automatically.

A = np.zeros((3,5))
for i in range(3):
    A[i] = range(5)
print(A)
[[0. 1. 2. 3. 4.]
 [0. 1. 2. 3. 4.]
 [0. 1. 2. 3. 4.]]

Question 6:

Change the (5) part (but nothing else) so that we get the same numbers 0,1,…,14 as above.

A = np.zeros((3,5))
for i in range(3):
    A[i] = range(5)
print(A)

I think the above is a good amount for class, but in case you finish early, here is a problem involving function definitions.

Question 7:

Write a function fill_array(A) which takes as input a NumPy array A, and as output returns an array of the same shape as A, but which contains consecutive numbers 0, 1, 2, … throughout, as in the following examples.

def fill_array(A):
    m,n = A.shape
    B = np.zeros((m,n))
    for i in range(???):
        B[???] = range(???)
    return B
fill_array(np.zeros((5,2)))
array([[0., 1.],
       [2., 3.],
       [4., 5.],
       [6., 7.],
       [8., 9.]])
fill_array(np.zeros((4,4),dtype=np.int32))
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]], dtype=int32)