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

Exercise 12.1

Loop through the following list and print out each word.

words = ['golf', 'level', 'spoon', 'reverser', 'noon', 'racecar', 'cell', 'rotator', 'tape', 'stats', 'bridge', 'lagoon', 'tenet']
for word in words: print(word)

Exercise 12.2

Loop through the following list and print out the reverse of each word.

words = ['golf', 'level', 'spoon', 'reverser', 'noon', 'racecar', 'cell', 'rotator', 'tape', 'stats', 'bridge', 'lagoon', 'tenet']
for word in words: print(word[::-1])

Exercise 12.3

Loop through the following list of DNA sequences. Test and print if the start codon "ATG" is in each sequence.

DNA_sequences = ['TATCATGGTAGAT', 'TATATCGCTAGGG', 'CTATAGCCTAGGA', 'TATGAGGATTAGA', 'TATAGACTAGGAT']
for dna_seq in DNA_sequences: if 'ATG' in dna_seq: print(dna_seq)

Exercise 12.4

Loop through the following list of measurements of female haemoglobin levels. Test and print if each measurement is between the normal values of 11.6 and 15.0 g/dl.

haemoglobin_levels = [12.5, 15.1, 12.6, 10.4, 15.7, 9.2, 17.6, 12.9, 10.6, 12.3, 17.9, 14.0, 15.5, 12.5, 10.6]
for x in haemoglobin_levels: if 11.6 < x and x < 15.0: print(x)

Exercise 12.5

The number of items in a list is given by the len() function.

  1. Write your own code that counts the number items in a list and prints it out.

  2. Test your code on any of the lists in these exercises.

count = 0 for x in haemoglobin_levels: count += 1 print(count)

Exercise 12.6

Create an new list called squares of the squares of each number in the list numbers below. That is, the new list should be [1, 4, 9, etc.].

  • Hint: Create an empty list, loop through the list numbers and append the square of each number to the new list.

numbers = [1, 2, 3, 4, 5, 6, 7]
squares = [] for n in numbers: squares.append(n**2) print(squares)