What is the use of yield?
This article will explain in detail what the usage of yield is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Let's take a look at the exercises related to the two generators, mainly to practice the use of yield.
Question 1
Enter a number n, write a program to determine the even number between 0 and n, and separate the output with a comma.
For example, enter: 10
Output result: 0, 2, 4, 6, 8, 10.
Topic analysis
We first need a loop to find all the numbers up to 10, and then determine if they are even. The topic requires the way of generator, we only need to write a generator function, after finding the qualified number, use yield output. Finally, loop the generator directly, find the desired values and put them in the list, and then separate the output with a comma.
Referenc
Def EvenGenerator (n):
I = 0
While i