元组基础
if __name__ == '__main__':
创建元组
if __name__ == '__main__':
t = ('python', 'world', 98)
print(t)
print(type(t))
t = 'python', 'world', 100
print(t)
print(type(t))
t1 = tuple(('python', 'world', 99))
print(t1)
print(type(t1))
t2 = (10, )
print(t2)
print(type(t2))
t = ()
print(t)
t = tuple()
print(t)
元组设计成不可变序列的原因
if __name__ == '__main__':
if __name__ == '__main__':
t = (10, [20, 30], 9)
print(t)
print(type(t))
for i in range(0,3):
print(t[i], type(t[i]), id(t[i]))
print(id(t[1]))
t[1].append(100)
print(t)
print(id(t[1]))
遍历元组
if __name__ == '__main__':
t = ('python', 'world', 99)
for item in t:
print(item)
集合基础
if __name__ == '__main__':
创建集合
if __name__ == '__main__':
s = {2, 3, 4, 5, 6, 7, 7}
print(s)
s = set(range(6))
print(s)
print(type(s))
s = set([1, 2, 3, 4, 5])
print(s)
print(type(s))
s = set((1, 2, 3, 4))
print(s)
print(type(s))
s = set('python')
print(s)
print(type(s))
s = set({1, 2, 3, 4})
print(s)
print(type(s))
s = {}
print(type(s))
s = set()
print(type(s))
判断集合元素
if __name__ == '__main__':
s = {1, 2, 3, 4, 5}
print(10 in s)
print(10 not in s)
添加集合元素
if __name__ == '__main__':
s = {1, 2, 3, 4, 5}
s.add(6)
print(s)
s.update({10, 20, 30})
s.update([40, 50])
s.update((60, 70))
print(s)
集合的删除操作
if __name__ == '__main__':
s = {1, 2, 3, 4, 5, 6, 7, 8}
s.remove(8)
print(s)
s.discard(6)
print(s)
s.pop()
print(s)
s.clear()
print(s)
集合间的关系
if __name__ == '__main__':
s1 = {10, 20, 30, 40}
s2 = {40, 30, 20, 10}
print(s1 == s2)
print(s1 != s2)
s1 = {10, 20, 30, 40, 50, 60}
s2 = {40, 30, 20, 10}
s3 = {1, 2, 10}
print(s2.issubset(s1))
print(s3.issubset(s1))
s1 = {10, 20, 30, 40, 50, 60}
s2 = {40, 30, 20, 10}
s3 = {1, 2, 10}
print(s1.issuperset(s2))
print(s1.issuperset(s3))
s1 = {10, 20, 30, 40, 50, 60}
s2 = {40, 30, 20, 10}
s3 = {1, 2, 10}
print(s1.isdisjoint(s3))
print(s2.isdisjoint(s3))
集合的数学操作
if __name__ == '__main__':
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)
总结
|