Python Essentials Classroom Series – 13/Aug/2021

Generator Functions

  • A generator in a Python is a Sequence creation object, using which we can iterator potentially huge sequences
  • We have already used range() function which is generator.
  • Refer Here for the code used to use a generator
  • Write a Generator function to print prime numbers in the sequence
for prime in prime_numbers(start=10, end=100):
  • The Solution is
def is_prime(number):
    for index in range(2, number//2 + 1):
        if number%index == 0:
            return False
    return True

def prime_numbers(start=2, end=100):
    for number in range(start, end+1):
        if is_prime(number):
            yield number

if __name__ == '__main__':
    for prime in prime_numbers(100,120):

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin