Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save normanlmfung/9c293bc8c93c9be7b9ca9fd09c155195 to your computer and use it in GitHub Desktop.
Save normanlmfung/9c293bc8c93c9be7b9ca9fd09c155195 to your computer and use it in GitHub Desktop.
python_syntax_yield_return_fibonacci
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Generate Fibonacci numbers up to a certain limit
def fibonacci_up_to(limit):
fib = fibonacci()
while True:
num = next(fib)
if num > limit:
break
yield num
# Example usage
limit = 100
print("Fibonacci numbers up to", limit, ":")
for num in fibonacci_up_to(limit):
print(num, end=" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment