Author Dusty Phillips Reviewers



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

File "", line 1, in
AttributeError: can't set attribute
If we need to be able to change stored data, a dictionary may be what we
need instead.
Dictionaries
Dictionaries are incredibly useful containers that allow us to map objects directly to 
other objects. An empty object with attributes to it is a sort of dictionary; the names 
of the properties map to the property values. This is actually closer to the truth than 
it sounds; internally, objects normally represent attributes as a dictionary, where the 
values are properties or methods on the objects (see the 
__dict__
attribute if you don't 
believe me). Even the attributes on a module are stored, internally, in a dictionary.
Dictionaries are extremely efficient at looking up a value, given a specific key 
object that maps to that value. They should always be used when you want to find 
one object based on some other object. The object that is being stored is called the 
value
; the object that is being used as an index is called the 
key
. We've already seen 
dictionary syntax in some of our previous examples.
Dictionaries can be created either using the 
dict()
constructor or using the 
{}
syntax 
shortcut. In practice, the latter format is almost always used. We can prepopulate a 
dictionary by separating the keys from the values using a colon, and separating the key 
value pairs using a comma.
For example, in a stock application, we would most often want to look up prices by 
the stock symbol. We can create a dictionary that uses stock symbols as keys, and 
tuples of current, high, and low as values like this:
stocks = {"GOOG": (613.30, 625.86, 610.50),
"MSFT": (30.25, 30.70, 30.19)}
www.it-ebooks.info


Chapter 6

Download 2,95 Mb.

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