Types of iterations: List,tuple,dict,str,bytes,bytearray, etc.
How to determine whether an object can be iterated
1>>> fromCollectionsImportiterable2>>> Isinstance ([1, 2, 3], iterable)3 True4>>> isinstance (1, 2, 3), iterable)5 True6>>> Isinstance ({' One': 1}, Iterable)7 True8>>> Isinstance ('ABC', iterable)9 TrueTen>>> Isinstance (b'ABC', iterable) One True A>>> Isinstance (ByteArray ('ABC', encoding='Utf-8'), iterable) -True
Second, the iteration of Dict
1>>> d = {' One': 1,' Both': 2}2>>> forKeyinchD:3...Print(Key)4 ... 5 One6 Both7>>> forValueinchd.values ():8...Print(value)9 ... Ten1 One2 A>>> forIteminchD.items (): -...Print(item) - ... the(' One', 1) -(' Both', 2)
Iii. list Iteration Indexes and elements--using Python's built-in enumerate
functions to turn a list into an index-element pair
1 >>> L = [1, 2, 3] 2 >>> e = Enumerate (l) 3 >>>< c5> e 4 <enumerate object at 0x101694558> 5 for in e: 6
... Print (index, value) 7 8 0 1 9 1 2 2 3
Iv. iterating over multiple variables
1 for inch [(1, 1), (2, 4), (3, 9)]: 2 ... Print (x, y) 3 4 1 15 2 46 3 9
Python Iteration Iteration