Insights I got during the course

List comprehension

Efficient way to operate on elements of lists.

In [ ]:
# reading fasta sequences
with open('data/problem_1_question_4.fasta', 'r') as f:
    lines = f.readlines()

# stripping newline and '>' characters with for-loop
L = []
for line in lines:
    L.append(line.strip().strip('>'))

# list comprehension can do it in one line
L = [line.strip().strip('>') for line in lines]

# list comprehension with if-clause, here separating sequence labels and
# sequence

labels    = [line.strip().strip('>') for line in lines 
             if line.find('sequence') >= 0]
sequences = [line.strip().strip('>') for line in lines 
             if line.find('sequence') <  0]
In [ ]:
print(labels)
print(sequences)

The build-in zip() function

Parallel operation on several lists.

In [ ]:
# translate sequence strings in lists of single characters
nucleos = [list(seq) for seq in sequences] 

# take pieces of three lists
for i in range(0,3):
    print(nucleos[i][0:5])

print()

for i in zip(nucleos[0][0:5],nucleos[1][0:5],nucleos[2][0:5]):
    print(i)
#    print(i.count('A'))

Python for-loops have an else clause

It is executed if the loop is terminated normally, i.e., not was terminated by a break statement. Allowed me to shorten my prime-number search script by two lines.

In [ ]:
maxtrial = 1000
primes   = [2]

for trialvalue in range(3,maxtrial,2):   # check odd integers only
    for dividend in primes:              # try only presently determined primes
        if (trialvalue%dividend == 0): break
    else:
        primes.append(trialvalue)

print(primes)