import sys def Fibonacci(n): a, b, counter = 0, 1, 0 while True: if(counter > n): return yield a a, b = b, a + b counter += 1 f = Fibonacci(15) while True: try: print(next(f), end=" ") except StopIteration: sys.exit()上述程式碼的執行結果如下所示:
>>> import sys
>>> def Fibonacci(n):
... a, b, counter = 0, 1, 0
... while True:
... if(counter > n):
... return
... yield a
... a, b = b, a + b
... counter += 1
>>> while True:
... try:
... print(next(f), end=" ")
... except StopIteration:
... sys.exit()
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
L = [1, 2, 3, 4, 5] T = tuple(i for i in L) print(T)上述程式碼的執行結果如下所示:
>>> L = [1, 2, 3, 4, 5]
>>> T = tuple(i for i in L)
>>> print(T)
(1, 2, 3, 4, 5)
print(sum(i for i in range(100)))上述程式碼的執行結果如下所示:
>>> print(sum(i for i in range(100)))
4950