-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdictionary.py
295 lines (219 loc) · 6.25 KB
/
dictionary.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# key-value pairs in dictionary
# simple introducion
info = {'first_name':'yash','last_name':'marmat','age':22,'city':'delhi'}
print('Full Name: ' + info['first_name'] + ' ' + info['last_name'])
# lets say we want more key-value pairs in above dictionary
info['interest'] = 'coding'
print(info)
# empty dictionary
info2 = {} # empty dictionary
info2['name'] = 'sam' # adding new key-value pair in an empty dictionary
info2['location'] = 'paris'
print(info2)
# modifying values in ditionary
fruits = {'Yellow':'Banana'}
# lets replace banana with mango
print(fruits)
fruits['Yellow'] = 'mango'
print(fruits)
# removing key-value pairs
info3 = {'first_name':'yash','last_name':'marmat','age':22,'city':'delhi','interest':'coding'}
print(info3)
del info3['interest']
print('\ndeleting interest field...')
print(info3)
# taking a poll
languages = {
'sam':'c++',
'jason':'python',
'edward':'java',
'peter':'c#'
}
print("Sam's favourite language is " + languages['sam'])
avoiding errors if a key-value pair is not assigned --> by get() method
bio = {
'name':'yash',
'age':22,
'location':'delhi'
}
print(bio['interest']) <-- this will give keyerror, so use below code
data = bio.get('interest','no such key-value pair is present')
print(data)
'''
# Sorting in dictionary
# Also, Dictionary and list together
f = {
'bob':'python',
'harry':'ruby',
'jack':'c',
'stan':'java',
}
list1 = ['jack','stan']
for each in f.keys():
print(each.title())
if each in list1:
print('hi ' + each.title()+' you said your fav lang is ' +f[each].title()+ '!')
note: that in last line the 'each' will work for 'values' as well, just mention it with dictionary name.
You can also use the keys() method to find out if a particular person was polled.
means if a person is not present in list we can send a message like,
if 'peter' not in f.keys():
print('Peter please mention your fav language')
for every in sorted(f): # sorting in dictionary
print('Thanks ' + every.title() + ' for taking the poll')
output:
Thanks Bob for taking the poll
Thanks Harry for taking the poll
Thanks Jack for taking the poll
Thanks Stan for taking the poll
# question 1: Introducion
'''
person = {
'first':'yash',
'last':'marmat',
'age':22,
'city':'delhi'
}
a = 'Full Name: ' + person['first'] + ' ' + person['last']
b = 'Age: ' + str(person['age'])
c = 'city: ' + person['city']
print(a.title())
print(b)
print(c.title())
'''
# question 2: Guessing numbers
'''
fav = {
'sam':5,
'peter':10,
'jack':12,
'sid':6
}
print("Sam's favourite number : " + str(fav['sam']))
print("Peter's favourite number : " + str(fav['peter']))
print("Jack's favourite number : " + str(fav['jack']))
print("Sid's favourite number : " + str(fav['sid']))
'''
# question 4: Printing keys and values only
'''
glossary = {
'loops':'for looping',
'append':'for adding alements',
'del':'for deleting elements',
'print':'to print something',
'dictionary':'contains key-value pairs'
}
for k,v in glossary.items():
print(k + ': ' + v)
'''
# question 6: Favourite language poll
'''
fav_lang = {
'sam':'python',
'peter':'c#',
'edward':'ruby',
'jack':'java',
}
names = ['sam','jack','shon','hanna']
for each in names:
print('Name: ' + each.title())
if each not in fav_lang.keys():
print(each.title() + ' take the poll')
else:
print(each.title() + ' thanks for participating')'''
# question 7: Introduction of multiple person (using nested dictionary)
# Method 1
'''
people = [] # an empty list
person = {
'first':'yash',
'last':'marmat',
'age':22,
'city':'delhi',
}
people.append(person)
person = {
'first':'edward',
'last':'kenwey',
'age':42,
'city':'hawana',
}
people.append(person)
person = {
'first':'conor',
'last':'kenwey',
'age':32,
'city':'boston',
}
people.append(person)
for each in people:
a1 = '\nName: ' + each['first'] +' ' + each['last']
a2 = 'Age: ' + str(each['age'])
a3 = 'City: ' + each['city']
print(a1.title(),'\n' + a2,'\n' + a3.title()) '''
# question 7: Method 2(using nested dictionary, also without lists)
'''
people = {
'person1': {
'name':'yash',
'age':22,
'location':'delhi',
},
'person2':{
'name':'edward',
'age':46,
'location':'hawana',
},
}
for k,v in people.items():
print(k.title() + ':')
print('Name: ' + v['name'].title())
print('Age: ' + str(v['age']))
print('Location: ' + v['location'].title() + '\n') '''
# note: if you are extracting the information of a dictionary from a list, then you dont need key-value pairs while looping.
# note: if you are extracting the information from a dictionary by using key-value pairs, then you do not need to specify a list.
# question 8: Pet details
'''
pet_info = []
pet1 = {
'pet type':'dog',
'owner name':'edward',
}
pet_info.append(pet1)
pet2 = {
'pet type':'cat',
'owner name':'sam',
}
pet_info.append(pet2)
pet3 = {
'pet type':'squirrel',
'owner name':'jack',
}
pet_info.append(pet3)
for each in pet_info:
print('Animal Type: ' + each['pet type'].title())
print("Owner's Name: " + each['owner name'].title() + '\n') '''
# question 9: Favourite places poll
'''
favourite_places = {
'edward':['INDIA', 'USA', 'CHINA'],
'sam':['JAPAN', 'DUBAI', 'INDIA'],
'peter':['UK', 'PARIS', 'JAPAN'],
}
for k,v in favourite_places.items():
print('\n' + k.title() + ' likes to visit:')
for each in v:
print('-> ' + each)
'''
# question 10: Favourite numbers poll
'''
fav = {
'sam':[5,8,10,15],
'peter':[10,16,21,24],
'jack':[1,12,18,22],
'sid':[6,5,8],
}
print("Sam's favourite numbers are : " + str(fav['sam']))
print("Peter's favourite numbers are : " + str(fav['peter']))
print("Jack's favourite numbers are : " + str(fav['jack']))
print("Sid's favourite numbers are : " + str(fav['sid']))
'''