Day 2 Materials

Downloads


Solutions can be found here

Part I: For loops

First, define the following two variables:

numbers = [0, 1, 1, 2, 3, 5, 8, 13]
animals = ["gorilla", "canary", "frog", "moth", "nematode"]
  1. Write a for-loop over the list numbers. At each iteration, print the value in numbers plus 2. In other words, your code should print out the following:

     2 
     3
     3 
     4 
     5 
     7 
     10 
     15
    
  2. Write a for-loop over the list animals. At each iteration, print out the value in animals followed by its length. Your code should print out the following:

    gorilla 7 
    canary 6  
    frog 4    
    moth 4     
    nematode 8  
    
  3. Write a for-loop over the list animals. At each iteration, print out the uppercase version of each animal (do not redefine anything, just print!). Your code should print out the following:

     GORILLA 
     CANARY  
     FROG    
     MOTH    
     NEMATODE
    
  4. Write a new for-loop to create a new list called upper_animals which should contain the uppercase versions of all items in animals. Hint: For this task, you will need to define the list upper_animals before entering the for-loop, and you will need to use the method .append() to build up this list as you go. Print the final list after the for-loop.

  5. Write a for-loop to create a new list called negative_numbers which should contain the negative values of the items in numbers, following a similar procedure to the previous task. Once this list is complete, write an if/else statement to check if the sum (hint: use the function sum()) of negative_numbers equals -1 times the sum of numbers.

  6. Write a for-loop, using the range() function, to create a list of the powers of 2 from to . (Note that in Python, the exponent symbol is **, as in 3**2 = 9).

  7. Modify the previous for-loop to create a list of every other power of 2 (specifically the even exponents) by modifying the arguments given to range().

Part II: Dictionaries

  1. Define this dictionary and perform the following tasks:

     molecules = {"DNA":"nucleotides", "protein":"amino-acids", "hair":"keratin"}
    
    • Add the key:value pair "ribosomes":"RNA" to the molecules and print to confirm.

    • Add another key:value pair, "ribosomes":"rRNA" to the molecules dictionary and print. Do you understand why the dictionary contains the key:value pairs shown?

    • Write a for-loop over the dictionary to print each key-value pair in sentence form, “ is comprised of ." (i.e., "DNA is comprised of nucleotides").

    • Write a for-loop over the dictionary to make a list of keys which are more than 5 letters long. For this task, you should employ an if statement, the len() function, and the .append() method. Print the list once it is made.

    • Write a for-loop over the dictionary to make a list of values which contain the letter a. Perform this task in two ways:

      • Use the .count() method to count the number of a’s in each dictionary value, and use an if statement to determine if a’s were found

      • Use the in statement.

  2. Congratulations, you’ve been hired as a zoo-keeper! Now you have to feed the animals. You received these handy Python dictionaries which tells you (a) to which category each animal belongs, and (b) what to feed each animal category. Perform the following tasks with these dictionaries:

     category = {"lion": "carnivore", 
                 "gazelle": "herbivore", 
                 "anteater": "insectivore", 
                 "alligator": "homovore", 
                 "hedgehog": "insectivore", 
                 "cow": "herbivore", 
                 "tiger": "carnivore", 
                 "orangutan": "frugivore"}
    		 
     feed = {"carnivore": "meat", 
             "herbivore": "grass", 
             "frugivore": "mangos", 
             "homovore": "visitors", 
             "insectivore": "termites"}
    
    • Determine what you should feed the orangutan and print the result.

    • Write a for-loop to loop over “feed” and print out what food each -vore eats. You might find it helpful to first loop over the feed dictionary and simply print the loop variable. Extend the code from there to print the full sentence. Your code should ultimately print the following (in arbitrary order):

        The carnivore eats meat
        The herbivore eats grass
        The frugivore eats mangos
        ...etc
      
    • Write a for-loop to print out what each animal eats. For this task, you should loop over the dictionary category and use indexing to obtain the relevant information from the feed dictionary to create your sentence. Your code should ultimately print the following (in arbitrary order):

        The lion eats meat 
        The gazelle eats grass 
        The anteater eats termites 
        The alligator eats visitors 
        ... etc
      
    • Modify the previous for-loop so that it creates a new dictionary called animals_eat. This dictionary should contain the exact animal:food pairs, e.g. "lion": "meat" will be one key:value pair. Print out the resulting dictionary.

Part III: Practice our skills

  1. A professor has decided to curve grades in a very special way, and you have been tasked with crunching the numbers. Here are the curving rules:

    • Grades above 95 are reduced by 10%
    • Grades between 75-95 (inclusive) remain the same
    • Grades below 75 are raised by 10%.

    Perform the following tasks:

    • Create a list of new grades that reflects these rules from the following original grades:

        grades = [45, 94, 25, 68, 88, 95, 72, 79, 91, 82, 53, 66, 58]
      
    • The professor has changed his mind: he now wants to use a scaling factor of 0.078325 (instead of 0.1), because why not! Recompute the grades using this new scaling. No hard-coding!

    • The nested list below contains three sets of grades for silly professor’s three classes. Create a new nested list with the curved grades for each of these groups.

        all_grades = [[45, 94, 25, 68, 88, 95, 72, 79, 91, 82, 53, 66, 58], 
            		    [23, 46, 17, 67, 55, 42, 31, 73], 
                      [91, 83, 79, 76, 82, 91, 95, 77, 82, 77]]
      
    • Now, imagine that those three sets of grades correspond, in order, to the classes indicated in this list:

       class_names = ["Psychology 101", "Sociology 101", "Political Science 101"]
      

      Create a dictionary representing the curved grades for each of these classes. Your final dictionary should have the class name as keys, and each list of curved grades as values.

  2. For this set of questions, you will calculate the molecular weight of a protein sequence, using this dictionary:

     amino_weights = {"A":89.09, "R":174.20, "N":132.12, 
                     "D":133.10, "C":121.15, "Q":146.15, 
                     "E":147.13, "G":75.07, "H":155.16, 
                     "I":131.17, "L":131.17, "K":146.19, 
                     "M":149.21, "F":165.19, "P":115.13, 
                     "S":105.09, "T":119.12, "W":204.23, 
                     "Y":181.19, "V":117.15} 
    
    • Calculate the molecular weight of this sequence: "GAHYADPLVKMPWRTHC"

    • Now, calculate the molecular weight of this sequence which contains ambiguities: "KLSJXXFOWXNNCPRHGGYA". Assume that the molecular weight of an ambiguous amino acid is the average weight of all amino acids.

  3. For this question, you will count the number of each nucleotide in a DNA sequence.

    • Create a dictionary which contains key:value pairs as nucleotide:count for this sequence: "ACATAGACCAGAGACT". Use the .count() method by looping over a list of nucleotides (nucs = ["A", "C", "G", "T"]) to solve this question.

    • Now, create a similar dictionary for this DNA sequence which contains ambiguities: "AGCTANTAGNNNNNAGGATCCNNAANNNNCATAGC". This time, use a for-loop over the sequence itself to “build up” a dictionary of counts for those characters which appear in the sequence.
    • Modify the previous code to make a dictionary of percentages of nucleotides, rather than counts.