注意:
(1)集合不同于列表和元组类型,集合存储的元素是无序且不能重复的 (2)集合元素类型只能是不可变数据类型,如整数,浮点数,字符串,元组等 (3)由于元素间没有顺序,因此元素不能比较,不能排序 (4)不能使用索引的方式来访问集合的元素
set1 = {1}
print(set1)
print(type(set1))
set2 = set([1,2,3])
print(set2)
set3 = set((1,2,3,4))
print(set3)
set4 = {1,2,3,4,5,6}
for i in set4:
print(i, end='')
set5 = {1,2,3,4}
set6 = {3,4,5,6,7}
print(set5-set6)
print(set5&set6)
print(set5 | set6)
print(set5 ^ set6)
set7 = {1,2,3}
set8 = {1,2}
set9 = {1,2,3,4,5,6}
set10 = {4,5,6,7,8,9}
print(set7.issubset(set8))
print(set8.issubset(set7))
print(set7.issuperset(set8))
print(set8.issuperset(set7))
print(set9.union(set10))
print(set9.intersection(set10))
print(set9.difference(set10))
print(set9.symmetric_difference(set10))
set11 = {1,2}
set12 = set11.copy()
print(set12)
set11 = {1,2,3}
print(set11)
|