set()函數用于創建一個集合(set),集合是無序、不重復的元素的集合。
使用方法如下:
my_set = set()
my_set = set([1, 2, 3, 4, 5])
my_set.add(6)
my_set.remove(1)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2) # 求并集
intersection_set = set1.intersection(set2) # 求交集
difference_set = set1.difference(set2) # 求差集