Generators

October 5, 2022
Python

Generators

Generators are very easy to implement, but a bit difficult to understand.

Generators are used to create iterators, but with a different approach. Generators are simple functions which return an iterable set of items, one at a time, in a special way.

When an iteration over a set of item starts using the for statement, the generator is run. Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn.

Here is a simple example of a generator function which returns 7 random integers:

import random
def lottery():
   # returns 6 numbers between 1 and 40
   for i in range(6):
       yield random.randint(1, 40)
   # returns a 7th number between 1 and 15
   yield random.randint(1, 15)
for random_number in lottery():
      print("And the next number is... %d!" %(random_number))
Output :
And the next number is... 23!
And the next number is... 36!
And the next number is... 18!
And the next number is... 25!
And the next number is... 22!
And the next number is... 9!
And the next number is... 12!

This function decides how to generate the random numbers on its own, and executes the yield statements one at a time, pausing in between to yield execution back to the main for loop.

VIkas Donta

My name is VIkas Donta and I first discovered Web Designingin 2018. Since then, It has impact on my web design projects development career, and  improve my understanding of HTML/CSS tremendously!

Related Posts

Stay in Touch

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form