Essential Computing for Bioinformatics High-level Programming with Python Iteration Over Sequences MARC: Developing Bioinformatics Programs Alex Ropelewski PSC-NRBSC Bienvenido Vélez UPR Mayaguez Reference: How to Think Like a Computer Scientist: Learning with Python
Essential Computing for Bioinformatics
Iterative Factorial Example def iterFact(n): result = 1 for i in xrange(1,n+1): result = result * i return result xrange(start,end,step) generates a sequence of values : The for loop is a somewhat less general iterative construct Every for can be written as a while, yet for loops tend to be easier to read and understand. For loops are recommended when we have an a priori list of values that we want to iterate over Examples of such lists of values are: Python lists or tuples Discrete ranges of integer numbers start = first value end = value right after last one step = increment These materials were developed with funding from the US National Institutes of Health grant #2T36 GM008789 to the Pittsburgh Supercomputing Center
Anatomy of the For Loop for <var> in <sequence>: <block> SYNTAX Repeat the execution of the <block> binding variable <var> to each element of the sequence SEMANTICS These materials were developed with funding from the US National Institutes of Health grant #2T36 GM008789 to the Pittsburgh Supercomputing Center
Complementing a DNA sequence Write a Python function that takes a DNA sequences as an parameter and returns its complement: Check validity of DNA sequence Repeat for each nucleotide in sequence Fetch the next nucleotide Find its DNA complement nucleotide Append the complement nucleotide to result sequence Return the complement sequence
Complementing a DNA sequence return result return True These materials were developed with funding from the US National Institutes of Health grant #2T36 GM008789 to the Pittsburgh Supercomputing Center
Complementing a DNA sequence from string import * DNANucleotides='acgt' DNAComplements='tgca' raise Exception (" Invalid DNA sequence: " + n) return (type(nucleotide) == type("") and len(nucleotide)==1 and nucleotide.lower() in DNANucleotides) These materials were developed with funding from the US National Institutes of Health grant #2T36 GM008789 to the Pittsburgh Supercomputing Center