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.
len()
to find the number of items in the list..remove()
.len()
again.)append()
.# 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)
# sort the list alphabetically
aa.sort()
print(aa)
# find the length of the list
aa_length = len(aa)
print("length of list =", aa_length)
# remove 'alanine' from the list
aa.remove('alanine')
aa_length = len(aa)
print("length of new list =", aa_length)
# note that only one 'alanine' was removed from the list
# add selenocysteine to the list
aa.append("selenocysteine")
print(aa)
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.
for
loop. # 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)
# add a 'selenocysteine' key to the dictionary
# give it a one-letter code (value) of 'U'
aa_dict['selenocysteine'] = 'U'
print(aa_dict)
# remove a key-value pair from the dictionary
aa_dict.pop('selenocysteine')
print(aa_dict)
# 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)
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.
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))
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.
data_string
.data_string
.data_string
and combine them into a single, new string that should look like this: "abcdefghijklmnopqrstuvwxyz0123456789"data_string
(the lower-case e) with a captial X. Do not use the function replace()
.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())