-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
56 lines (43 loc) · 1.24 KB
/
dictionary.py
File metadata and controls
56 lines (43 loc) · 1.24 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
56
#Implement methods for dictionary data structures
info = {
"name":"divyansh",
"age":19,
"course":"PYTHON"
}
#Print dictionary
print(info)
#Returns a copy of the dictionary
copied = info.copy()
print(copied)
#Returns a dictionary with the specified keys and value
keyss = ('key1', 'key2', 'key3')
common_values = "value"
thisdict = dict.fromkeys(keyss, common_values)
print(thisdict)
#Returns the value of the specified key
age = info.get("age")
print(age)
#Returns a list containing a tuple for each key value pair
items = info.items()
print(items)
#Returns a list containing the dictionary's keys
keys = info.keys()
print(keys)
#Removes the element with the specified key
info.pop("age")
print(info)
#Removes the last inserted key-value pair
info.popitem()
print(info)
#Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
x = info.setdefault("age",19)
print(x)
#Updates the dictionary with the specified key-value pairs
info.update({"course":"web development"})
print(info)
#Returns a list of all the values in the dictionary
vals = info.values()
print(vals)
#Removes all the elements from the dictionary
info.clear()
print(info)