有序不可变序列,其中的元素可以是任意对象
创建:Tuple = ( ... , ... , ......)
Tuple = ... , ... , ......
将其他序列转为元组:Tuple = tuple(...)
单元素元组声明:t = (1,) 必须要加个,
运算同str
print([i for i in range(5)]) # [0, 1, 2, 3, 4] print([i for i in range(5) if i % 2]) # [1, 3] print([(x, y) for x in range(5) if x % 2 == 0 for y in range(5) if y % 2 == 1]) # [(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]
无序可变
元素由键和值组成,键不可变且唯一
量大的时候,字典运行快
Dict = {key:value,}
Dict = dict{key = value,}
Dict = dict([(key,value),])
字典推导式(同列表)
遍历键值
D = {"a": 1, "b": 2, "c": 3} for key, value in D.items(): print(key, end=":") print(value) """ a:1 b:2 c:3 """ # 分别遍历key(D.keys)和value(D.values)也ok