使用生成器生成杨辉三角形:
def triangles():
L = [1]
while(True):
yield L
L = [1]+[x+y for x,y in zip(L[:-1],L[1:])] + [1]
def main():
n = 0
results = []
for t in triangles():
print(t)
results.append(t)
n = n + 1
if n == 10:
break
main()
列表生成式
使用列表生成式生成列表,将首字母大写,其他小写
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [x.capitalize() for x in L1 if isinstance(x,str)]
使用filter求回数
def is_palindrome(n):
return str(n)==str(n)[::-1]
def main():
output = filter(is_palindrome, range(1, 1000))
print('1~1000:', list(output))