+ - 0:00:00
Notes for current slide
Notes for next slide

More Python Loops, Arrays, and Functions

1 / 10

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
2 / 10

Logic some more

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"
a not b
a == b
3 / 10

for loops

Reminder of how to use for to iterate through items in a list or in a string

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
4 / 10

Loops with while

Another way to loop is to use the while operator

# hello
i=0
while i < 10:
print "i is ", i
i += 1
5 / 10

functions

Function info

  • def to define the function
  • can use return to inidicate the return from the routine
# 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])
6 / 10

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

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)
7 / 10

Problems

  1. Write code to test if there is a stopcodon in the following text
    AGAGACGAGAGTTACCGACGTAT
  2. Check and see if it is in frame?
8 / 10

search and replace

Using re.sub and re.subn can replace

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)
#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
My dog likes to eat dogfood
My cat likes to eat catfood
My lion likes to eat catfood
9 / 10

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)
10 / 10

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
2 / 10
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow