Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet98/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   94   95   96   97   98   99   100   101   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

What Are Sets?
Sets share the same characteristics of lists and dictionaries. A set is a collection of information like a list; however, like a key in a dictionary, sets can only contain unique values. They are also an unordered collection. This means that they cannot be accessed by index but rather by the value itself like dictionary keys. They can be iterated through though, like how dictionary keys can be looped over. Sets are practical in situations of storing unique items.

Declaring a Set


There are two ways to declare a set. The first way is by using the keyword “set” followed by parenthesis and enclosing square brackets. The second way, which is more practical, looks like a dictionary being declared by using a set of curly brackets. Let’s check it out:

# declaring a set
s1 = set( [1, 2, 3, 1] ) # uses the set keyword and square brackets s2 = {4, 4, 5} # uses curly brackets, like dictionary print( type(s1), type(s2) ) s1.add(5) # using the add method to add new items to a set s1.remove(1) # using the remove method to get rid of the value 1 print(s1) # notice when printed it removed the second "1" at the end

Go ahead and run the cell. We’ll see that it outputs the types for both variables as “sets”. When we output the value of our s1 variable, it ends up outputting “1, 2, 3” only. Remember that sets are unique items, so it drops the second “1” value. Sets have various methods that allow us to add, remove, and change information within them, as seen with the add/remove lines.
What Are Frozensets?
Frozensets are essentially the combination of a set and a tuple. They are immutable, unordered, and unique. These are perfect for sensitive information like bank account numbers, as you wouldn’t want to alter those. They can be iterated over, but not indexed.

Declaring a Frozenset


To declare a frozenset, you use the keyword “frozenset” followed by parenthesis and enclosing square brackets. This is the only way you can declare a frozenset. Let’s check out an example:

# declaring a frozenset fset = frozenset( [1, 2, 3, 4] ) print( type(fset) )

Go ahead and run the cell. We won’t use frozensets too often in this book, but all these data collections serve a specific purpose for use in the Python language.
ChapTer 6 DaTa ColleCTions anD Files

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   94   95   96   97   98   99   100   101   ...   200




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