Lab Worksheet 8 Solutions

Problem 1: Create a list with your favor amino acids (use Google if you can't remember your amino acids); your list should have at least 5 amino acids. Then, complete the following steps using Python.

  1. Sort the list so that the amino acids appear in alphabetical order.
  2. Use len() to find the number of items in the list.
  3. Remove any number of amino acids from your list using .remove().
  4. How many amino acids are in your list now? (Use len() again.)
  5. Add the amino acid "selenocysteine" to your list using the function append().
In [1]:
# create a list containing the amino acids arginine, alanine, cysteine,
# histine, glutamic acid, and leucine
aa = ['arginine', 'alanine', 'cysteine', 'histidine', 
      'alanine', 'glutamic acid', 'leucine']
print(aa)
['arginine', 'alanine', 'cysteine', 'histidine', 'alanine', 'glutamic acid', 'leucine']
In [2]:
# sort the list alphabetically
aa.sort()
print(aa)
['alanine', 'alanine', 'arginine', 'cysteine', 'glutamic acid', 'histidine', 'leucine']
In [3]:
# find the length of the list
aa_length = len(aa)
print("length of list =", aa_length)
length of list = 7
In [4]:
# remove 'alanine' from the list
aa.remove('alanine')
aa_length = len(aa)
print("length of new list =", aa_length)
length of new list = 6
In [5]:
# note that only one 'alanine' was removed from the list
In [6]:
# add selenocysteine to the list
aa.append("selenocysteine")
print(aa)
['alanine', 'arginine', 'cysteine', 'glutamic acid', 'histidine', 'leucine', 'selenocysteine']

Problem 2: Create a dictionary using the amino acids from the first (unedited) list you created above, where the amino acid is the key and their one-letter code is the value (use Google to find the one-letter codes if you can't remember them). Then, complete the following steps using Python.

  1. Add "selenocysteine" to your dictionary and give it a value of "U".
  2. Remove a key-value pair from your dictionary. Google "python delete entry from dictionary" to find out how.
  3. Print out a list of the keys and a list of the values in your dictionary (i.e., the amino acid names and their one-letter codes).
  4. Create a string from the one-letter codes in your amino acid dictionary. Hint: Use a for loop.
In [7]:
# define a dictionary where the keys are the amino acids
# and the values are the one-letter codes
aa_dict = {'arginine':'R', 'alanine':'A', 'cysteine':'C', 'histidine':'H',
           'alanine':'A', 'glutamic acid':'E', 'leucine':'L'}
print(aa_dict)
{'arginine': 'R', 'alanine': 'A', 'cysteine': 'C', 'histidine': 'H', 'glutamic acid': 'E', 'leucine': 'L'}
In [8]:
# add a 'selenocysteine' key to the dictionary
# give it a one-letter code (value) of 'U'
aa_dict['selenocysteine'] = 'U'
print(aa_dict)
{'arginine': 'R', 'alanine': 'A', 'cysteine': 'C', 'histidine': 'H', 'glutamic acid': 'E', 'leucine': 'L', 'selenocysteine': 'U'}
In [9]:
# remove a key-value pair from the dictionary
aa_dict.pop('selenocysteine')
print(aa_dict)
{'arginine': 'R', 'alanine': 'A', 'cysteine': 'C', 'histidine': 'H', 'glutamic acid': 'E', 'leucine': 'L'}
In [10]:
# define an empty string
sequence = ''

# loop through the values (letters) in the dictionary
# and add each letter to the string
for letter in aa_dict.values():
    sequence += letter
print(sequence)
RACHEL

Problem 3: Using only list indexing and the string function split(), write code that extracts the weight (here the number 7) from data_string defined below, and print out the result. What is the type of your final result? Use Python's type() function to find out.

In [11]:
data_string = "height: 15; weight: 7; width: 5;"

# here are two approaches for solving this question 
# there are more ways, particularly using regular expressions!

# one way 
split_data = data_string.split(": ")
weight = split_data[2][0]
print(weight, type(weight))

# another way
split_data = data_string.split("; ")
weight_raw = split_data[1]
weight = weight_raw.split(" ")[1]
print(weight, type(weight))
7 <class 'str'>
7 <class 'str'>

Problem 4: Using only string indexing, write code that does each of the following. In each case, store the result in a new variable and print its contents.

  1. Extract the first 10 characters from the string data_string.
  2. Extract characters 27 to 52 (corresponding to the upper-case letters of the alphabet) from the string data_string.
  3. Extract characters 1 to 26 (corresponding to the lower-case letters of the alphabet) and characters 53 to 62 (corresponding to the numbers) from the string data_string and combine them into a single, new string that should look like this: "abcdefghijklmnopqrstuvwxyz0123456789"
  4. Replace the letter at position 5 in data_string (the lower-case e) with a captial X. Do not use the function replace().
  5. Take the resulting string from the previous point and convert it all to upper case.
In [12]:
data_string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

# extract the first 10 characters
print(data_string[:10])

# extract characters 27-52
print(data_string[26:52])

# extract characters 1-26 and 53-62
combined_string = data_string[:26] + data_string[52:]
print(combined_string)

# replace e with X
substituted_string = data_string[:4] + "X" + data_string[5:]
print(substituted_string)

# turn the previous string to uppercase
print(substituted_string.upper())
abcdefghij
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789
abcdXfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
ABCDXFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789