labo etahracarerthn Onebrveteste! Da AraicyaStPyhon Sep ounce
Set
1.Setis a built-in collection data type.
2. Sets are used to store multiple items ina single variable
3. Sets are written with curly brackets.
eg: $= (1,23)
5 = (,2,34)
print (“Example Set:",5)
zxample Set: (1, 2, 3, 4)
Characteristics of a Set
set itens are :
Pron set
4 Unorderes Itene in a Set go not have a defined order.set dtens can appear in a different ond
‘every tine you use then, and cannot be referred to by index or key.
2. Unchangeabte: (Immutable) + Once a set Ss created, you cannot change ts stems, but you can renove Stems snd a
new stens.
3. Do not allow duplicate values: Dp not allow same values tore than one tine.
check whether set ollow duplicate volves
5 = (2,2.3124,2)
sue (a, tera)
print(s1) # print’ itens in different order than the input order
Print(s) aduplicate values hos been removed
(ib, fat, d's‘)
ty, 3,4)
The values True and 1 (also, False and 0) are considered the same value in sets, and are
treated as duplicates:Pron set
5 = (2,2, True,3,@,False}
print(s) #True ,False has been removed
{8 1, 2, 3)
Set Length
1en():tethod to ind the Length of @ set-Length means the no. of Htene in a set.
ength of 2 set
5 = (2,2,3,4,5)
print(“Lengeh of sot Ss: *,2en(s))
Length of set is: 5
Set Items - Data Types
Setitems can take any dat type. Setcan have diferent datatypes as their leet.
type() : Method to find the type of a variale
‘set with integer, string, bootean dato ‘tens
Ss = (4,2,3,4) # Urteger dota ttens
52 = Cwelcone™,"to", "set", Tutorial") #string dota itens
(True, False} sbootean’ data itens
S# = (3,"Welcone", True) #2ifferent data itens
print(si,"\n",52,"\n",53,"\n" 58)
(2, 2, 3, 4)
‘(Urutorial’, "set", to’, “welcone"}
(false, True)
(Welcone", True, 3)
identify the type of « varile
5 = (2,2,3,4)
print (type(s)
«class ‘set">
The set() Constructor
beahos SothracnserthinlOneDrveiDaste!ta naeuPynan'SatPython Sept soubaerfePron set
set) + Constructor of class Set, used to make a set.
set constructor
Y= (2,345)
5 set)
print(s) # List turned to o set
print (type(s)
fy 2, 3, 4 5)
class "Set"
Set Methods
Python has a set of built-in methods that you can use on sets.
ethod Description
ad) ‘Adds. an elenent to the set
clear) enoves 811 the elenents from the set
comy() Returns 2 copy of the set
airterence() Returns » set containing the difference between two oF more sets
{4:Fference pdate() Renaves the itens in this set that are also included in anather, specified set
fiscard() enove the specified {tex
Sntersection() Returns 2 set, that is the intersection of two other sets
fntersection_update() Renoves the ions in this set that are not present in other, specified set(s)
ssdisjoint) Returns whether two sets have 2 intersection or not
sssubset() Returns whether another set contains this set or not
{ssuperset() Returns whether this set contains another set or not
pe90) Renaves an elenent from the set
Fenove() enoves the specified elerent
union() Return a set containing the union of sets
Update) Update the set with the union of this set and others.
Access Set Items
Me cannot access itens in a set by referring to an Index or 2 key.
But you can loop through the set iters using for loop.
in "keyword to check wether an Stem present or not
‘Loop through each ‘ten using for Loop
22 (2,345)
labo etahracarerhinl OneDrvetesks!Din AraicayaStPyhon Sep ouncenaa. nea Pron set
for tin
prane(t)
‘rin keyword to check on item present or not
5 = (1,2.3,4,5)
if ain st
print ("yes Teen present)
else:
print (°No, Tten not present)
yes; dan present
Change Items
\We can not change an existing item in a set but we can add new items tothe set
Add Set Items
aad() ethod in set used to add new itens to the set. This method takes
‘exactly one argunent,
Update() : To add Stans fran anather set into the current set. The object in the update() nethod
does not have to be a set, it can be any iterable object (tuples,
17 Add naw ‘ten to the set
5 = (2,345)
5.246(6)
prine(s)
(4, 23, 4, 5, 6)
# update() wethod
su (.2,3,4,5)
5,9, 10)
Le ate")
‘llupdate(s2) # add tens from anather set
S2.update(1) odd ttens fron @ Uist
prine(s2)
print (=2)
labo etahracarerhinOnebrvtests!da AracayarStPyhor Sep cuban
Usts, dictionaries etc.).naa. nea Pron set
(2 2, 3, 4y 5, 6, 7, 8 9, 20)
15, 7, 8, 9,28, fc", “4, "bY, 12")
Remove Set Items
renove() + Method to renove an Sten from set.If the Sten to remove does not exist,
iscard() + Method to renove an Sten fron set.If the Sten to renove does not exist,
pon) ethoe to renove an ster, but this nethod will renove a randon stem,
0 you cannot be sure what ‘ten that gets renoved.
clear() + Method to enpties the set.
det 1 keyword to delete the set completely.
suse of renove()
5 (8,23,4,5)
.renove(3)
He.renove(®) throw ervor since {tem dosen't exist
prine(s)
(2, 4, 5)
suse of dtscara()
5 (112.3445)
s.dlecard(3)
sudiscard(6) #WiLl not throw error even