不支持元素重复,内容无序,元素的顺序无法保证(每次运行结果可能不同),允许修改
name= {'a','b','c','c'}
print(type(name))
print(name)
集合.add(元素)
name= {'a','b','c','c'}
print(type(name))
print(name)
name.add('d')
print(name)
集合.remove(元素)
name= {'a','b','c','c'}
print(type(name))
print(name)
name.remove('b')
print(name)
元素=集合.pop()
name= {'a','b','c','c'}
print(type(name))
print(name)
popelement=name.pop()
print(name)
print(popelement)
集合.clear()
name= {'a','b','c','c'}
print(type(name))
print(name)
name.clear()
print(name)
differ1=集合1.difference(集合2)
name1= {'a','b','c','c'}
name2={'b','c','d'}
#name1-name2
differ1=name1.difference(name2)
print(differ1)
#name2-name1
differ2=name2.difference(name1)
print(differ2)
集合1.difference_updata(集合2)
改变了集合1,集合2不变
name1= {'a','b','c','c'}
name2={'b','c','d'}
name1.difference_update(name2)
print(name1) #name1对于name2消除交集
print(name2) #name2不变
name1= {'a','b','c','c'}
name2={'b','c','d'}
name3=name1.union(name2) #name1并name2
print(name3)
name= {'a','b','c','c'}
print(type(name))
print(name)
num=len(name)
print(num)
不用while,因为没有下标,没顺序
用for循环
name1= {'a','b','c','c'}
for x in name1:
print(x)