Created
March 30, 2024 22:19
-
-
Save normanlmfung/9c293bc8c93c9be7b9ca9fd09c155195 to your computer and use it in GitHub Desktop.
python_syntax_yield_return_fibonacci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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