-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary Data Type.py
242 lines (184 loc) · 6.05 KB
/
Dictionary Data Type.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'''
DICTIONARY:
* It is a ordered and mutable collection of elements.
* It is denoted by {}.
* It represents the key value pair in the dictionary.
* Insertion order is preserved.
* Hetrogenious objects are allowed.
* Duplicate are not allowed.
* Indexing and slicing are not allowed.
-------------------------------------------------------------------------------------------------------------------------------
* When list and tuple comparison by default datatype is "TUPLE".
* When set and dictionary comparison by default datatype is "DICTIONARY".
-------------------------------------------------------------------------------------------------------------------------------
* We can use list,tuple,and set to represent a group of individual objects as a single entity.
* If we want to represent a group of objects key-value pairs then we should go for dict.
EX : rollno -------- name
phone number ----------- address
Note :
* In c++ and java dict are known as 'Map'.
* Where as in perl and ruby is known as 'Hash'
'''
# Creating a dictionary
d = {}
print(d)
c = dict()
print(c)
a = {100:'laptop' , 200:'Mouse' , 300:'Keyboard'}
print(a[100])
print(a[300])
print(a[1000]) #KeyError
print(a)
# W.A.P to enter name and percentage of marks in a dict and display information on the screen.
rec = {}
n = int(input("Enter the number of students : "))
i = 1
while i <= n:
name = input("Enter the name of the student : ")
marks = int(input("Enter % marks: "))
rec[name] = marks
i = i + 1
print('Name of the Student','\t','% of Marks')
for x in rec:
print('\t',x,'\t\t\t',rec[x])
# ----------------FUNCTIONS IN THE DICT --------------------------
'''
1.dict(): To create a dictionary.
2.len(): Returns the number of items in the dict.
3.clear(): To remove all the elements from the dict.
4.get(): To get the value associated with the key.
d.get(key):
If the key is available then returns the corresponding value otherwise returns None. It wont raise any error.
d.get(key,default value):
If the key is available then returns the corresponding value otherwise returns default value.
5.pop():
d.pop(key):
It removes the entry associated with the specified key and returns the corresponding value.
If the specified key is not available then we will get KeyError.
6.popitem():
It removes an item(key-value) from the dict and return it.
-->If the dict is empty then we will get KeyError.
7.keys(): it returns all keys associated with dict.
8.values(): it returns all values associated with dict.
9.items(): It returns list of tuples represented key-value pairs
[(k,v),(k,v),(k,v)]
10).copy(): To create exactly duplicate dict(cloned copy)
11).setdefault(k,v):
-->If the key is available then this function returns the corresponding value.
-->If the key is not available then the specified key-value will be added as new item to the dict.
12).update(): d.update(x):
All items in the dict x will added to dict d.
'''
# 1 Example :
d1 = dict({100:'sunny',200:'bunny',300:'vinny'})
print(d1)
d2 = dict([(333,'katrina'),(666,'kareena'),(999,'deepika')])
print(d2)
d3 = dict((('l','lilly'),('r','radhika'),('s','sunny')))
print(d3)
# 2 Example :
d1 = dict({100:'sunny',200:'bunny',300:'vinny'})
print(len(d1))
# 3 Example :
d1 = dict({100:'sunny',200:'bunny',300:'vinny'})
print(d1.clear())
# 4 Example :
d1 = dict({100:'sunny',200:'bunny',300:'vinny'})
d = {100:'sunny',200:'bunny',300:'vinny'}
print(d[100])#sunny
print(d[400])#KeyError
print(d.get(100))#sunny
print(d.get(400))#None
print(d.get(100,'pinny'))#sunny
print(d.get(400,'pinny'))#pinny
# 5 Example :
d = {100:'sunny',200:'bunny',300:'vinny'}
print(d.pop(300))#vinny
print(d.pop(400))#KeyError
# 6 Example :
d = {100:'sunny',200:'bunny',300:'vinny'}
print(d.popitem())
d = {}
print(d.popitem())#KeyError
# 7 Example :
d = {100:'sunny',200:'bunny',300:'vinny'}
print(d.keys())
for k in d.keys():
print(k)
# 8 Example :
d = {100:'sunny',200:'bunny',300:'vinny'}
print(d.values())
for k in d.values():
print(k)
# 9 Example :
d = {100:'sunny',200:'bunny',300:'vinny'}
print(d.items())
for k,v in d.items():
print(k,'--->',v)
# 10 Example :
d = {100:'sunny',200:'bunny',300:'vinny'}
d1 = d.copy()
print(id(d))
print(id(d1))
# 11 Example :
d = {100:'sunny',200:'bunny',300:'vinny'}
print(d.setdefault(100,'pinny'))
print(d.setdefault(400,'pinny'))
# print(d)
# 12 Example :
d = {100:'sunny',200:'bunny'}
d1 = {'a':'apple'}
d.update(d1)
print(d)
d.update([(333,'A')])
print(d)
# **************** PROBLEMS ****************
# Q.w.a.p to find number of occurences of each letter present in the given string?
# ----------------------------------------------------------------------------------------------------------------------
# i/p:'missisippi'
# o/p:
# m occurs 1 time
# i occurs 4 times
# s occurs 3 times
# p occurs 2 times
word = input("Enter any word: ")
d = {}
for x in word:
d[x] = d.get(x,0) + 1
for k,v in d.items():
print(k,'occurs',v,'times')
# Q.w.a.p to find number of occurances of each vowel present in the given string
word = input("Enter any word: ")
vowels = {'a','e','i','o','u'}
d = {}
for x in word:
if x in vowels:
d[x] = d.get(x,0) + 1
for k,v in d.items():
print(k,'occurs',v,'times')
# Q.w.a.p to accept student name and marks from the keyboard and create a dictionary, also display student marks by taking student name as input.
n = eval(input("Enter number of students: "))
d = {}
for i in range(n):
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
d[name] = marks
while True:
name = input("Enter student name to get marks: ")
print(d.get(name,-1))
if marks == -1:
print("Invalid student name")
break
else:
print("The marks of",name,"is",marks)
option = input("Do you want to find another student marks[Yes | No] :")
if option.lower() == 'no':
break
print("Thanks for using our application")
# Dictionary comprehension:
# ----------------------------------------
# Comprehension concept applicable for dict also.
squares = {i:i*i for i in range(1,6)}
print(squares)
doubles = {i:2*i for i in range(1,6)}
print(doubles)