-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab02D-Solution.py
More file actions
55 lines (32 loc) · 1.29 KB
/
Lab02D-Solution.py
File metadata and controls
55 lines (32 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
print("Q1")
dictionary = {"Shanghai":17.8, "Istanbul":13.3, "Karachi": 13.0, "Mumbai": 12.5}
print(dictionary)
print("Q2 - string, int, float")
# A list cannot be a key because keys must be immutable
print("Q3")
keys = ['key1', 'key2', 'key3']
values = [1, 2, 3]
# zip will take the first item in the keys sequence and
# pair it with the first value in the values sequence
# Then the second tiem in each sequence...
pairs = zip(keys, values)
# The dict constructor will take each incoming pair (k, v)
# and load it into the dictionary with k as the key and v as the value
dictionary = dict(pairs)
print(dictionary)
print("Q4")
dict_1 = {'key1': 1, 'key2': 2, 'key3': 3}
dict_2 = {'key4': 4, 'key5': 5, 'key6': 6}
dict_1.update(dict_2)
print(dict_1)
# If dict_1 must not be modified then
dict_3 = dict(dict_1) # Copy the dictionary, why is dict_3 = dict2 not going to work?
dict_3.update(dict_2)
print(dict_3)
# Add a key4 to dict_1, then we will see what happens with the update
dict_1 = {'key1': 1, 'key2': 2, 'key3': 3, 'key4': -1}
dict_1.update(dict_2) # The incoming key4 : 4 value will overwrite the original -1
print(dict_1)
print("Q5")
dictionary = {"class": {"student": {"name": "Mike", "marks": {"physics": 70, "history": 80}}}}
print(dictionary["class"]["student"]["marks"]["history"])