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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically