Data Types in Python Set and different operations available on set
Set
Set do not have duplicate values
Values cannot be accessed using an index, need to iterate through loop
a={8,7,5,3,2.5,"hello",True,False,0}
#only unique and increasing order
print(a) #{False, True, 2.5, 3, 'hello', 5, 7, 8}
a.add(10)
a.add(7)
a.add(True)
print(a) #{False, True, 2.5, 3, 'hello', 5, 7, 8, 10}
#Order is - boolean (false-true) int or float -(String hasno order)
#from 0 or False - which is added first gets added
#from 1 or True- which gets added first gets added
a.remove(5)
print(a) #{False, True, 2.5, 3, 'hello', 7, 8, 10}
Delete/Remove an Element:
remove(element): Removes element from the set; raises KeyError if element is not found.
discard(element): Removes element from the set; does nothing if element is not found.
pop(): Removes and returns an arbitrary element from the set; raises KeyError if the set is empty.
clear(): Removes all elements from the set.
EX:
a={2,3,4,2,5}
print(a)
for c in a:
if c==2:
break
a.remove(c)
print(a)
------
a={2,4,6,8,3,1}
b={3,6,9,10}
#Values exist in any one
print(a|b) #{1, 2, 3, 4, 6, 8, 9, 10}
#values which exist in both
print(a&b) #{3, 6}
#values of a-b
print(a-b) #{8, 1, 2, 4}
#values only unique to them
print(a^b) #{1, 2, 4, 8, 9, 10}