-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdictionary.py
More file actions
125 lines (85 loc) · 2.53 KB
/
dictionary.py
File metadata and controls
125 lines (85 loc) · 2.53 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
# Dictionary = unordered, changable, index
#Making dictionary
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# student2 = dict(name="abdi", email="abdi@gmail.com", age=25)
# print(student)
# print(student2)
# accessing key
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# # x = student["email"]
# x = student.get("email")
# print(x)
# Change item
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# student["name"] = "Ahmed"
# print(student)
# How to loop over dictionary
#Print Key
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# for x in student:
# print(x)
#Print Value
# for x in student:
# print(student[x])
#print both key and value
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# for x, y in student.items():
# print(x, y)
# Check if item exists in dict
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# if "class" in student:
# print("yes")
# else:
# print("No")
# Check Length of the dict
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# print(len(student))
# How to add an item
# student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# student["subject"] = "English"
# print(student)
# How to remove item
# # student = { "name" : "duraan", "email" : "duran@gmail.com", "age": 30}
# # student.pop("age")
# # student.popitem() # Before Python 3.7, it will remove random
# # del student # Delete the dict
# # student.clear() # Empty dict
# # print("Student: ", student)
# # Nested
# students = {
# "student1": {
# "name" : "duraan",
# "email" : "duran@gmail.com",
# "age": 30,
# "subject": "English",
# "enrolled": True
# },
# "student2" : {
# "name": "Ahmed",
# "email": "Ahmed@gmail.com",
# "age": 25,
# "subject": "Math",
# "enrolled": False
# }
# }
# print(students)
# Making a nested dictionary
# student1 = {
# "name" : "duraan",
# "email" : "duran@gmail.com",
# "age": 30,
# "subject": "English",
# "enrolled": True
# }
# student2 = {
# "name": "Ahmed",
# "email": "Ahmed@gmail.com",
# "age": 25,
# "subject": "Math",
# "enrolled": False
# }
# students2 = {
# "student1": student1,
# "student2": student2
# }
# print(students2)