Advertisement
elena1234

Fibonacci Numbers in Python

Feb 7th, 2022
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. def fib(number):
  2.     a = 0
  3.     b = 1
  4.     print(a)
  5.     print(b)
  6.     for i in range(number - 1):
  7.         c = a + b
  8.         a = b
  9.         b = c
  10.         yield c
  11.        
  12. for element in fib(10):
  13.     print(element)
  14.  
  15.  
  16. # with List
  17. def fib2(number):
  18.     result = []
  19.     a = 0
  20.     b = 1
  21.     result.append(a)
  22.     result.append(b)
  23.     for i in range(number - 1):
  24.         c = a + b
  25.         a = b
  26.         b = c
  27.         result.append(c)
  28.     return result
  29.  
  30. print(fib2(10))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement