元组
集合
1.元组的创建:
'''元组的创建'''
t=('python','world',98) #使用()或者直接省略不写
print(t)
print(type(t))
t1=tuple(('python','world',98))#使用内置函数tuple()
print(t1)
print(type(t1))
t2=(10,) #只包含一个元组的元素需要使用逗号和小括号
print(type(t2))

2.元组的遍历:
t=('python','world',98)
print(t[2]) #使用索引
for i in t:
print(i,end=' ')
?
?3.集合的创建:
s={1,2,3,4,5,6,1,2} #使用{},删除重复元素
print(s,type(s))
s1=set(range(0,6)) #使用set()函数
print(s1,type(s1))
s2=set([3,4,5,6,4])
print(s2)
s3=set('python') #无序输出
print(s3,type(s3))

4. 集合元素的操作:
'''集合元素的判断操作'''
s={10,20,30,40,55}
print(10 in s)
'''集合元素的新增操作'''
s.add(80) #一次增加一个元素
print(s)
s.update([100,98,55])#一次至少添加一个元素
s.update((78,65,55))
print(s)
'''集合元素的删除操作'''
s.remove(65) #一次删除一个元素,如果不存在抛出keyerror
s.discard(10)#一次删除一个元素,如果不存在不抛出异常
s.pop()#一次只删除任意一个元素,不能指定参数
s.clear()#清空集合
print(s)

5.判断两个集合是否相等:
'''判断两个集合是否相等'''
s={10,20,30,40}
s2={30,40,20,10}
print(s==s2)
print(s!=s2)
?
6.判断子集和交集:
'''判断子集'''
a={10,20,30}
b={10,20}
print(b.issubset(a)) #判断b是不是a的子集
print(a.issuperset(b)) #判断a是不是b的超集
'''两个集合是否有交集'''
print(b.isdisjoint(a)) #有交集为false
c={100,200}
print(b.isdisjoint(c)) #无交集为true
?7.集合的数学操作:
'''集合的数学操作'''
s1={10,20,30,40}
s2={20,30,40,50,60}
#求交集
print(s1.intersection(s2))
print(s1&s2)
#求并集
print(s1.union(s2))
print(s1|s2)
#求差集
print(s1.difference(s2))
print(s1-s2)
#对称差集
print(s1.symmetric_difference(s2))
print(s1^s2)
?
?8.集合生成式:
'''集合生成式'''
#列表生成式
lst=[i*i for i in range(6)]
print(lst)
#集合生成式
s={i*i for i in range(10)}
print(s)

9.列表[],元组(),字典(),集合{}的比较:?
?
|