-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
106 lines (89 loc) · 2.29 KB
/
dictionary.py
File metadata and controls
106 lines (89 loc) · 2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'''creating a dictionary
with integer keys'''
Dict={1:'welcome',2:' to sharaz',3:'codes'}
print("\nwiththe use of integer keys:")
print(Dict)
#with mixed keys
Dict={'programmer':'sharaz','integers':[1,2,3]}
print("\nwith mixed keys:")
print(Dict)
#empy dictionary
Dict={}
print("empty dictionary:")
print(Dict)
#with dict()method
Dict=dict({1:'you',2:'sleep',3:'i code'})
print(Dict)
Dict=dict([(1,'programmer'),(2,'sharaz')])
print("\nDictionary with each item as pair")
print(Dict)
#creating nested Dictionary
Dict={1:'sharaz',2:'codes',
3:{'p':'are','Q':'the best'}}
print(Dict)
#Adding elements to a Dictionary
Dict={}
print("empty dictionary")
print(Dict)
#adding elements one at a time
Dict[0]='you'
Dict[1]='sleep'
Dict[2]=3
print("\nafter adding 3 elements")
print(Dict)
'''Adding a set of values to a single
key'''
Dict['value_set']='we', 'code'
print("\nafter adding a set of values")
print(Dict)
#updating an exixting key's value
Dict[0]='they'
print("\nupdated:")
print(Dict)
#addding nested values
Dict[4]={'Nested':{'1':'geeks','2':'for','3':'life'}}
print("\nafter adding nested value")
print(Dict)
#Acessing elements from a dictionary
Dict={1:'welcome',2:' to sharaz',3:'codes'}
print("\naccesing elements using key:")
print(Dict[3])
#using get method
Dict={1:'welcome',2:' to sharaz',3:'codes'}
print("\nAccessing elements using get:")
print(Dict.get(1))
#Accessing elements from a nested dictionary
Dict={'Dict1':{1:'sharaz',2:'codes'},
3:{'p':'are','Q':'the best'}}
print(Dict['Dict1'])
print(Dict['Dict1'][2])
print(Dict[3]['Q'])
'''Deleting elements from a dictionary
del dict-deletes the entire dictionary'''
#initial dictionary
Dict={'Dict1':{1:'sharaz',2:'codes'},
3:{'p':'are','Q':'the best'}}
print("\nintial dictionary:")
print(Dict)
#deleting deleting keys
del Dict['Dict1'][2]
del Dict[3]['p']
print("\nafater deletion")
print(Dict)
#using pop
Dict={1:'sharaz',2:'codes',
3:{'p':'are','Q':'the best'}}
print("\nintial dictionary:")
print(Dict)
pop_ele=Dict.pop(1)
print('\nafter deletion:'+str(Dict))
print('value associated with the popped key is'+str(pop_ele))
#using clear
Dict={1:'sharaz',2:'codes',
3:{'p':'are','Q':'the best'}}
print("\nintial dictionary:")
print(Dict)
#deleting entire dictionary
Dict.clear()
print("\nDeleting entire dictionary")
print(Dict)