Change language

Python | Generator expressions

|

# Python code to illustrate the generator, yield () and next ().

def generator ():

t = 1

print ’First result is’ , t

yield t

  

  t + = 1

print ’Second res ult is ’ , t

  yield t

 

t + = 1

print ’Third result is’ , t

yield t

 

call = generator ()

next (call)

next (call)

next (call)

Exit:

 First result is 1 Second result is 2 Third result is 3 

Difference between generator function and normal function —

  • When the function exits, the function is suspended and control is transferred to the caller.
  • When the function exits, StopIteration is automatically called on subsequent calls.
  • Local variables and their states are remembered between successive calls.
  • >
  • A generator function contains one or more yield statements instead of a return statement.
  • Since methods such as _next_ () and _iter_ () are implemented automatically, we can iterate over the elements using next () .

There are various other expressions that can simply be coded similar to lists, but instead of parentheses, we use parentheses ... These expressions are intended for situations where the generator is immediately used by the enable function. A generator expression allows you to create a generator without the yield keyword. However, it does not share all the power of the generator created with the yield function. Example:

# Python code to illustrate the generator expression

generator = (num * * 2 for num in range ( 10 )) 

for num in generator:

print (num)

Output:

 0 1 4 9 16 25 36 49 64 81 

We can also generate cn Source using generator expressions:

string = ’geek’

li = list (string [i] for i in range ( len (string) - 1 , - 1 , - 1 ))

print (li)

You move:

 [’k’,’ e’, ’e’,’ g’] 

This article is provided by Chinma Lenka . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method