SNIPPET
1 Upvote

Using a Generator and Next Function

Python
General Python

In this example we have a generator which yields even numbers between two limits. Consecutive even numbers are output each time the generator is passed to the "next" function.

def get_even_numbers(lower_limits,upper_limit):
    for i in range(lower_limits,upper_limit):
        if i % 2 == 0:
            yield i
even_numbers_under_100 = get_even_numbers(1,100)

next(even_numbers_under_100)
>> 2

next(even_numbers_under_100)
>> 4

By detro - Last Updated Oct. 30, 2021, 4:44 p.m.

COMMENTS
RELATED SNIPPETS
Python For Else Loop
Python
General Python

Loop

2
1
Search Snippets by Tag: