列表表示式
a_range = range(10)
# 對a_range執行for表示式
a_list = [x * x for x in a_range]
# a_list集合包含10個元素
print(a_list)
# 巢狀生成
e_list = [[x, y, z] for x in range(5) for y in range(4) for z in range(6)]
# e_list列表包含120個元素
print(e_list)
元祖表示式
a = (x for x in range(1,10))
print(a)
<generator object <genexpr> at 0x0000020BAD136620>
print(tuple(a))
(1, 2, 3, 4, 5, 6, 7, 8, 9)
字典表示式
olddict={'C語言中文網': 6, 'c.biancheng.net': 15}
newdict = {v: k for k, v in olddict.items() if v>10}
print(newdict)
集合表示式
tupledemo = (1,1,2,3,4,5,6,6)
setnew = {x**2 for x in tupledemo if x%2==0}
print(setnew)