Python First Knowledge builder iterator

Source: Internet
Author: User

Generator

The function with yield is called generator (generator) in Python

defXragns ():#Defining function Generators    Print('little Guy')    yield('Good')#plus yield generates a generator    Print('python')    yield('Hao')    Print('Programming')    yield('Hao') x= Xragns ()#executing a function generator is just getting inside not executingret = x.__next__()#the next method of the generator will only execute the execution function to find the next yieldPrint(ret) RET= x.__next__()#the second execution goes back to where it was last time.Print(ret) RET= x.__next__()Print(ret)

Iterators

Iterators are a way to access the elements of a collection. An iterator object is accessed from the first element of the collection, knowing that all of the elements have been accessed and finished. Iterators can only go forward and not back

EF xrange (N):#Define a functionStart =0 whileTrue:#While Loop        ifStart > N:#Judging if the value is less than n            return        yieldStart#Generator ExitsStart + = 1#each cycle to this adds a forIinchXrange (10):#iterators    Print(i) obj= Xrange (5)#executes the function, and the generator does not execute internally at this timeN1 = obj.__next__()#The generator executes from top to bottom knowing the first yield encounteredN2 = obj.__next__()#the generator goes back to where it was last jumped and goes back to the loop.N3 = obj.__next__()#Next, you want to executeN4 = obj.__next__() N5= obj.__next__() N6= obj.__next__() N7= obj.__next__()Print(N1,N2,N3,N4,N5,N6)#output all the results#

Python First Knowledge builder iterator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.