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

Exercise 11.1

  1. Create a list with the following items: 1, 1, 2, 3, 5, 8.

  2. Assign it to the variable called fib.

  3. And print it.

fib = ['1', '1', '2', '3', '5', '8'] print(fib)
['1', '1', '2', '3', '5', '8']

Exercise 11.2

Print the length of the list fib.

print(len(fib))
6

Exercise 11.3

  1. Create an empty list called eggs_laid.

  2. Add the following two items to the list: 10, 11.

  3. And print it.

eggs_laid = [] eggs_laid.append('10') eggs_laid.append('11') print(eggs_laid)
['10', '11']

Exercise 11.4

  1. Sort the following list in place.

  2. Print it.

  • Note: As this is a list of strings, the list will be sorted alphabetically.

cell_types = ['Neutrophil', 'Eosinophil', 'Basophil', 'Small lymphocyte', 'Large lymphocyte', 'Monocyte'] cell_types.sort() print(cell_types)
['Basophil', 'Eosinophil', 'Large lymphocyte', 'Monocyte', 'Neutrophil', 'Small lymphocyte']

Exercise 11.5

  1. Sort the following list and assign it to a new variable called sorted_eggs_laid.

  2. Print sorted_eggs_laid.

eggs_laid = [10,11,14,16,13,14,12,10,16,11,12,13,16,11,10,14,16,16,15,12,11,13,14,15,12,14,12,15,13,13,11] sorted_eggs_laid = sorted(eggs_laid) print(sorted_eggs_laid)
[10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 16, 16, 16, 16, 16]

Exercise 11.6

Access and print items from the second to the sixth positions inclusive from sorted_eggs_laid.

  • Hint: Remember that the first item of a list has index 0.

print(sorted_eggs_laid[1:7])
[10, 10, 11, 11, 11, 11]

Exercise 11.7

Print the reverse of the list sorted_eggs_laid.

print(sorted_eggs_laid[::-1])
[16, 16, 16, 16, 16, 15, 15, 15, 14, 14, 14, 14, 14, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 10, 10, 10]

Exercise 11.8

Get and print the index of the first item with value 14 from sorted_eggs_laid.

print(sorted_eggs_laid.index(14))
18

Exercise 11.9

A single nighttime survey of bats in the Forest of Dean produced a list of the species of all individual bats caught.

Test if the bat species Myotis alcathoe is in the following list bat_list and print whether it is or not.

bat_list = ['Plecotus austriacus', 'Pipistrellus nathusii', 'Myotis daubentonii', 'Nyctalus noctula', 'Pipistrellus pipistrellus', 'Pipistrellus pipistrellus', 'Pipistrellus nathusii', 'Pipistrellus nathusii', 'Eptesicus serotinus', 'Myotis bechsteinii', 'Pipistrellus nathusii', 'Pipistrellus pygmaeus', 'Pipistrellus pipistrellus', 'Plecotus austriacus', 'Myotis daubentonii', 'Nyctalus noctula', 'Myotis brandtii', 'Myotis mystacinus', 'Pipistrellus nathusii', 'Pipistrellus pygmaeus', 'Pipistrellus nathusii', 'Rhinolophus hipposideros', 'Nyctalus leisleri', 'Pipistrellus pipistrellus', 'Pipistrellus nathusii', 'Nyctalus noctula', 'Plecotus austriacus', 'Pipistrellus nathusii', 'Myotis nattereri', 'Pipistrellus pipistrellus', 'Pipistrellus nathusii', 'Plecotus auritus', 'Barbastella barbastellus', 'Pipistrellus nathusii', 'Myotis brandtii', 'Pipistrellus pipistrellus', 'Myotis nattereri']

Exercise 11.10

Test if the bat species Rhinolophus hipposideros is in the above list bat_list. If it is, print its index in the list otherwise print that it is not in the list.

if 'Rhinolophus hipposideros' in bat_list: print(bat_list.index('Rhinolophus hipposideros')) else: print('no')
21