Changing a list [] to () creates a generator: Generator,generator is the algorithm that is saved.
Data types that can be used for a For loop:
1. Collection type: List tuple dict set str
2, generator
An object that can directly act on a for loop is collectively known as: An Iterative object (iterable)
from Import iterable Print (Isinstance ([],iterable)) Print (Isinstance ({},iterable)) Print for in range (iterable))print(isinstance (iterable))
Results:
Truetruetruefalse
An object that can be called by Next and continually returns the next value is called an iterator (Iterator)
from Import Iterator Print (Isinstance ([],iterator)) Print (Isinstance ({},iterator)) Print for in range (Iterator))print(isinstance (Iterator))
Results:
from Import Iterator Print (Isinstance ([],iterator)) Print (Isinstance ({},iterator)) Print for in range (Iterator))print(isinstance (Iterator))
turn list
, dict
and str
wait for the Iterable
Iterator
function to be used iter()
:
#!/usr/bin/env python#-*-coding:utf-8-*- fromCollectionsImportIteratorPrint(Isinstance (iter([]), Iterator))Print(Isinstance (ITER ({}), Iterator))Print(Isinstance (x forXinchRange (10)) , Iterator))Print(Isinstance (ITER ('ABC'), Iterator))
Show Results:
Truetruetruetrue
Python builder, iterator, iterator object