What are generators in Python?

0 votes
I am new to Python. I just came across generators in Python. But I  am not able to understand it completely. Can anyone explain to me it in simple words?
Jul 11 in Python by Arvind
• 1,530 points
12 views

1 answer to this question.

0 votes

There are two terms involved when we discuss generators.

  1. Generator-Function : A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
  2. Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop 
So a generator function returns a generator object that is iterable, i.e., can be used as an Iterator

# A simple generator for Fibonacci Numbers 
def fib(limit): 
      
    # Initialize first two Fibonacci Numbers  
    a, b = 0, 1
  
    # One by one yield next Fibonacci Number 
    while a < limit: 
        yield a 
        a, b = b, a + b 
  
# Create a generator object 
x = fib(5) 
  
# Iterating over the generator object using next 
print(x.next()); # In Python 3, __next__() 
print(x.next()); 
print(x.next()); 
print(x.next()); 
print(x.next()); 
  
# Iterating over the generator object using for 
# in loop. 
print("\nUsing for in loop") 
for i in fib(5):  
    print(i) 

Output :

0
1
1
2
3

Using for in loop
0
1
1
2
3

answered Jul 11 by Neel
• 1,610 points

Related Questions In Python

0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 17, 2018 in Python by Johnathon
• 9,070 points
33 views
0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14 in Python by Shashank
• 1,350 points
40 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,670 points

reshown Aug 24, 2018 by Priyaj 51 views
+5 votes
1 answer

What are metaclasses in Python?

A metaclass instantiates and defines behavior for ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 56,160 points
24 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 128 views
0 votes
1 answer

What are decorators in Python?

Python has an interesting feature called decorators to add ...READ MORE

answered 14 hours ago in Python by Neel
• 1,610 points
4 views
0 votes
1 answer

What is setup.py in Python?

Suppose you want to install a module ...READ MORE

answered Jul 10 in Python by Neel
• 1,610 points
18 views