Introduction to Computer Programming

Lab 9: lists

Instructions

  1. Try these in the IDLE shell, in order, one at a time:

     >>> a = []
     >>> type(a)
     >>> len(a)
     >>> a.append('Hello')
     >>> a.append(29)
     >>> a.append(False)
     >>> a
     >>> len(a)
     >>> a.append('whatever')
     >>> a
     >>> a.pop()
     >>> a
     >>> for x in a:
             print(x)
    
     >>> seasons = ['Winter', 'Spring', 'Summer', 'Fall']   # a list literal
     >>> seasons[3] = 'Autumn'   # lists are mutable
     >>> seasons
     >>> ','.join(seasons)
    
     >>> 'the quick brown fox'.split()
    
     >>> ns = [2, 11, 3, 7, 5]
     >>> ns[4]
     >>> max(ns)
     >>> sum(ns)
    
     >>> a = [99, 88]
     >>> b = a
     >>> b[0] += 1
     >>> b
     >>> a
  2. Download and unzip the starter archive. Use IDLE to open lab9_examples.py in the lab9 folder. (Re)read the functions defined in that file and experiment with using them as in:

     >>> ns = [44, 66, 55, 99]
     >>> append_first(ns)
     >>> ns
     >>> decrement_list(ns)
     >>> ns
     >>> pop_first(ns)
     >>> ns
     >>> swap(ns, 0, 2)
     >>> ns
     >>> insert_at(ns, 1, 333)
     >>> ns
    
     >>> build_streak(5, 7)
    
     >>> xs = random_list(15)
     >>> xs
     >>> ys = xs[:]   # copying the list
     >>> xs[4] = -1
     >>> xs
     >>> ys
     >>> bubble_sort(xs)
     >>> xs
     >>> ys
     >>> selection_sort(ys)
     >>> ys
  3. Experiment with using the mini_graphics module:

     >>> from mini_graphics import *
     >>> ROWS
     >>> COLUMNS
     >>> clear()
     >>> from random import randrange
     >>> for _ in range(200):
             paint(randrange(COLUMNS), randrange(ROWS))
             update()
     >>> clear()
     >>> for _ in range(200):
             paint(randrange(COLUMNS), randrange(ROWS))
  4. Included in the lab9 folder is a runnable solution module called lab9x.py. You can use it to see the result of executing the solution version of any of the assigned exercises. After running either lab9_examples.py or your version of lab9 as a module in IDLE, you can import lab9x and access the solution versions in the IDLE shell as follows:

     >>> import lab9x
     >>> lab9x.build_zero_array(5)

If one of the exercises you are working on requires the solution to a previous exercise that you have not yet completed, you can use the solution version.

  1. Rename your lab9.py as described above (following the aforementioned lab9_alpha_beta.py form). Use IDLE to open your renamed file in the lab9 folder. Remove the starter comment line in that file. Replace <YOUR NAME(s) GO HERE> with your name(s). Save as you go. Run the module in IDLE, test your functions in the IDLE shell.

Challenge problems available upon request.