0% found this document useful (0 votes)
10 views5 pages

Python Iterators Generators QA

Uploaded by

dubeykaran2356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Python Iterators Generators QA

Uploaded by

dubeykaran2356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Iterators & Generators

40 Essential Interview-Style Questions & Answers


Contents:
1–20 Iterators
21–40 Generators
Iterators
Q1. What is an iterator in Python?
Ans: An iterator is an object that implements the iterator protocol, consisting of __iter__() and __next__() methods.

Q2. What is the difference between iterable and iterator?


Ans: Iterable: object that can return an iterator (has __iter__). Iterator: object that produces items via _

Q3. How do you create an iterator from an iterable?


Ans: Use iter(): numbers = [1,2,3]; it = iter(numbers).

Q4. How do you get the next value from an iterator?


Ans: Use next(it). When exhausted, raises StopIteration.

Q5. What happens when an iterator is exhausted?


Ans: Further calls to next() raise StopIteration.

Q6. How does for loop use iterators internally?


Ans: for loop calls iter() on the object, then repeatedly calls next() until StopIteration.

Q7. What is the __iter__ method used for?


Ans: It returns the iterator object itself, allowing use in for-loops and comprehensions.

Q8. What is the __next__ method used for?


Ans: It returns the next item or raises StopIteration when no more items remain.

Q9. Can an iterator be reset?


Ans: No. To iterate again, you must create a new iterator from the iterable.

Q10. What is the difference between list and iterator?


Ans: List stores all elements in memory; iterator produces elements on demand without storing them all.

Q11. How do you build a custom iterator?


Ans: Define a class with __iter__(self) returning self and __next__(self) yielding next values.

Q12. Give an example of a custom iterator that generates squares.


Ans: class Squares:
def __init__(self, n): self.i, self.n = 0,n
def __iter__(self): return self
def __next__(self):
if self.i<self.n: val=self.i**2; self.i+=1; return val
raise StopIteration

Q13. What are advantages of iterators?


Ans: Memory efficiency, lazy evaluation, infinite sequences possible.

Q14. What is itertools module?


Ans: A standard library providing advanced iterator building blocks like count, cycle, chain, islice.

Q15. What is itertools.count?


Ans: An infinite iterator yielding consecutive integers: itertools.count(start=0, step=1).

Q16. What is itertools.cycle?


Ans: Repeats elements of an iterable indefinitely.

Q17. What is itertools.islice?


Ans: Slices an iterator: islice(iterable, start, stop, step).

Q18. What is itertools.chain?


Ans: Chains multiple iterables into a single iterator.

Q19. What is the difference between iter() and __iter__()?


Ans: iter(obj) calls obj.__iter__() under the hood.

Q20. Why are iterators important in Python?


Ans: They unify iteration, enable lazy evaluation, and support efficient looping in large/streaming data.
Generators
Q21. What is a generator in Python?
Ans: A generator is a function that yields values one at a time using 'yield', producing an iterator automatically.

Q22. How is a generator different from a normal function?


Ans: Normal function uses return and ends; generator uses yield and resumes state on next call.

Q23. What does 'yield' keyword do?


Ans: It pauses the function, returns a value, and can resume from that point later.

Q24. What is a generator expression?


Ans: A concise way to create generators, like (x*x for x in range(5)).

Q25. What is the difference between list comprehension and generator expression?
Ans: List comprehension builds list in memory; generator expression produces values lazily.

Q26. How to create a simple generator?


Ans: def gen():
yield 1
yield 2
yield 3

Q27. What happens when generator is exhausted?


Ans: Further calls raise StopIteration.

Q28. How do you get values from a generator?


Ans: Use next(gen) or iterate with for loop.

Q29. Can a generator be reused?


Ans: No, once exhausted it cannot be restarted. Must call function again to get new generator.

Q30. What is the advantage of generators?


Ans: Memory efficient, fast, suitable for large datasets or infinite sequences.

Q31. What is send() in generators?


Ans: Allows sending a value back into generator to replace yield expression result.

Q32. What is throw() in generators?


Ans: Raises an exception inside the generator at the yield point.

Q33. What is close() in generators?


Ans: Terminates the generator, raising GeneratorExit inside.

Q34. What are generator pipelines?


Ans: Combining multiple generators to process data in stages.

Q35. Give example of generator for Fibonacci numbers.


Ans: def fib(n):
a,b=0,1
for _ in range(n):
yield a
a,b=b,a+b

Q36. What is difference between generator function and generator object?


Ans: Generator function contains yield and returns generator object when called. Generator object is the iterator
produced.

Q37. How are generators implemented internally?


Ans: They use frames and state machines to preserve local variables across yields.

Q38. Can generators be infinite?


Ans: Yes, e.g., def count(): i=0; while True: yield i; i+=1.

Q39. What is yield from in Python?


Ans: Delegates part of a generator to another generator or iterable, simplifying composition.

Q40. What are practical applications of generators?


Ans: Reading large files line by line, streaming data, pipelines, producer-consumer problems.

You might also like