Python---Iteration

Source: Internet
Author: User

#  Iteration #  If given a list or tuple, we can traverse the list or tuple through a for loop, which we call an iteration (iteration) #  in Python, the iteration is through the for  ... in, and in many languages such as C or Java, the iteration list is a high abstraction of the # python for loop done by subscript, because Python's for loop can be used not only on list or tuple, It is also possible to #  iterations on other iterative objects dict#  because Dict's storage is not arranged in list order, the resulting order of the iterations is likely to be different #  by default dict iterations key#  If you want to iterate over value, you can use For value in d.values () #  If you want to iterate both key and value at the same time, you can use the For k, v in  d.items () #  determines that an object is an iterative object by the iterable type of the collection module from collections import iterabled  = {' A ': 1,  ' B ': 2,  ' C ':  3}print (' iterative key ') for key in d:     print (key) print (' Iteration value ') for value in d.values ():     Print (value) print (' Iterative key&value ') For k, v in d.items ():     print (K,  V) #  Iteration string print (' Iteration string ') for ch in  ' ABC ':     print (CH) #  Whether Str can iterate print (isinstance (' abc ', &NBSP, iterable)) # list whether the print (Isinstance ([1, 2, 3], iterable)) #  integer can be iterated by the print (isinstance (123, iterable)) #  uses Python's built-in enumerate function to turn a list into an index-element pair that iterates through both the index and the element itself in the For loop for i, value in enumerate ([' A ',  ' B ',  ' C ']):     print (I, value)


Python---Iteration

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.