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 i in words: print(i)
golf level spoon reverser noon racecar cell rotator tape stats bridge lagoon tenet

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 i in words: print(i[::-1])
flog level noops resrever noon racecar llec rotator epat stats egdirb noogal tenet

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 i in DNA_sequences: if 'ATG' in i: print('yes') else: print('no')
yes no no yes no

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 i in haemoglobin_levels: if i > 11.6 and i < 15.0: print('between the normal values of 11.6 and 15.0 g/dl.') else: print('not between the normal values of 11.6 and 15.0 g/dl.')
between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl. between the normal values of 11.6 and 15.0 g/dl. not between the normal values of 11.6 and 15.0 g/dl.

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 i in haemoglobin_levels: if i != '': count += 1 print (count) print(len(haemoglobin_levels))
15 15

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 i in numbers: squares.append(i**2) print(squares)
[1, 4, 9, 16, 25, 36, 49]