-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path13 dict.py
More file actions
134 lines (102 loc) · 3.18 KB
/
13 dict.py
File metadata and controls
134 lines (102 loc) · 3.18 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
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
# def:IN DICTIONARY WE Store the elelment in the formof key and value dict is changable,
# (acc:python 3.7)orderable,also donot allow duplicate values using{key:value}brackets
print("==========get()==========")
dict1={"name":"Saman","Father":"Tariq","Mother":"Shabnam","s_Age":19}
print(dict1.get("Father"))
print("=======================values()==================================")
print(dict1. values())# it will print the values of the key
print()
print("=============================keys()======================")
print(dict1.keys())# print keys of the values
print()
print("=========================items()=====================================")
print(dict1.items()) #print key+value whole item
print("==========check==========")
key="s_Age"
if key in dict1:
print(True)
else:
print(False)
print("==============update======================")
dict1.update({"updating_age":20})
print(dict1)
print()
print("=====================add item=================================")
dict1["country"]="Pakistan"
print(dict1)
print("==========================update existing value=========================")
dict1.update({"Father":"Tariq Rajput"})
print(dict1)
print()
print("==========================pop()================================")
dict1.pop("updating_age")# delete pecific item
print(dict1)
print()
print("===============popitem()==========")
dict1.popitem()#delete last inserted item
print(dict1)
print("=======================copy()=================")
dict2=dict1.copy()
#or
dict3=dict(dict1)
print(dict2," =>dict2")
print(dict3," =>dict3")
print("===========clear()============")
dict3.clear()
print(dict3,"=>dict3 ")
# #we can also use del
# del dict2
# print(dict2)#it will give an error
print("===========================loop in dict========================")
#print keys of dict1
for i in dict1:
print(i)
print()
#print values
for i in dict1:
print(dict1[i])
print()
#print values
for i in dict1.values():
print(i)
print()
#print keys
for i in dict1.keys():
print(i)
print()
#print whole items
for i in dict1.items():
print(i)
print("============================nested dict1=================")
dict4={
"Fruits":{
"Mango":"yellow","apple":"red","kiwi":"brown"
},
"Animal":{
"cat":"meow","dog":"bhao,bhao","goat":"maaaaaaa"
}
}
print(dict4["Animal"]["dog"])
print()
## loop for nested dictionary
for i,k in dict4.items():
for j in k:
print(j+":",k[j])# ,=>provide automatic space in print
print()
# setdefault()
print("==================set default=============")
dict5={"nadia":"ggggg"}
dict5.setdefault("age",22)# set default value if key does not exsist if it exsist return old value
print(dict5)
print()
print("=======used in nested dict===============")
school={}
school.setdefault("class1",{})
school["class1"].setdefault("student1",{})
school["class1"]["student1"].setdefault("name","nadia")
print(school)
print()
# fromkeys(): use to add new dict for every key havingsame value
keys=["name","rollno","subject"]
d=dict.fromkeys(keys,"unknown")
print(d)