5.2. Loops

The two statements while and for are used to write loops in Python.

5.2.1. while

The while construct executes a block of code while a condition is true.

Example 5.3. Find all occurrences of a restriction site

from string import *
	
def restrict(dna, enz):
    "print all start positions of a restriction site"
    site = find (dna, enz)
    while site != -1:
        print "restriction site %s at position %d" % (enz, site)
        site = find (dna, enz, site + 1)

>>> restrict(dna, EcoRI)
restriction site gaattc at position 188
restriction site gaattc at position 886
restriction site gaattc at position 1326

5.2.2. for

The loop construct for iterates over all members of a sequence.

Caution

This is equivalent to the foreach statement in some other programming languages. It is not the same as the for statement in most other programming languages.

Example 5.4. Remove whitespace characters from a string

>>> from string import *

>>> whitespace
'\t\n\x0b\x0c\r '
>>> dna = """ 
... aaattcctga gccctgggtg caaagtctca gttctctgaa atcctgacct aattcacaag
... ggttactgaa gatttttctt gtttccagga cctctacagt ggattaattg gccccctgat
... tgtttgtcga agaccttact tgaaagtatt caatcccaga aggaagctgg aatttgccct
... tctgtttcta gtttttgatg agaatgaatc ttggtactta gatgacaaca tcaaaacata
... ctctgatcac cccgagaaag taaacaaaga tgatgaggaa ttcatagaaa gcaataaaat
... gcatggtatg tcacattatt ctaaaacaa """

>>> for s in whitespace:
...     dna = replace(dna, s, "")
... 
>>> dna
'aaattcctgagccctgggtgcaaagtctcagttctctgaaatcctgacctaattcacaagggttactga
agatttttcttgtttccaggacctctacagtggattaattggccccctgattgtttgtcgaagaccttac
ttgaaagtattcaatcccagaaggaagctggaatttgcccttctgtttctagtttttgatgagaatgaat
cttggtacttagatgacaacatcaaaacatactctgatcaccccgagaaagtaaacaaagatgatgagga
attcatagaaagcaataaaatgcatggtatgtcacattattctaaaacaa'
	  

Exercise 5.1. Count ambiguous bases

Write a function returning the number of ambiguous bases in a DNA sequence (Solution A.10).

5.2.3. More about loops

Python provides the following advanced features while executing a loop:

  • to quit a loop before the end condition is true by using break

  • to go directly to the next iteration step by using continue

  • to execute code only if the loop was not interrupted with break by using the else statement following the while clause

    Caution

    The else statement is also executed if the loop is not entered.

Example 5.5. Find a unique occurrence of a restriction site

def restrict_uni(dna, enz):
    """ find unique restriction sites """

    found = None
    site = dna.find(enz)

    while site != -1:
        if found:
            break
        found = site
        site = dna.find(enz, found+1) 

    else:
        if found is not None:                                             (1)
            return found
1

The test ensures that a restriction site occurrence at position 0 is also true.

Exercise 5.2. Check DNA alphabet

Write a loop to verify all bases in a DNA sequence. (Solution A.11).

Example 5.6. Find all possible start codons in a cds

def find_starts (cds):
    """ find start codons in a cds """

    start = -1
    while 1:
        start = cds.find("atg", start+1)
    
        if start == -1:
            break
    
        if start % 3:
            continue                                                      (1)

        print "possible start codon at position %d" % start
1

The continue statement is used to skip all atg codons that are out of frame.

Go back

Return at the end of Section 2.1.