-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathles9_dictionaries.py
152 lines (140 loc) · 3.83 KB
/
les9_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
#https://newdigitals.org/2024/01/23/basic-python-programming/#dictionaries
#Dictionaries are used to store data values in key: value pairs.
#A dictionary is a collection which is ordered (as of Python version 3.7), changeable and do not allow duplicates.
#Dictionaries are written with curly brackets, and have keys and values:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
#It is possible to use the dict() constructor to make a dictionary
thisdict = dict(name = "John", age = 36, country = "Norway")
print(thisdict)
Output:
{'name': 'John', 'age': 36, 'country': 'Norway'}
#You can access the items of a dictionary by referring to its key name, inside square brackets
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
print (x)
Output:
Mustang
#The keys() method will return a list of all the keys in the dictionary
x = thisdict.keys()
print (x)
Output:
dict_keys(['brand', 'model', 'year'])
#The values() method will return a list of all the values in the dictionary
x = thisdict.values()
print (x)
Output:
dict_values(['Ford', 'Mustang', 1964])
#Make a change in the original dictionary, and see that the values list gets updated as well
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
Output:
dict_values(['Ford', 'Mustang', 1964])
dict_values(['Ford', 'Mustang', 2020])
#Check if “model” is present in the dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the thisdict dictionary")
Output:
Yes, 'model' is one of the keys in the thisdict dictionary
#The update() method will update the dictionary with the items from the given argument
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
print (thisdict)
Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}
#Adding an item to the dictionary is done by using a new index key and assigning a value to it
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
#The pop() method removes the item with the specified key name:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
Output:
{'brand': 'Ford', 'year': 1964}
#The clear() method empties the dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
Output:
{}
#Make a copy of a dictionary with the copy() method:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)
Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
#Add Items to a Python Dictionary with Different Data Types
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Dict[0] = 'Go'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)
Dict[2] = 'Welcome'
print("\nUpdated key value: ")
print(Dict)
Dict[5] = {'Nested': {'1': 'Life', '2': 'Python'}}
print("\nAdding a Nested Key: ")
print(Dict)
Output:
Empty Dictionary:
{}
Dictionary after adding 3 elements:
{0: 'Go', 2: 'For', 3: 1}
Dictionary after adding 3 elements:
{0: 'Go', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)}
Updated key value:
{0: 'Go', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4)}
Adding a Nested Key:
{0: 'Go', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4), 5: {'Nested': {'1': 'Life', '2': 'Python'}}}
#Dictionary Methods: clear, copy, fromkeys, get, items, keys, pop, popitem, setdefault, update, and values.