lis = [x * x for x in range(1, 100)]
# 增加条件语句
[x * x for x in range(1, 11) if x % 2 == 0]
# 多重循环
[a + b for a in '123' for b in "xyz"]
# 更多用法
dic = {"k1":"v1", "k2":"v2"}
a = [k+":"+ v for k, v in dic.items()]
def fib(n):
a, b, count = 0, 1, 0
while True:
if count > n:
return
yield n # yield让该函数变成一个生成器
a, b = b, a+b
count += 1
res = fib(10)
print("生成器对象:"res)
for i in res:
print(i,end=" ")
一.推导式1.列表推导式:一种快速生成列表的方式,其形式是用方括号括起来的一段语句。 lis = [x * x for x in range(1, 100)] # 增加条件语句 [x * x for x in range(1, 11) if x % 2 == 0] # 多重循环 [a + b for a in '123' for b in "xyz"] # 更多用法 dic =...