Module 3

Looping through DNA

Applying loops to biological sequences.

Counting bases

Now let's use a loop for something biological. Count how many times the base A appears in a sequence.

Python
seq = "ATGCGCA"
count_a = 0
for base in seq:
    if base == "A":
        count_a = count_a + 1
print(count_a)
2

The loop visits each base. The if statement checks whether it is A. If so, the counter increases. After the loop, count_a holds the answer.

Your task

Count A bases

  • Loop through ATGCGCA and count how many times A appears. Print the count.
Biology connection

This pattern, visiting each element and counting matches, is fundamental in bioinformatics. You will use it constantly.