Author Dusty Phillips Reviewers



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

[
 162 
]
The 
GOOG
stock was already in the dictionary, so when we tried to 
setdefault
it to 
an invalid value, it just returned the value already in the dictionary. 
BBRY
was not in 
the dictionary, so 
setdefault
returned the default value and set the new value in 
the dictionary for us. We then check that the new stock is, indeed, in the dictionary.
Three other very useful dictionary methods are 
keys()

values()
, and 
items()
. The 
first two return an iterator over all the keys and all the values in the dictionary. We 
can use these like lists or in 
for
loops if we want to process all the keys or values. The 
items()
method is probably the most useful; it returns an iterator over tuples of 
(key, 
value)
pairs for every item in the dictionary. This works great with tuple unpacking 
in a 
for
loop to loop over associated keys and values. This example does just that to 
print each stock in the dictionary with its current value:
>>> for stock, values in stocks.items():
... print("{} last value is {}".format(stock, values[0]))
...
GOOG last value is 613.3
BBRY last value is 10.50
MSFT last value is 30.25
Each key/value tuple is unpacked into two variables named 
stock
and 
values
(we could use any variable names we wanted, but these both seem appropriate)
and then printed in a formatted string.
Notice that the stocks do not show up in the same order in which they were inserted. 
Dictionaries, due to the efficient algorithm (known as hashing) that is used to make 
key lookup so fast, are inherently unsorted.
So, there are numerous ways to retrieve data from a dictionary once it has been 
instantiated; we can use square brackets as index syntax, the 
get
method, the 
setdefault
method, or iterate over the 
items
method, among others.
Finally, as you likely already know, we can set a value in a dictionary using the
same indexing syntax we use to retrieve a value:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   153   154   155   156   157   158   159   160   ...   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