set python
# set is collection of unordered items.
# each item in the set is unique and immutable.
# in set we can not store list and dictionary.
# set is mutable but set elememts are immutable.
# -------------------------------------------------------
# sett={1,2,3,5,8,4,"ved",78.9, "ved"}
# print(sett)
# print(type(sett))
# empty set-> set()
sett=set()
# print(type(sett))
# set methods-
# 1- add()-> add elements in set.
# sett.add(1)
# sett.add(2)
# sett.add(2)
# print(sett)
# 2- remove()-> if you want to remove item which is not present in set , then you get error.
# sett.remove(1)
# print(sett)
# discard()-> use for remove value , if value is not present then no error.
# sett.discard(2)
# print(sett)
# 3- clear()-> remove all elements from set.
# sett.clear()
# print(sett)
# 4- pop()->remove random elements from set.
# set.pop()
# print(sett)
# 5- len()-> size of set
# print(len(sett))
# 6- union()->
# set1={1,2,3}
# set2={3,4,5}
# print(set1.union(set2))
# 7-intersection()->
# print(set1.intersection(set2))
# 8- update()-> add sequence in a set , inplace
# set1={1,2,3,4}
# new_list=[5,6]
# set1.update(new_list)
# print(set1)
# symmetric_difference()-> which is not common in both set.
# print(set1.symmetric_difference(set2))
Comments
Post a Comment