-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal hw problem
48 lines (45 loc) · 1.44 KB
/
Final hw problem
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
dictionary = {"a": 4, "b": 5, "c": [6, 7]}
dictionary_1 = {"a": 8, "m": 2, "v": 7}
#update merges dictionary with dictionary 1
dictionary.update(dictionary_1)
print(dictionary)
#pop item will remove v
x = dictionary.popitem()
print (x)
print(dictionary)
#pop will remove b and prints the avlue of b which is 5
x = dictionary.pop("b")
print (x)
#key loops through all the kys in the dictionary and print them, keys are a, b c, m, v.
for key in dictionary.keys() :
print (key)
#values are on the right hand side
for value in dictionary.values() :
print (value)
for key, value in dictionary.items() :
print (key, "-", value)
#get returns value of the item with the specified key
x = dictionary.get("b")
print (x)
#from keys creates a dictionary, uses all keys from dictionary,
# value 1 from all of them and puts them into dictionary 1.
dictionary_1 = dict.fromkeys(dictionary, 1)
print (dictionary_1)
#copy creates a copy on an existing list
dictionary_1 = dictionary.copy()
dictionary_1["b"] = 2
print (dictionary)
print (dictionary_1)
#clear removes all items from list
dictionary.clear()
print (dictionary)
length = len(dictionary)
#set default returns value/sets default value to the key which is
# 100 since no D in dictionary so it uses default dicionary
dictionary.setdefault("d",100)
print(dictionary["d"])
print ("length of dictionary is :", length)
count = 0
for key in dictionary.keys():
count = count + dictionary[key];
print("Count:",count)