Change language

Python | give a keyword

Code # 1: Demonstration

# Python3 demo code
# yield keyword

 
# generator for printing 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 ]

 
# print starting list

print ( "The original list is:" +   str (test_list))

 
# print 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:

# Python program to generate squares from 1
# up to 100 using crop and therefore generator

 
# Infinite generator function that prints
# the next square number. Starts with 1

def nextSquare (): 

i = 1

 

# Endless loop to generate squares

  while True

  yield i *

i + = 1 # Next execution resumes

  # from now on

 
# Driver code

for num in  nextSquare (): 

if num"  100

break  

print (num) 

Exit:

 1 4 9 16 25 36 49 64 81 100 

Benefits of the crop:

  • Since it stores local state variables, hence the memory allocation overhead is controlled.
  • Since the old state is retained, the thread does not start from the beginning and therefore saves time.

Disadvantages of yield:

  • Sometimes it becomes erroneous to use yield if a function call is not handled correctly.
  • Optimizing time and memory requires a lot of code complexity and hence, it is sometimes difficult to understand the logic behind this.

Practical application :
A possible practical use is that when processing the last amount of data and finding details from it, you can use yield , since we do not need to search again from the beginning and, hence save time. There can be many profitability applications depending on the use cases.

# Python3 code to demonstrate the yield keyword

 
# Checking the number of appearances
# geeks in line

 
# generator for printing even numbers

def print_even (test_string):

  for i in test_string:

if i = =   "geeks" :

yield i

 
# initialization string

test_string = "The are many geeks around you,

  geeks are known for teaching other geeks "

  
# print 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 

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