Skip to content

Commit b547816

Browse files
committed
dict
1 parent afe0d56 commit b547816

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

Dictionary/dictionary.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Dictionary
2+
3+
thisDict = {
4+
"name" : "Grande",
5+
"brand" : "Toyota",
6+
"year" : 2025
7+
}
8+
9+
print(thisDict)
10+
print(type(thisDict))
11+
print(len(thisDict))
12+
13+
thisDict2 = {
14+
"Name" : "Axio",
15+
"Hybrid" : False,
16+
"Model" : 2020,
17+
"Color" : ["Red","Green","Blue"]
18+
}
19+
print(thisDict2)
20+
print(type(thisDict2))
21+
22+
# Accesss
23+
24+
print(thisDict2["Name"])
25+
x = thisDict2.get("Model")
26+
print(x)
27+
28+
# Key()
29+
30+
print(thisDict2.keys())
31+
32+
# Values()
33+
34+
print(thisDict2.values())
35+
36+
# Items()
37+
38+
print(thisDict2.items())
39+
40+
if "Model" in thisDict2 :
41+
print('Yes Available!')
42+
43+
# Change / Update
44+
45+
thisDict2["Model"] = 2021
46+
print(thisDict2)
47+
48+
thisDict2.update({"Hybrid":True})
49+
print(thisDict2)
50+
51+
for x in thisDict2 :
52+
print(x,thisDict2[x])
53+
54+
# Keys is x and value is dictionary
55+
56+
for key , value in thisDict2.items():
57+
print(key,value)
58+
59+
if "Name" in thisDict2 :
60+
print("Yours Car is Here")
61+
62+
# Add
63+
64+
myDict = {
65+
"userName" : "MaazKhan",
66+
"employeeId" : 176823,
67+
"underGraduate" : True
68+
}
69+
print(myDict)
70+
71+
myDict["Age"] = 20
72+
print(myDict)
73+
74+
myDict.update({"Country":"Pakistan"})
75+
print(myDict)
76+
77+
# Remove
78+
79+
myDict.pop("Age")
80+
print(myDict)
81+
82+
myDict.popitem()
83+
print(myDict)
84+
85+
del myDict["employeeId"]
86+
print(myDict)
87+
88+
# del myDict;
89+
# print(myDict)
90+
91+
myDict2 = {
92+
"Name" : "Maaz Khan",
93+
"Gender" : "Male"
94+
}
95+
96+
# Copy
97+
98+
myDict_Copy = myDict2.copy()
99+
print(myDict_Copy)
100+
101+
# Nested Dictionaries
102+
103+
carCompany = {
104+
"Corolla":{"Model":2023,"Trasmission":"AutoMatic"},
105+
"City":{"Model":2022,"Trasmission":"Manual"},
106+
"Shark 6":{"Model":2025,"Trasmission":"AutoMatic"},
107+
"Elantra":{"Model":2024,"Trasmission":"Manual"}
108+
}
109+
print(carCompany)
110+
111+
# Access
112+
113+
print(carCompany["Corolla"])
114+
115+
print(carCompany["Elantra"]["Model"])
116+
117+
# Loop
118+
119+
for x, obj in carCompany.items():
120+
print(x , obj);
121+
122+
for y in obj:
123+
print(y + ':', obj[y])
124+
125+
keys = {"Name","UserName","DisplayName"};
126+
default_values = "Maaz";
127+
128+
newDict = dict.fromkeys(keys,default_values)
129+
print(newDict)

0 commit comments

Comments
 (0)