CSE 111 Bio: Program Desig I Lecture 17: software developmet, list methods Robert H. Sloa(CS) & Rachel Poretsky(Bio) Uiversity of Illiois, Chicago October 19, 2017
NESTED LOOPS: REVIEW
Geerate times table for ay 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 =4 1 for i i rage(1,+1): 2 for j i rage(1,+1): 3 prit(i*j,ed=' ') 4 prit() To start pritig colum oly at some k > 1 (because I kow my 1's time tables): A. Edit oe umber i the rage statemet i lie 1 (for i) B. Edit oe umber i the rage statemet i lie 2 (for j) C. Both A ad B D. Edit both umbers i the rage statemet i lie 1 (for i) E. Edit both umbers i the rage statemet i lie 2 (for j)
INCREMENTAL DEVELOPMENT, ETC.
How to get hired by Google From the Google HSI CS Faculty Summit Have codig ability i oe of Pytho, Java, or C++ ad be able to demostrate i solvig iterview questio live q Do't go directly to writig code: First discuss with iterviewer to show you ca thik about it, ad to cofirm you uderstood the questio
Icremetal Programmig Extesio of our geeral philosophy: q q q Desig first! Desig top dow Implemet bottom up Implemet bottom up ofte meas buildig workig partial program of, say, at most 20 lies (less if i CS 111!) ad gettig it implemeted ad tested first
Read i CSV ad plot 3 species Maybe first: q q q Read i CSV ad get x's ad y's ad just prit out those two lists Next might be read i CSV ad just do oe x-y plot of all the poits, without splittig out the 3 species Fially do the assiged problem
Watch the master (Prof. Sloa, of course) def plot_icremet(fame, x, y): """Icremetal start o Iris lab; grabs ad prits x ad y""" xlist = [] ylist = [] with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: xlist.apped(float(row[x])) ylist.apped(float(row[y])) prit(xlist) prit(ylist) plot_icremet("bezdekiris.csv", 0, 1) Traceback (most recet call last): File "<ipytho-iput-31-c83ba8cc3347>", lie 1, i <module> plot_icremet("bezdekiris.csv", 0, 1) File "/Users/robertsloa/Google Drive/CS Plus Bio Course 111 Gree/CS111Gree-FA2017/Programmig/slides.plotIcremet.py", lie 18, i plot_icremet xlist.apped(float(row[x])) IdexError: list idex out of rage
@#$%%!! Oh, ca I ot say that i class? First thig to check: The error message, the last bit: what's it tellig us? IdexError: list idex out of rage
So, idex to list out of rage??? def plot_icremet(fame, x, y): """Icremetal start o Iris lab; grabs ad prits x ad y""" xlist = [] ylist = [] with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: xlist.apped(float(row[x])) ylist.apped(float(row[y])) prit(xlist) prit(ylist)
Whe i doubt Start pritig out variables to see what is goig o (Or use upper right pael of Spyder to do the same)
So, pritig def plot_icremet(fame, x, y): """Icremetal start o Iris lab; grabs ad prits x ad y""" xlist = [] ylist = [] with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: prit(row) xlist.apped(float(row[x])) ylist.apped(float(row[y])) prit(xlist) prit(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-virgiica'] ['5.9', '3.0', '5.1', '1.8', 'Iris-virgiica'] []
What happeed? Probably a last lie that had a blak, or maybe that's just how it eds Wat to stop that loop from processig a empty list break or probably better cotiue
break & cotiue break: Causes immediate termiatio of the loop Use carefully! It ca make code very hard to read cotiue: causes this iteratio to be skipped q Need a reaso to use it, but it ca be helpful i some situatios
Fixig our problem with cotiue with ope(fame, 'r') as csvfile: reader = csv.reader(csvfile) for row i reader: if le(row) == 0: # empty? cotiue #regular processig here
Which of these will exit o 9? A x = iput ("Eter a umber: ") while (x%2 == 1 ad x%3 == 0): x = iput ("Eter a umber: ") B x = iput ("Eter a umber: ") while True: if (x%2 == 1 ad x%3 == 0): break x = iput ("Eter a umber: ") C. Both D. Neither E. I do t kow
LIST FUNCTIONS AND SOME REVIEW
Next big project: Populatio Geetics We will see how a populatio evolves by simulatio, hugely successful ad powerful method for doig moder sciece Classical Medelia geetics We'll start with a small populatio represeted as hmm, somethig, i a list q Ad keep buildig that list Today: We will build our list-fu!
Populatio geetics simulatio List of cotaiig iitial populatio while ot at some ed coditi cocerig list: q q q q q Take (1? a matig pair? all?) elemets from the list Usig radom ad biology, determie offsprig (Maybe?) remove breeders from list (geeratio dies) Add ewly bred items to list Get all the liks i that page Compute ad retur (or prit out?) appropriate stats o fial populatio results of "dry lab" experimet
Ad besides Lists i Pytho are just awesome
Lists versus Strigs List Elemets ca be ay type Mutable Heterogeeous elemets Ca be ested i other lists Strig Elemets are characters Immutable Homogeous elemets
Recall Lists are data structures that let us store collectios of data i sequece with idices
Remember the smallest "" # Empty strig, could also be writte '' [] # empty list
A = [2,3,5] B = ["Medel"] C = A + B prit(c) will result i: A. [2,3,5] B. [2,3,5,"Medel"] C. ["2","3","5","Medel"] D. This will cause a error E. I do t kow
List Fuctios These are ot methods; there are also list methods, ad we'll do those i a few miutes (have already see ad used.apped()) le: legth of a list (i.e., umber of elemets) + will cocateate lists mi, max: miimum or maximum of list sum: sum of the elemets i the list (if all umbers) q E.g., sum([2, 3, 5]) à 10
Importat: Lists are mutable >>> years= [1788, 1800, 1860, 1932] >>> years[0] = years[0] + 4 >>> prit(years) [1792, 1800, 1860, 1932]
What is prited lst = ['abc.com', 'c.com', 'msbc.com'] lst[1] = 'fox.com' prit(le(lst)) A. 3 B. 4 C. 23 D. 30 E. No output; error i 2 d lie of code
What is prited lst = ['abc.com', 'c.com', 'msbc.com'] lst[1] = 'fox.com' ow lst has become ['abc.com', 'fox.com', 'msbc.com'] prit(le(lst)) A. 3
LIST METHODS
Source of list methods material Much of this material based o but modified from "CS1 i Pytho Peer Istructio Materials" by Daiel Zigaro, i the repository http://www.peeristructio4cs.org/ licesed uder a Creative Commos Attributio- NoCommercial-ShareAlike 3.0 Uported Licese. (Licese at: https://creativecommos.org/liceses/by-csa/3.0/deed.e_us)
Methods: Gettig ifo You ca lear about methods for stadard built-i types, e.g., strig, list, i q q Pytho documetatio https://docs.pytho.org/3/library/idex.html For may list fuctios ad methods, see Mutable sequece types At IPytho cosole prompt I [1]: dir(list), dir(str), etc. tells ames of all methods For CS 111, igore all methods with ames startig with uderscore; we wo't use
Key list methods ls.apped(item): add item to ed of ls ls.pop(): remove ad retur elemet at ed of ls ls.pop(i): remove ad retur elemet at idex i i ls ls.remove(item): remove first occurrece of item from ls ls.isert(i, item): isert item ito ls at positio i q slidig elemets of ls[i:] all oe positio right to make room ls.sort(): Move elemets of ls so that ls is i sorted order q Requires all elemets to be comparable (all umbers or all strigs) All those methods modify ls Oly pop amog those methods has a retur value
What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop() A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error
What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop() A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error
What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2) # The 2 is what's ew A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error
What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.remove('b') a.pop(2) # The 2 is what's ew A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error
What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b') A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error
What is value of a after code rus? a = ['a', 'b', 'c', 'd'] a.pop(2) a.remove('b') A. ['a', 'b'] B. ['c', 'd'] C. ['a', 'c'] D. ['a', 'd'] E. Nothig; code geerates a error
a == b? a = ['a', 'b', 'c', 'd'] b = a.pop() a. Yes b. No