CS 115 Lab 9: Setup and Practice

[Back to lab instructions]

Refer to the Week 10 reading or other resources as necessary to complete this assignment.


Setup

  1. If the machine was on when you started using it, reboot it to MacOS.
  2. Follow these procedures to mount your blue home-directory on the local machine and create a PyCharm project (call it Lab09). You will use PyCharm in the next part, but, it is important that you set up your project now so that if you run into any issue, we can resolve them quickly.
  3. Right-click or control-click to download these 4 files into your default Python directory (C:\Python34 on Windows or Documents on a Mac):

Practice Part 1: Creating and accessing 2D lists

At this point, you should be very comfortable using and manipulating one-dimensional lists. Lists usually are created and accessed through loops. For example, this code creates a list of 10 elements, where each element has a value of 0:

my_list = []    # create an empty list
for i in range(10):
    my_list.append(0)  

We can also create and initialize a list in a single statement:

list_of_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The elements of a list can be of any data type, including lists. For example:

two_three = [ [1, 2, 3], [10, 11, 13] ] 

Here, we have a list with two elements. Each of the elements is itself a list, which makes two_three a two-dimensional list.

Each of the elements of two_three is a row, and each row has three elements (columns).

Practice Part 2: building 2D lists with append

Copy and paste the following code into the online Python 3 tutor, run it one step at a time, and answer Question 4 in your writeup.

v = 0
two_d = []            # create an empty list
for i in range(5):
    two_d.append([])  # append an empty list to two_d
    for j in range(4):
        two_d[i].append(v)   # two_d[i] is the empty list that we just created.
                             # here, we are adding elements to it.
        v += 1