0% found this document useful (0 votes)
4 views11 pages

Sets

Uploaded by

hadihussan573
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Sets

Uploaded by

hadihussan573
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

മലയാളത്തിൽ

▪ Mathematically a set is a collection of items not in any particular order.


▪ A Python set is similar to this mathematical definition with some additional
features.
▪ The elements in the set cannot be duplicates.
▪ The elements in the set are immutable(cannot be modified) but the set as a whole is
mutable.
▪ There is no index attached to any element in a python set. So they do not support
any indexing or slicing operation.
▪ A set is created by using the set() function or placing all the elements
within a pair of curly braces.
▪ S=set() #empty set
▪ Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
▪ Months={"Jan","Feb","Mar"}
▪ Dates={21,22,17}
▪ print(Dates)
▪ {21,22,17} #output
▪ We cannot access individual values in a set. We can only access all the elements
together
▪ But we can also get a list of individual elements by looping through the set.
▪ Days=set(["Mon","Tue","Wed“,” Thu","Fri","Sat","Sun"])
for d in Days:
print(d)
Output
Wed
Sun
Fri
Tue
Mon
Thu
Sat
▪ We can add elements to a set by using add() method.
▪ There is no specific index attached to the newly added element.
▪ Days=set(["Mon","Tue","Wed”])
▪ Days.add("Sun")
▪ print(Days)
▪ {“Mon”,”Sun”,”Tue”,”Wed”} #output
▪ We can remove elements from a set by using discard() method.
▪ Days=set(["Mon","Tue","Wed",“Sun"])
▪ Days.discard("Sun")
▪ print(Days)
▪ {“Mon,”Tue”,”Wed”}
▪ The union operation on two sets produces a new set containing all the distinct
elements from both the sets.
▪ Example
▪ DaysA = set(["Mon","Tue","Wed"])
▪ DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
▪ AllDays = DaysA|DaysB
▪ print(AllDays)
▪ {'Wed', 'Fri', 'Tue', 'Mon', 'Thu', 'Sat’} #Output
▪ The intersection operation on two sets produces a new set containing only the
common elements from both the sets.

▪ Example
▪ DaysA = set(["Mon","Tue","Wed"])
▪ DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
▪ AllDays = DaysA & DaysB
▪ print(AllDays)
▪ {‘Wed’} #Output
▪ The difference operation on two sets produces a new set containing only the
elements from the first set and none from the second set.

▪ Example
▪ DaysA = set(["Mon","Tue","Wed"])
▪ DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
▪ AllDays = DaysA - DaysB
▪ print(AllDays)
▪ set(['Mon', 'Tue’]) #Output
▪ We can check if a given set is a subset or superset of another set.
▪ The result is True or False depending on the elements present in the sets.
▪ Example
▪ DaysA = set(["Mon","Tue","Wed"])
▪ DaysB = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
▪ Sub = DaysA <= DaysB
▪ Sup = DaysB >= DaysA
▪ print(Sub)
▪ print(Sup)
#Output
▪ True
▪ True

You might also like