


First we import from a small program, each specify a list, and find the prime numbers in it, we will write like this
import math def is_Prims(number): if number == 2: return True //除2以外的所有偶数都不是素数 elif number % 2 == 0: return False //如果一个数能被除1和本身之外的数整除,则为合数。其实我们的判定范围到根号n就可以 for cur in range(2,int(math.sqrt(number))+1,2): if number % cur == 0: return False else: return True def get_Prims(input_list): result_list = list() for element in input_list: if is_Prims(element): result_list.append(element) return result_list aa = get_Prims([1,2,3,4,5,6,7,8,9]) print (aa)
But if we want to give a number, then list What about all the prime numbers greater than this number? We might write like this:
def get_Prims(number): if is_Prims(number): return number
But once the returnfunction ends completely after handing control to the caller, any local variablesand function work are discarded. Next A call will start from scratch again. So we can use the following writing method:
def get_Prims(number): while(True): if is_Prims(number): yield number number += 1 def get_numbers(): total = list() for next_prim in get_Prims(2): if next_prim < 100: total.append(next_prim) else: print(total) return get_numbers()
The generator function is explained below. The def code of a function contains yield, and the function automatically becomes a generator function (even if it still contains return), the generator function creates Generator (a special form of iterator, this iterator has a built-in next() method), when a value is needed, it is generated through yield instead of direct return. Therefore, unlike ordinary functions, the control right is not Not handed over.
for loop will implicitly call the next() function. The next() function is responsible for calling the next() method in the generator. At this time, the generator is responsible for returning a value to any call to next( ) method, use yield to transfer this value back, which is equivalent to the return statement.
The above is the detailed content of Detailed explanation of yield and generator example codes in python. For more information, please follow other related articles on the PHP Chinese website!

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
