Python advanced tutorial-loop functions: range, enumerate, zip, and pythonenumerate

Source: Internet
Author: User

Python advanced tutorial-loop functions: range, enumerate, zip, and pythonenumerate

In the "loop" section, we have discussed the basic loop Syntax of Python. In this section, we will be exposed to more flexible loop methods.

Range ()

In Python, if the in after a for loop follows a sequence, the sequence elements used for each loop are not the subscript of the sequence.

We have used range () to control the for loop before. Now, we continue to develop the range Function to control the loop by Subscript:
Copy codeThe Code is as follows:
S = 'abcdefghjk'
For I in range (0, len (S), 2 ):
Print S [I]

In this example, we use len () and range () Functions and I as the subscript of the S sequence to control the loop. In the range function, the upper limit, lower limit, and step size of each loop are defined respectively. This is similar to the for loop in C.

Enumerate ()

Using the enumerate () function, you can get the subscript and element in each loop at the same time:
Copy codeThe Code is as follows:
S = 'abcdefghjk'
For (index, char) in enumerate (S ):
Print index
Print char

In fact, in each cycle of enumerate (), a value table (tuple) containing two elements is returned.

Zip ()

If you want to extract one element from multiple equi-length sequences in each cycle, you can use zip () to conveniently implement the following:
Copy codeThe Code is as follows:
Ta = [1, 2, 3]
Tb = [9, 8, 7]
Tc = ['A', 'B', 'C']
For (a, B, c) in zip (ta, tb, tc ):
Print (a, B, c)

In each loop, an element is extracted from left to right from each sequence and merged into a tuple. Then, the tuple elements are assigned to a, B, and c.

The zip () function extracts one element from multiple lists in sequence. The elements extracted each time (from different lists) are combined into a single tuple, And the merged tuples are placed in the list returned by the zip. The zip () function is used to aggregate the list.

We can break down the aggregated list as follows:

Copy codeThe Code is as follows:
Ta = [1, 2, 3]
Tb = [9, 8, 7]

# Cluster
Zipped = zip (ta, tb)
Print (zipped)

# Decompose
Na, nb = zip (* zipped)
Print (na, nb)

Summary

Range ()

Enumerate ()

Zip ()


Parameters in the range Function in python

For I in range (-1,-len (s),-1): #-1 to-(5-1), followed by-1 indicates reverse fetch, that is (-1,-2,-3,-4)
In the loop:
Print s (:-1) # obtain the last digit from 0, but does not include the last digit, abcd
Print s (:-2) # Get from 0 to the second to the last, but does not include the second to the last, abc
....

Python for Loop

L = []
For I in range (5 ):
Tmp = int (input ("Enter an integer :"))
L. append (tmp)
Print (sum (L ))

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.