Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet156/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   152   153   154   155   156   157   158   159   ...   183
Bog'liq
python3-oop

[
 161 
]
As we've seen in previous examples, we can then look up values in the dictionary
by requesting a key inside square brackets. If the key is not in the dictionary, it will 
raise an exception:
>>> stocks["GOOG"]
(613.3, 625.86, 610.5)
>>> stocks["RIM"]
Traceback (most recent call last):
File "", line 1, in
KeyError: 'RIM'
We can, of course, catch the 
KeyError
and handle it. But we have other options. 
Remember, dictionaries are objects, even if their primary purpose is to hold other 
objects. As such, they have several behaviors associated with them. One of the most 
useful of these methods is the 
get
method; it accepts a key as the first parameter 
and an optional default value if the key doesn't exist:
>>> print(stocks.get("RIM"))
None
>>> stocks.get("RIM", "NOT FOUND")
'NOT FOUND'
For even more control, we can use the 
setdefault
method. If the key is in the 
dictionary, this method behaves just like 
get
; it returns the value for that key. 
Otherwise, if the key is not in the dictionary, it will not only return the default value 
we supply in the method call (just like 
get
does), it will also set the key to that same 
value. Another way to think of it is that 
setdefault
sets a value in the dictionary only 
if that value has not previously been set. Then it returns the value in the dictionary, 
either the one that was already there, or the newly provided default value.
>>> stocks.setdefault("GOOG", "INVALID")
(613.3, 625.86, 610.5)
>>> stocks.setdefault("BBRY", (10.50, 10.62, 10.39))
(10.50, 10.62, 10.39)
>>> stocks["BBRY"]
(10.50, 10.62, 10.39)
www.it-ebooks.info


Python Data Structures

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   152   153   154   155   156   157   158   159   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish