Python3 for Loop, Enumerat, range

Source: Internet
Author: User

1. For loop: The user iterates through the contents of the object sequentially

Format: For variable in object: (during traversal, assign element one by one in the object to the variable)

s = ' python ' for item in S:    print (item) Result: Python

2. Enumerate: Add serial number for the iterated object

s = ' python ' for i,v in Enumerate (s):    print (I,V) results: 0 p1 y2 t3 h4 o5 N

As shown above, each access string s an element adds a serial number to the element

3. Range: Generates the specified number within the specified range

There are three parameters: Start (start position), stop (as of position +1), step (digital spacing)

Three different forms:

1, Range (stop): Indicates the range from 0 to stop-1, the next number is the previous number plus 1 (only one parameter, the range by default from zero to the parameter stop-1, the step is the default of 1)

2, Range (Start,stop): Indicates that the range is start to stop-1, the next number is the last digit plus 1

3. Range (Start,stop,step): Indicates the range is start to stop-1, the next number is the previous digit plus step

Note: The corresponding number is created only when the number is called

Print (range) for I in range:    print (i) Results: Range (0, 10) 0123456789

The direct output range is a range (0, 10) that does not create a number, and it creates a number only after each subsequent for loop executes once.

Python3 for Loop, Enumerat, range

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.