1、赋值
set1 = {"a","b","c",1,2} set2 = {"a",1}
2、集合运算(交、并、差、对称差集)
1)交集符号:&
print(set1&set2)
2)并集符号:|
print(set1|set2)
3)差集符号:-
print(set1-set2) #差集符号:-,及属于set1不属于set2 print(set2-set1)
4)对称差集:^
print(set1^set2) #对称差集符号:^,单独属于set1和set2的元素,及set1和set2的并集,减去set1与set2的差集
3、子集判断
print(set1>=set2) #判断set2是否为set1的子集
4、常用方法
set1.discard(2) #删除元素2,不存在不报异常 print(set1) set1.pop() #删除随机参数 print(set1)