Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Exercise 2: Lists and Tuples


  1. Imagine that you have a list of matching percentages, matches = [0.6, 0.8, 0.94, 0.2, 0.4], write code to create a new list good_matches which only includes matches better than 0.7.

  2. The previous list didn’t include the name of the sample that the match was associated with. Now imagine that the list consists of both sample names and their match percentiles. Write code to return a new list good_samples that includes the sample names only where the match is better than 0.7. Here is an example input list:

sample_matches = [
  ['SAM1', 0.6],
  ['SAM2', 0.8],
  ['SAM3', 0.94],
  ['SAM4', 0.2],
  ['SAM5', 0.4]
  ]
  1. Write a function find_good_matches that takes a match threshold (e.g. 0.7) and a list like the one presented as good_matches and returns a list of tuples where each tuple contains the sample name and the percentage match for samples over match threshold.

  2. Write a function sum_it(a, b) that takes two numbers, a and b and returns the result of adding them together. Then write a function print_sum(a, b) that takes two numbers, a and b and prints the result of adding the two numbers together. What is the difference between these two functions? Which one returns a value?

  3. Write a function double_it that takes a list of numbers and doubles each of them in place.

  4. What effect does the double_it function have on the original list that you pass as a parameter to the double_it function? What is this effect called?

  5. Write a function double_it2 that takes a list of numbers and returns a new list of the numbers, doubled. Would double_it2 work with a tuple of numbers? Would double_it work with a tuple? Why?