-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictonary_section.py
260 lines (146 loc) · 4.44 KB
/
dictonary_section.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'''
Limitations of lists
good for storing information, but you'll unsure about what each item stands for
A dictionary
a data structure that consists of key value pairs
we use the keys to describe our data and the values to represent the data
Dictionary example:
instructor = {
"name" : "colt",
"owns_dog" : True,
"num_courses" 4,
"favorite_language" : "Python",
"is_hilarious" : False,
44 : "my favorite number!"
}
Keys and values separated by a colon
Keys are almost always numbers or strings
Values can be anything
lists, dictionary's, strings, ints, ect
Another approach is to use the dict function
You assign values to keys by passing in keys and values
seperated by an =
another_dictionary = dict(key = 'value')
#another_dictionary = dict('key' : 'value')
'''
#EXERCISE
#Create a dictionary called user_info and add 3 key value pairs
'''
user_info = {
"Name" : "Peter",
"Nationality" : "German",
"Age" : 28,
}
user_info = dict(name = "Peter", nationality = "German", age = 28)
print(user_info)
'''
#Accessing Individual Values
'''
To get the value out, you pass in a key
Example:
instructor = {
"name" : "colt",
"owns_dog" : True,
"num_courses" 4,
"favorite_language" : "Python",
"is_hilarious" : False,
44 : "my favorite number!"
instructor["name"] #"Colt"
instructor["thing"] # KeyError
'''
#Access Data in Dictionary Exercise
'''
artist = {
"first": "Neil",
"last": "Young",
}
#Declare a variable called full_name that is equal to arist's first and last name with a space between them
full_name = artist["first"] + ' ' + artist["last"]
print(full_name)
'''
#Iterating Dictionaries
'''
#Example:
instructor = {
"name" : "colt",
"owns_dog" : True,
"num_courses" : 4,
"favorite_language" : "Python",
"is_hilarious" : False,
44 : "my favorite number!"
}
#for loop method
for v in instructor.values(): # prints values out
print(v)
for k in instructor.keys(): # prints keys out
print(k)
for key,value in instructor.items(): # prints all keys and values.
print(key,value)
# dict.item() returns a list of tuplets.
'''
'''
donations = dict(sam=25.0, lena=88.99, chuck=13.0, linus=99.5, stan=150.0, lisa=50.25, harrison=10.0)
# Use a loop to add together all the donations and store the resulting number in a variable called total_donations
#total_donations = for v in donation.values()
total_donations = 0 #creates variable sets it to 0
for donation in donations.values():
total_donations += donation #adds the next itteration of the loop to the varible
print(total_donations)
'''
#checking if a dictionary has a key
'''
#Example:
instructor = {
"name" : "colt",
"owns_dog" : True,
"num_courses" : 4,
"favorite_language" : "Python",
"is_hilarious" : False,
44 : "my favorite number!"
}
#keys
"name" in instructor #True
"awesome" in instructor #False
#values
"Colt" in instructor.values() # True
"Nope!" in instructor.values() #False
'''
#Dictonary Methods
'''
#clear, clears all the keys and values in a dictionary
d = dict(a=1, b=2,c=3)
d.clear()
#copy, makes a copy of a dictionary
d = dict(a=1, b=2,c=3)
c = d.copy()
c # {'a' : 1, 'b' : 2, 'c' : 3}
c is d #False
#fromkeys, creates key value pairs from comma separated values. called in an empy dictionary
{}.fromkeys("a", "b") #{'a': 'b'}
#get, retrieves a key in an object and return None instead of an key error if they does not exist
d = dict(a=1, b=2, c=3)
d['a'] #1
d.get('a') #1
d['no_key'] #KeyError
d.get('no_key') #None
'''
###Dictionary Access Practice####
# This code picks a random food item:
from random import choice
food = choice(["cheese pizza", "quiche","morning bun","gummy bear","tea cake"]) #DON'T CHANGE!
bakery_stock = {
"almond croissant" : 12,
"toffee cookie": 3,
"morning bun": 1,
"chocolate chunk cookie": 9,
"tea cake": 25
}
if food in bakery_stock:
print("{} left".format(bakery_stock[food]))
else:
print("We dont have that")
quantity = bakery_stock.get(food)
if quantity:
print("{} left".format(quantity))
else:
print("We dont have that")