What are the generators in python?



What are the generators in python?..

Answer / Uttam Kumar Gupta

Generators in Python are special functions that return an iterator. They allow you to iterate through a sequence or produce values on-the-fly, saving memory. To create a generator, you use the 'yield' keyword instead of 'return'. Here is an example: nn```pythonndef my_generator():n yield 1n yield 2n yield 3ngenerator = my_generator()nprint(next(generator)) # prints 1

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

What does the enum function in python do?

1 Answers  


What does __name__=='__main__' in python mean?

1 Answers  


Why is python weakly typed?

1 Answers  


What animals sleep the least?

1 Answers  


How python is interpreted?

1 Answers  


How does the python version numbering scheme work?

1 Answers  


List out the different types of inheritance available in python?

1 Answers  


Tell me how to find bugs or perform static analysis in a python application?

1 Answers  


What is the output of the below program?

1 Answers  


What is flask-wtf and what are their features?

1 Answers  


Is python or r more useful?

1 Answers  


What is abs in python?

1 Answers  


Categories