1.列表生成式 [i*2 for i in range(10)] [fun(i) for i in range(10)] 2.生成器 # Author Qian Chenglong #列表生成器 a=(i*2 for i in range(10)) #a[1]#只是将算法存储了,只有在调用时才会生成相应的数据,不能直接读取 a.__next__()#生成器只能一个一个往后取,且只存储当前值 #函数生成器 # def fib(max): # n,a,b = 0,0,1 # while n < ma…
英文原文出处:Iterables vs. Iterators vs. Generators 在python学习中,通常会陷入对以下几个相关概念之间的确切差异的困惑中: a container(容器) an iterable(可迭代对象) an iterator(迭代器) a generator(生成器) a generator expression(生成器表达式) a {list, set, dict} comprehension(列表/集合/字典推导式) 于是写下这篇文章作为以后的参考. 原文…
循环 python 循环语句有for循环和while循环. while循环while循环语法 while 判断条件: 语句 #while循环示例 i = 0 while i < 10: i += 1; print(i) while else 语句 语法 while 判断条件: 语句 else: 语句 #while else 示例 n = 0 while n < 10: n += 1; print(n); else: print("n不小于10") for循环 for循环可以…