**What is “yield” in Python**

The `yield`

keyword in Python is a unique beast.

It’s similar to the `return`

statement we use for returning values in Python, but with a twist.

**While return gives back a value and forgets all about it, yield produces a value and pauses its function, waiting for the next call.**

This makes `yield`

an essential tool in creating Python’s generators – special functions that produce a sequence of results instead of a single value.

**Generator**: A type of iterable, like lists or tuples, but does not allow indexing with arbitrary indices, instead, it generates values on the fly.**Iterable**: An object capable of returning its members one at a time, allowing it to be iterated over in a for-loop.

Imagine you’re reading a book, and every time you finish a chapter, you place a bookmark before moving on to the next one.

**The yield keyword is like that bookmark, allowing you to pick up where you left off.**

```
def simple_generator():
yield 1
yield 2
yield 3
for value in simple_generator():
print(value)
```

This will output:

```
1
2
3
```

In this example, `simple_generator`

is a generator function because it uses the `yield`

keyword.

**When we loop over simple_generator(), it yields the numbers 1, 2, and 3, one at a time.**

**Difference from “return”**

The `yield`

keyword is often compared to `return`

because they both produce a value.

However, there’s a key difference.

**When a function hits a return statement, it completely exits the function and forgets its local variables.**

**On the other hand, yield pauses the function and saves its state for resumption later.**

Consider these two functions:

```
def return_function():
return 1
return 2
return 3
def yield_function():
yield 1
yield 2
yield 3
```

**If you call return_function(), it will only ever return 1.**

The function exits at the first `return`

statement it encounters.

**However, if you call yield_function() in a loop, it will yield 1, then 2, then 3.**

The `yield`

keyword allows the function to remember where it left off between calls.

**Examples of “yield”**

Let’s look at a more complex example of `yield`

in action.

Suppose we want to create a generator that yields the Fibonacci sequence, which is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.

- Fibonacci Sequence: A series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.

```
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Create a generator
fib = fibonacci()
# Print the first 10 numbers in the Fibonacci sequence
for i in range(10):
print(next(fib))
```

This script will output the first 10 numbers in the Fibonacci sequence:

```
0
1
1
2
3
5
8
13
21
34
```

**In this example, fibonacci is a generator function that yields the next number in the Fibonacci sequence each time it’s called.**

We use the `next()`

function to get the next value from the generator.