-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.dictionaries.py
189 lines (149 loc) · 4.1 KB
/
5.dictionaries.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
# मेरे Youtube चैनल को सबस्क्राइब करना ना भूलो ताकि आपको कोड का पूरा फ़्लो समझमे आए - https://www.youtube.com/channel/UCphs2JfmIClR62wbyf76HDg
# कोई भी सवाल है उसको मेरे यूट्यूब चैनल के कमेन्ट या डिस्कशन सेक्शन मे पूछ सकते हो - https://www.youtube.com/channel/UCphs2JfmIClR62wbyf76HDg/discussion
# और हाँ GitHub पर मुझे फॉलो करना ना भूलो ताकि सारे अपडेट एण्ड वर्क आपको मिलता रहे।
# Ditionaries
#{key:value}
user = {'name':'Hari','course':'python'}
print('newd: ',type(user))
print('user: ',user['name'])
# add
user['mobile'] = 989879879
print('user: ',user)
# change
user['mobile'] = 999999999
print('updated user: ',user)
# pop
user.pop('course')
print('pop user course: ',user)
user['dob'] ='09th july'
print('user: ',user)
# popitem
user.popitem()
print('updted user: ',user)
# del
del user
# print(user)
# multilevel dictionary
detail = {
'Hari': {
'email':'hari@gamil.com',
'mobile':987987987
},
'Shyam': {
'email':'shyam@gmai.com',
'mobile':87987987,
'Eductaion': {
'BCA': '98%',
'MBA': '76%'
}
}
}
print('\ndetails: ',detail)
print('hari details: ',detail['Shyam']['Eductaion']['BCA'])
# copy
newdetial = detail.copy()
print('\nnewdetial: ',newdetial)
# clear
detail.clear()
print('details: ',detail)
# keys
print('\nnewdetial.keys(): ',newdetial.keys())
# values()
print('\nnewdetial.values(): ',newdetial.values())
'''
Homework:
update()
formkeys()
get()
items()
'''
'''
Programs:
Normal Questions:
1. Diye gaye dict me brad ki salary update karke 8500 karni hai:
sampleDict = {
'emp1': {'name': 'Jhon', 'salary': 7500},
'emp2': {'name': 'Emma', 'salary': 8000},
'emp3': {'name': 'Brad', 'salary': 6500}
}
Expected output:
sampleDict = {
'emp1': {'name': 'Jhon', 'salary': 7500},
'emp2': {'name': 'Emma', 'salary': 8000},
'emp3': {'name': 'Brad', 'salary': 8500}
}
2. Ye do list hai inko dictionary me convert karna hai:
keys = ['Ten', 'Twenty', 'Thirty']
values = [10, 20, 30]
Output kuch aysa hona chaiye:
{'Ten': 10, 'Twenty': 20, 'Thirty': 30}
In 2 dictionary ko ek sath marg karo:
dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}
dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
Aur iska output kuch aysa mulna cahiye:
{'Ten': 10, 'Twenty': 20, 'Thirty': 30, 'Fourty': 40, 'Fifty': 50}
3. Is diye gaye dict mese history ki value ko show karao:
sampleDict = {
"class":{
"student":{
"name":"Mike",
"marks":{
"physics":70,
"history":80
}
}
}
}
Expected answer: 80
4. Diyegaye list ko as a key aur diyegaye dict ko as a value set karo:
employees = ['Kelly', 'Emma', 'John']
defaults = {"designation": 'Application Developer', "salary": 8000}
Dict me check karna hai ki kya 200 value exist karta hai ya nahi:
sampleDict = {'a': 100, 'b': 200, 'c': 300}
Expected output:
True
5. Diye gaye dict me city ko rename karke location karna hai:
sampleDict = {
"name": "Kelly",
"age":25,
"salary": 8000,
"city": "New york"
}
Expected output:
{
"name": "Kelly",
"age":25,
"salary": 8000,
"location": "New york"
}
6. Diye gaye dict me jis subject ka bhi marks sabse kam hai usko find kar ke show karna hai:
sampleDict = {
'Physics': 82,
'Math': 65,
'history': 75
}
Expected output:
Math
Advance Questions:
7. Diye gaye dict mese niche bataye keys ka use kar ke new dict banao:
sampleDict = {
"name": "Kelly",
"age":25,
"salary": 8000,
"city": "New york"
}
Ye keys use karna hai:
keys = ["name", "salary"]
Output:
{'name': 'Kelly', 'salary': 8000}
8. Dict mese following keys ko remove karna hai:
sampleDict = {
"name": "Kelly",
"age":25,
"salary": 8000,
"city": "New york"
}
keysToRemove = ["name", "salary"]
Expected output:
{'city': 'New york', 'age': 25}
'''