Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Exercises 14.1

Loop through the following DNA sequence using range() and print each base.

dna_seq = 'ACGGATATATATATGCCCGTAATATATAGCG'
for i in range( len(dna_seq) ): print( dna_seq[i] )

Exercises 14.2

Modify your code so that it prints all quintuplets (5 consecutive bases). For example, the first few quintuplets are "ACGGA", "CGGAT", "GGATA", etc.

  • Hint: This is slightly different to the example in Notebook 14 in which the step size was 3. Here we want a step size of 1.

for i in range( len(dna_seq)-4 ): print( dna_seq[i:i+5] )

Exercises 14.3

Modify your code so that it counts all occurrences of the quintuplet "TATAT".

count = 0 for i in range( len(dna_seq)-4 ): if dna_seq[i:i+5] == 'TATAT': count += 1 print(f'"TATAT" occurs {count} times')

Exercises 14.4

Use range() to calculate the sum of all the numbers in the following list. Print the sum.

arriving_penguins = [10, 156, 73, 376, 786, 432, 1035, 901, 1102, 2567, 1571, 916, 1560, 632, 943, 246, 654, 1456, 504, 632]
total = 0 for i in range( len(arriving_penguins) ): total += arriving_penguins[i] print(f'The total number of penguins is {total}' )

Exercises 14.5

Use range() to simultaneously loop through the following two lists. Add each pair of numbers and print them.

x = [1.2, 12.3, -23.6] y = [15.0, -5.4, 4.5]
for i in range( len(x) ): print (f'{x[i]} + {y[i]} = {x[i]+y[i]}')