 # Python – yield Keyword

## Python | yield Keyword

Yield is a keyword in Python that is used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. Any function that contains a yield keyword is termed a generator. Hence, yield is what makes a generator. The yield keyword in Python is less known off but has a greater utility which one can think of.
Code #1 : Demonstrating yield working

 `# Python3 code to demonstrate` `# yield keyword` `# generator to print even numbers` `def` `print_even(test_list) :` `    ``for` `i ``in` `test_list:` `        ``if` `i ``%` `2` `=``=` `0``:` `            ``yield` `i` `# initializing list` `test_list ``=` `[``1``, ``4``, ``5``, ``6``, ``7``]` `# printing initial list` `print` `(``"The original list is : "` `+`  `str``(test_list))` `# printing even numbers` `print` `(``"The even numbers in list are : "``, end ``=` `" "``)` `for` `j ``in` `print_even(test_list):` `    ``print` `(j, end ``=` `" "``)`

Output :

```The original list is : [1, 4, 5, 6, 7]
The even numbers in list are :  4 6```

Code #2:

 `# A Python program to generate squares from 1` `# to 100 using yield and therefore generator` `# An infinite generator function that prints` `# next square number. It starts with 1` `def` `nextSquare():` `    ``i ``=` `1` `    ``# An Infinite loop to generate squares` `    ``while` `True``:` `        ``yield` `i``*``i                ` `        ``i ``+``=` `1` `# Next execution resumes` `                ``# from this point    ` `# Driver code` `for` `num ``in` `nextSquare():` `    ``if` `num > ``100``:` `        ``break` `    ``print``(num)`

Output:

```1
4
9
16
25
36
49
64
81
100```

• Since it stores the local variable states, hence overhead of memory allocation is controlled.
• Since the old state is retained, the flow doesn’t start from the beginning and hence saves time.

• Sometimes, the use of yield becomes erroneous if the calling of function is not handled properly.
• Time and memory optimization has a cost of complexity of code and hence sometimes hard to understand the logic behind it.

Practical Applications:
The possible practical application is that when handling the last amount of data and searching particulars from it, yield can be used as we don’t need to look up again from start and hence would save time. There can possibly be many applications of yield depending upon the use cases.

 `# Python3 code to demonstrate yield keyword` `# Checking number of occurrence of` `# geeks in string` `# generator to print even numbers` `def` `print_even(test_string) :` `    ``for` `i ``in` `test_string:` `        ``if` `i ``=``=` `"geeks"``:` `            ``yield` `i` `# initializing string` `test_string ``=` `" The are many geeks around you, \` `              ``geeks are known ``for` `teaching other geeks"` `# printing even numbers using yield` `count ``=` `0` `print` `(``"The number of geeks in string is : "``, end ``=` `"" )` `test_string ``=` `test_string.split()` `for` `j ``in` `print_even(test_string):` `    ``count ``=` `count ``+` `1` `print` `(count)`

Output :

`The number of geeks in string is : 3`

Last Updated on November 13, 2021 by admin

## Type Conversion in PythonType Conversion in Python

Type Conversion in Python Python defines type conversion functions to directly convert one data type

## Python program to convert list to stringPython program to convert list to string

Given a list, write a Python program to convert the given list to string. There

## Find average of a list in pythonFind average of a list in python

Find average of a list in python Given a list of numbers, the task is

## Pandas Series.str.extract()Pandas Series.str.extract()

Pandas Series.str.extract() In Python’s Pandas library, Series.str.extract() function enables you to extract specific parts of

## Accessing Attributes and Methods in PythonAccessing Attributes and Methods in Python

Accessing Attributes and Methods in Python Attributes of a class are function objects that define

## Get() method for dictionaries in PythonGet() method for dictionaries in Python

Get() method for dictionaries in Python Python get() method return the value for the given key

## Context Manager in PythonContext Manager in Python

Context Manager in Python Managing Resources : In any programming language, the usage of resources like