class: center, middle # More Python Loops, Arrays, and Functions --- #Loops and logic Useful to walk through your data and perform logic Testing operators * a < b - test if something is less than * a > b - test if something is greater than * a >= b - test if something is greater than or equal * a > b __and__ b > c - combine logic using AND * a > b __or__ b > c - combine using OR * __not__ ( a < b ) - negate the result with NOT --- #Logic some more ```python a=1 b=2 if a != b: print "a not b" else: print "a == b" if not (a != b): print "a not b" else: print "a == b" ``` ```text a not b a == b ``` --- #for loops Reminder of how to use for to iterate through items in a list or in a string ```python dna="AAGATCAGGAC" for c in dna: print "DNA base is " + c for c in range(len(dna)): print "DNA base is " + dna[c] lst = [70,78,12] for n in lst: print "item is", n ``` --- #Loops with while Another way to loop is to use the while operator ```python # hello i=0 while i < 10: print "i is ", i i += 1 ``` --- #functions Function [info](https://docs.python.org/2/tutorial/controlflow.html#defining-functions) * def to define the function * can use return to inidicate the return from the routine ```python # code to calculate the average def average(lst): """This is the average function """ sum = 0; for n in lst: sum += n return sum / len(lst) print average([1,2,3,4,5,100]) ``` --- #Regular expressions and matching Regular expressions - there are many components I will explain on the board, but here is the main syntax The documentation is best [read here](https://docs.python.org/2/library/re.html) ```python import re m = re.search('(dog)', 'dogwood'): if m: print " contains dog" print m.group(0) m = re.search('(d[aoe]g)', 'dogwood'): if m: print m.group(0) m = re.search('(d[aoe]g)', 'dugwood'): if m: print m.group(0) ``` --- #Problems 1. Write code to test if there is a stopcodon in the following text ```text AGAGACGAGAGTTACCGACGTAT ``` 2. Check and see if it is in frame? --- #search and replace Using re.sub and re.subn can replace ```re.sub(_pattern_,_replacement_,_string_,count_num_times_to_perform)``` subn() can replace the same as sub() but returns the updated string and count of number of changes instead of updating the string ```re.subn(_pattern_,_replacement_,_string_,count_num_times_to_perform)``` ```python #replace dog with cat import re str = "My dog likes to eat dogfood" print str str2 = re.sub("dog","cat",str) print str2 str3 = re.sub("cat","lion",str2,1) print str3 ``` ```text My dog likes to eat dogfood My cat likes to eat catfood My lion likes to eat catfood ``` --- #More advanced Regex use Regular expressions can be 'compiled' which will make them run faster and simply the use when you need to repeatedly search a pattern. For example, to enumerate the locations of all the sites that match ``` import re motif = "\s+([Tt]he)\s+" message = "It was the darkest and stormiest of night. The candles were lit but the wind kept blowing them out. -OutTheDoor" the_pattern = re.compile(motif) m = the_pattern.search(message) while m: print "matched ",m.group(1), " from ", m.start(1), " to ", m.end(1) m = the_pattern.search(message,m.end()+1) ```