Round 2.1 --- nested for loops

Oct 26, 2012 • Lex Nederbragt

I picked ‘nested for loops’ as my topic (book/python.html#s:nestloop and http://software-carpentry.org/2012/10/key-points/). Thanks a lot Ross, for pointing to cmap, it is a great little program. Here is my first throw at a concept map.

Worked examples:

One example to illustrate the concept

for i in 'ABC':
    for j in 'abc':
        print i+j

Aa
Ab
Ac
Ba
Bb
Bc
Ca
Cb
Cc

Exercises

1) adjust the example code so that the output becomes
Ca
Cb
Cc
Ba
Bb
Bc
Aa
Ab
Ac

Solution:

for i in 'CBA':
    for j in 'abc':
        print i+j

2) Finish this code:

for i in range(1,4):
    for j in range(1,4):
        print ____

1
2
3
2
4
6
3
6
9

Solution:

print i*j

3) without running it, what will this code’s output be:

dinuc = []
for first_base in 'ACGT':
    for second_base in 'ACGT':
        dinuc.append(first_base + second_base)
print dinuc

Solution:

['AA', 'AC', 'AG', 'AT', 'CA', 'CC', 'CG', 'CT', 'GA', 'GC', 'GG', 'GT', 'TA', 'TC', 'TG', 'TT']

4) what does this code do:

for i in list1:
    for j in list2:
        if i == j:
            matches_found.append(i)

How could you do this in another way?

Solution: returns elements common between list1 and list2. Alternative: use sets and their intersect

 

Real world example: exercise 3, —> all permutations of dinucleotide (two-character) DNA sequences

Explain how this concept relates to something a scientist might actually want to do:
relates to 15-17:. How can I analyze/manage/reformat data, and 19, how to find something in data.