---
title: "What is the use of the \"yield\" keyword in Python?"  
description: "What is the use of the \"yield\" keyword in Python?"  
author: "Utpal Vishwas"  
published: 2023-04-11  
updated: 2023-04-17  
canonical: https://www.mindstick.com/forum/157751/what-is-the-use-of-the-yield-keyword-in-python  
category: "python"  
tags: ["python", "python 2"]  
reading_time: 1 minute  

---

# What is the use of the "yield" keyword in Python?

What is the use of the "yield" [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Aryan Kumar

The Yield keyword in Python is **similar to a return statement used for returning values or objects in Python**. However, there is a slight difference. The yield statement returns a generator object to the one who calls the function which contains yield, instead of simply returning a value.

### Reply by Krishnapriya Rajeev

The *yield* keyword is used in Python in order to create a generator function, i.e., one that can be used like an *iterator object*. In such a function, the yield keyword converts the expression following it into a generator object, which may be iterated over again and again to return the values contained inside it.

An example of a Python program implementing the yield keyword is:

```plaintext
# define the generator function
def generator_function():
     yield “Apple”
     yield “Banana”
     yield “Carrot”

generator_object = generator_function()
print(type(generator_object))

# yield the different values inside the object
print(next(generator_obj)
print(next(generator_obj)
print(next(generator_obj)

# Output = <class ‘generator’>
           Apple
           Banana
           Carrot
```


---

Original Source: https://www.mindstick.com/forum/157751/what-is-the-use-of-the-yield-keyword-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
