cse 111 bio: program design i lecture 17: software ... design i lecture 17: software development,...
TRANSCRIPT
CSE111Bio:ProgramDesignILecture17:software
development,listmethods
RobertH.Sloan(CS)&RachelPoretsky(Bio)UniversityofIllinois,Chicago
October19,2017
NESTEDLOOPS:REVIEW
Generatetimestableforanyn
1234246836912481216
n=4
1 for i in range(1,n+1):2 for j in range(1,n+1):3 print(i*j,end=' ')4 print()
To start printing column only at some k > 1 (because I know my 1's time tables):
A. Edit one number in the range statement in line 1 (for i)B. Edit one number in the range statement in line 2 (for j)C. Both A and BD. Edit both numbers in the range statement in line 1 (for i)E. Edit both numbers in the range statement in line 2 (for j)
INCREMENTAL DEVELOPMENT, ETC.
How to get hired by Google
n From the Google HSI CS Faculty Summitn Have coding ability in one of Python, Java, or
C++ and be able to demonstrate in solving interview question liveq Don't go directly to writing code: First discuss with
interviewer to show you can think about it, and to confirm you understood the question
Incremental Programming
n Extension of our general philosophy:q Design first!q Design top downq Implement bottom up
n Implement bottom up often means building working partial program of, say, at most 20 lines (less if in CS 111!) and getting it implemented and tested first
Read in CSV and plot 3 species
n Maybe first:q Read in CSV and get x's and y's and just print out
those two listsq Next might be read in CSV and just do one x-y
plot of all the points, without splitting out the 3 species
q Finally do the assigned problem
Watch the master (Prof. Sloan, of course)
def plot_increment(fname, x, y):"""Incremental start on Iris lab; grabs and prints x and y"""xlist = []ylist = []with open(fname, 'r') as csvfile:
reader = csv.reader(csvfile)for row in reader:
xlist.append(float(row[x]))ylist.append(float(row[y]))
print(xlist)print(ylist)
plot_increment("bezdekIris.csv", 0, 1)Traceback (most recent call last):
File "<ipython-input-31-c83ba8cc3347>", line 1, in <module> plot_increment("bezdekIris.csv", 0, 1) File "/Users/robertsloan/Google Drive/CS Plus Bio Course 111 Green/CS111Green-FA2017/Programming/slides.plotIncrement.py", line 18, in plot_increment xlist.append(float(row[x]))
IndexError: list index out of range
@#$%%!!
n Oh, can I not say that in class?
n First thing to check: The error message, the last bit: what's it telling us?
n IndexError: list index out of range
So, index to list out of range???
def plot_increment(fname, x, y):"""Incremental start on Iris lab; grabs and prints x and y"""xlist = []ylist = []with open(fname, 'r') as csvfile:
reader = csv.reader(csvfile)for row in reader:
xlist.append(float(row[x]))ylist.append(float(row[y]))
print(xlist)print(ylist)
When in doubt
n Start printing out variables to see what is going on
n (Or use upper right panel of Spyder to do the same)
So, printingdef plot_increment(fname, x, y):
"""Incremental start on Iris lab; grabs and prints x and y"""xlist = []ylist = []with open(fname, 'r') as csvfile:
reader = csv.reader(csvfile)for row in reader:
print(row)xlist.append(float(row[x]))ylist.append(float(row[y]))
print(xlist)print(ylist)
We get
['5.1', '3.5', '1.4', '0.2', 'Iris-setosa'] ['4.9', '3.0', '1.4', '0.2', 'Iris-setosa'] ['4.7', '3.2', '1.3', '0.2', 'Iris-setosa'] …['6.2', '3.4', '5.4', '2.3', 'Iris-virginica'] ['5.9', '3.0', '5.1', '1.8', 'Iris-virginica'] []
What happened?
n Probably a last line that had a blank, or maybe that's just how it ends
n Want to stop that loop from processing an empty list
n break or probably better continue
break & continue
n break: Causes immediate termination of the loop
n Use carefully! It can make code very hard to read
n continue: causes this iteration to be skippedq Need a reason to use it, but it can be helpful in
some situations
Fixing our problem with continue
with open(fname, 'r') as csvfile:reader = csv.reader(csvfile)for row in reader:
if len(row) == 0: # empty? continue
#regular processing here
x = input ("Enter a number: ")while (x%2 == 1 and x%3 == 0):
x = input ("Enter a number: ")
x = input ("Enter a number: ")while True:
if (x%2 == 1 and x%3 == 0):break
x = input ("Enter a number: ")
Which of these will exit on 9?
A
B
C. Both D. Neither E. I don’t know
LIST FUNCTIONS AND SOME REVIEW
Next big project: Population Genetics
n We will see how a population evolves by simulation, hugely successful and powerful method for doing modern science
n Classical Mendelian geneticsn We'll start with a small population
represented as hmm, something, in a listq And keep building that list
n Today: We will build our list-fu!
Population genetics simulation
n List of containing initial populationn while not at some end conditin concerning list:
q Take (1? a mating pair? all?) elements from the listq Using random and biology, determine offspringq (Maybe?) remove breeders from list (generation dies)q Add newly bred items to listq Get all the links in that page
n Compute and return (or print out?) appropriate stats on final population–results of "dry lab" experiment
And besides
n Lists in Python are just awesome
Lists versus Strings
List StringElements can be any type
Elements are characters
Mutable ImmutableHeterogeneous elements
Homogenous elements
Can be nested in other lists
Recall
n Lists are data structures that let us store collections of data in sequence with indices
Remember the smallest
n "" # Empty string, could also be written ''
n [] # empty list
A = [2,3,5]B = ["Mendel"]C = A + B print(C) will result
in:
A. [2,3,5]B. [2,3,5,"Mendel"]C. ["2","3","5","Mendel"]D. This will cause an errorE. I don’t know
List Functions
n These are not methods; there are also list methods, and we'll do those in a few minutes (have already seen and used .append())
n len: length of a list (i.e., number of elements)
n + will concatenate lists
n min, max: minimum or maximum of list
n sum: sum of the elements in the list (if all numbers)q E.g., sum([2, 3, 5]) à 10
Important: Lists are mutable
>>> years= [1788, 1800, 1860, 1932]>>> years[0] = years[0] + 4>>> print(years)[1792, 1800, 1860, 1932]
What is printed
lst = ['abc.com', 'cnn.com', 'msnbc.com']lst[1] = 'fox.com'print(len(lst))
A. 3B. 4C. 23D. 30E. No output; error in 2nd line of code
What is printed
lst = ['abc.com', 'cnn.com', 'msnbc.com']lst[1] = 'fox.com'now lst has become ['abc.com', 'fox.com', 'msnbc.com']print(len(lst))
A. 3
LIST METHODS
Source of list methods material
n Much of this material based on but modified from "CS1 in Python Peer Instruction Materials" by Daniel Zingaro, in the repository http://www.peerinstruction4cs.org/ licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 UnportedLicense. (License at: https://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US)
Methods: Getting info
n You can learn about methods for standard built-in types, e.g., string, list, inq Python documentation
https://docs.python.org/3/library/index.htmln For many list functions and methods, see Mutable
sequence typesq At IPython console prompt In [1]: dir(list), dir(str),
etc. tells names of all methodsn For CS 111, ignore all methods with names starting with
underscore; we won't use
Key list methods
n ls.append(item): add item to end of lsn ls.pop(): remove and return element at end of lsn ls.pop(i): remove and return element at index i in lsn ls.remove(item): remove first occurrence of item from lsn ls.insert(i, item): insert item into ls at position i
q sliding elements of ls[i:] all one position right to make roomn ls.sort(): Move elements of ls so that ls is in sorted order
q Requires all elements to be comparable (all numbers or all strings)
n All those methods modify lsn Only pop among those methods has a return value
What is value of a after code runs?
A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code
generates an error
a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop()
What is value of a after code runs?
A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code
generates an error
a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop()
What is value of a after code runs?
A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code
generates an error
a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2)
# The 2 is what's new
What is value of a after code runs?
A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code
generates an error
a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2)
# The 2 is what's new
What is value of a after code runs?
A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code
generates an error
a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b')
What is value of a after code runs?
A. ['a', 'b']B. ['c', 'd']C. ['a', 'c']D. ['a', 'd']E. Nothing; code
generates an error
a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b')
a == b?
a. Yesb. No
a = ['a', 'b', 'c', 'd'] b = a.pop()