One of the most commonly asked question in Python interviews is "Have you worked with generators? How and what was the need to use it?" This post will try to explore what are they how and why they are used? Basically, Generator in simple terms can be thought as the function which returns series of results instead of one single result. Syntactic difference between function and generator is, Generator uses "yield" keyword instead of "return" keyword. Generators give us an iterator(Something which we can loop on for ex. list or dictionary) so they can be used with looping constructs such as 'for' and 'while' loops. Example: # Define a generator. def generator(number): while True: number += 1 yield number # Use the generator. x = generator(5) # Now calling the decorator print("Calling generator:") x.next() print("Calling generator:")