Python iterator (2)--Implement for loop

Source: Internet
Author: User

Python's for loop is essentially implemented by constantly invoking the next () function, for example:

1 #_*_coding:utf-8_*_2 #__author__ = "Csy"3D = ["Coffee Berry Flavor Latte","green tea star Ice Music","Cocoa Shard star Ice Music","Caramel Macchiato","Classic Chocolate","Coffee, Smith .","Hot Milk","Oriental Beauty","Ice Mocha","Cappuccino","American Coffee","Double fruit Juice"]4A = ITER ([0,1,2,3,4,5,6,8,9,10,11])5  whileTrue:6     Try:7x =Next (a)8         Print(D[x])9     exceptstopiteration:Ten          Break

Output:

Coffee Berry Flavor Latte
Green Tea star Ice Music
Cocoa Shard Star Ice Music
Caramel Macchiato
Classic Chocolate
Coffee, Smith.
Hot milk
Ice Mocha Card
Cappuccino
American Coffee
Double fruit juice

Equivalent:

1 #_*_coding:utf-8_*_2 #__author__ = "Csy"3D = ["Coffee Berry Flavor Latte","green tea star Ice Music","Cocoa Shard star Ice Music","Caramel Macchiato","Classic Chocolate","Coffee, Smith .","Hot Milk","Oriental Beauty","Ice Mocha","Cappuccino","American Coffee","Double fruit Juice"]4  forXinchRange (0,12):5     Print(D[x])

Python iterator (2)--Implement for loop

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.