---
title: "What are Python generators and why are they useful?"  
description: "What are Python generators and why are they useful?"  
author: "Ravi Vishwakarma"  
published: 2025-08-28  
updated: 2025-08-28  
canonical: https://www.mindstick.com/interview/34362/what-are-python-generators-and-why-are-they-useful  
category: "python"  
tags: ["python-3.4", "python", "python 2", "Python 3"]  
reading_time: 1 minute  

---

# What are Python generators and why are they useful?

## Answer:

- Generators are functions that use `yield` to return an **iterator** one value at a time.
- They are **memory-efficient** (lazy evaluation).

```python
def gen_numbers():
    for i in range(5):
        yield i

for num in gen_numbers():
    print(num)
```

## Answers

### Answer by Ravi Vishwakarma

## Answer:

- Generators are functions that use `yield` to return an **iterator** one value at a time.
- They are **memory-efficient** (lazy evaluation).

```python
def gen_numbers():
    for i in range(5):
        yield i

for num in gen_numbers():
    print(num)
```


---

Original Source: https://www.mindstick.com/interview/34362/what-are-python-generators-and-why-are-they-useful

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
