These exercises are about data types from Session 1 session1.
Exercise 1 - Lists
## [1, 2, 3, 4, 5]
x.## ['a', 'b', 'c', 'd', 'e']
## ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
## ['a', 'b', 'c', 'a', 'b']
## 'c'
## ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b']
Notice how the last value of your list is missing, even though you access it with [-1]. That’s because slicing is exclusive, while indexing is inclusive.
Append the list in variable x. Add the string z.
## ['a', 'b', 'c', 'd', 'e', 'z']
## ['a', 'z']
## ['c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'a']
## ['c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a', 'a']
## ['c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a']
Notice that .remove() eliminates only the first instance of your specified entry.
Create a list of lists. Use the values 1,2,3,4,5, along with the lists x and y.
## [[1, 2, 3, 4, 5], ['a', 'z'], ['c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a']]
## 'c'
Exercise 2 - Tuples
## (1, 2, 3, 4, 5)
## ('a', 'z')
## ([1, 2, 3, 4, 5], ['a', 'z'], ['c', 'c', 'c', 'c', 'c', 'b', 'b', 'b', 'b', 'b', 'a', 'a', 'a', 'a'])
Exercise 3 - Dictionaries
my_dict = {
'my_float_list': [1.5,2.5,3.5],
'my_integer_list': [4,5,6],
'my_strings_tuple': ("a","b","c")
}
my_dict## {'my_float_list': [1.5, 2.5, 3.5], 'my_integer_list': [4, 5, 6], 'my_strings_tuple': ('a', 'b', 'c')}
my_dict = {
'GO:0030955': ["PKM", "ADPRH", "ATP4A", "DRG1"],
'GO:0031403': ["IMPA1", "PDXK"],
'GO:0031402': ["SLC6A4", "CAPN3", "TDG", "ATP1A2"]
}
my_dict## {'GO:0030955': ['PKM', 'ADPRH', 'ATP4A', 'DRG1'], 'GO:0031403': ['IMPA1', 'PDXK'], 'GO:0031402': ['SLC6A4', 'CAPN3', 'TDG', 'ATP1A2']}
my_dict = {
'GO:0030955': ["PKM", "ADPRH", "ATP4A", "DRG1"],
'GO:0031403': ["IMPA1", "PDXK"],
'GO:0031402': ["SLC6A4", "CAPN3", "TDG", "ATP1A2"]
}
my_dict.setdefault('GO:0031420', my_dict['GO:0030955'] + my_dict['GO:0031403'] + my_dict['GO:0031402'])## ['PKM', 'ADPRH', 'ATP4A', 'DRG1', 'IMPA1', 'PDXK', 'SLC6A4', 'CAPN3', 'TDG', 'ATP1A2']
## {'GO:0030955': ['PKM', 'ADPRH', 'ATP4A', 'DRG1'], 'GO:0031403': ['IMPA1', 'PDXK'], 'GO:0031402': ['SLC6A4', 'CAPN3', 'TDG', 'ATP1A2'], 'GO:0031420': ['PKM', 'ADPRH', 'ATP4A', 'DRG1', 'IMPA1', 'PDXK', 'SLC6A4', 'CAPN3', 'TDG', 'ATP1A2']}
Hint: Dictionary elements can also be nested dictionaries.
my_dict_names = {
'GO:0030955': {"name": "potassium ion binding",
"genes": ["PKM", "ADPRH", "ATP4A", "DRG1"]},
'GO:0031403': {"name": "lithium ion binding",
"genes": ["IMPA1", "PDXK"]},
'GO:0031402': {"name": "lithium ion binding",
"genes": ["SLC6A4", "CAPN3", "TDG", "ATP1A2"]}
}
my_dict_names## {'GO:0030955': {'name': 'potassium ion binding', 'genes': ['PKM', 'ADPRH', 'ATP4A', 'DRG1']}, 'GO:0031403': {'name': 'lithium ion binding', 'genes': ['IMPA1', 'PDXK']}, 'GO:0031402': {'name': 'lithium ion binding', 'genes': ['SLC6A4', 'CAPN3', 'TDG', 'ATP1A2']}}
## 'lithium ion binding'
## ['IMPA1', 'PDXK']
Exercise 4 - Sets
my_set1 = {"PKM", "ADPRH", "ZNF724","ATP4A","GSS", "DRG1", "FA2H", "MCM10"}
my_set2 = {"SLC6A4", "MCM10", "CAPN3", "TDG", "ATP1A2","GSS", "BRCA1", "HIVEP2", "PKM"}## {'PKM', 'GSS', 'MCM10'}