Don't let the Lockdown slow you Down - Enroll Now and Get 3 Course at 24,999/- Only. Explore Now!

General

Python Generators

Python Generators

Introduction

A generator python function is defined as creating iterators as a normal function, with the yield keyword rather than using the return keyword. def which contains yield, the function is automatically becoming a generator function.

Python generator can be done through a keyword called yield which is because of multiple returns and multiple values accessing by using in def function. All the values are stored in stacks and retrieve one by one value. Each value is accessing through the function called next and iter function. These two functions are the most common retrieving functions of python generator.

The next function which is used to call by an object of python generator, which will call one by one value once the value is removed from the stacks it will through the error. Even we can be done through the iter function this will loop the value of the generator one by one done through looping.

For example:

>>def calculator(a,b)

yield (a+b)

yield (a-b)

yield (a*b)

yield (a/b)

The above example program explains the multiple yield functions for calculating the two values and the functions which are done for addition, subtraction, multiplication, and division .so that it will have four yield functions which will return the four values. The first output is shown below.

Output-1:

>>>x=calculator(5,10)

>>x.next()

15

This Output will provide the calculator of a+b which will only retain the value of adding value no other outputs are displayed. the second output is displayed below.

Output-2

x=calculator
>>>x.next()
15
>>>x.next()
-5

This Output will provide the calculator of a-b which will only retain the value of subtraction value no other outputs are displayed. the third output is displayed below.

Output-3

>>x=calculator(5,10)
>>x.next()
15
>>>x.next()
-5
>>>x.next()
50

This Output will provide the calculator of a*b which will only retain the value of multiplication value no other outputs are displayed. the Fourth output is displayed below.

Output-4

>x=calculator(5,10)
>>x.next()
15
>>>x.next()
-5
>>>x.next()
50
>>x.next()
0

This Output will provide the calculator of a/b which will only retain the value of adding value no other outputs are displayed. the final output which is done by a loop of value and it shows an example is displayed below.

For Example:

>>>def calculator(a,b)

yield (a+b)

yield (a-b)

yield (a*b)

yield (a/b)

Output

for i in calculator(5,10)

print i

15

-5

50

0

Even we can loop the generator Function without using next(), By loop which will call iterate.each of the values are retrieve the values one by one. Looping can even be done the iter function.

Related Blogs:

  1. Brief Overview of Python Language
  2. Python Career opportunities
  3. Python Break Continue
  4. Python Control Flow
  5. Python Data Types
  6. Python Dictionary
  7. Python Exception Handling
  8.  Python File
  9. Python Functions
  10. Python Substring
  11. Hash Table and Hash Map in Python
Scroll Up
Besant Technologies WhatsApp