Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet168/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   164   165   166   167   168   169   170   171   ...   183
Bog'liq
python3-oop

artists = set()
for song, artist in song_library:
artists.add(artist)
print(artists)
There is no built-in syntax for an empty set as there is for lists and dictionaries; 
we create a set using the 
set()
constructor. However, we can use the curly braces 
(borrowed from dictionary syntax) to create a set, so long as the set contains 
values. If we use colons to separate pairs of values, it's a dictionary, as in 
{'key': 
'value', 'key2': 'value2'}
. If we just separate values with commas, it's a set, as 
in 
{'value', 'value2'}
. Items can be added individually to the set using its 
add
method. If we run this script, we see that the set works as advertised:
{'Sarah Brightman', "Guns N' Roses", 'Vixy and Tony', 'Opeth'}
www.it-ebooks.info


Python Data Structures
[
 174 
]
If you're paying attention to the output, you'll notice that the items are not printed 
in the order they were added to the sets. Sets, like dictionaries, are unordered. They 
both use an underlying 
hash-based data structure for efficiency. Because they are 
unordered, sets cannot have items looked up by index. The primary purpose of a set 
is to divide the world into two groups: "things that are in the set", and, "things that 
are not in the set". It is easy to check whether an item is in the set or to loop over the 
items in a set, but if we want to sort or order them, we'll have to convert the set to a 
list. This output shows all three of these activities:
>>> "Opeth" in artists
True
>>> for artist in artists:
... print("{} plays good music".format(artist))
...
Sarah Brightman plays good music
Guns N' Roses plays good music

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   164   165   166   167   168   169   170   171   ...   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