Iterator
Iterator is the object in python which contains some countable values.it can be run with the for and while loop over the string, list, tuple etc.
Iterator contain Two methods
- __iter__ : this method is used for the initialization of the iterator it will return the objects to the __next__.
- __next__ : it is the method which returns the next value in the iterator when it will use as next()
Example 1
>>> name=["Rohan","Sohan","Sita"] >>> myiterator=iter(name) >>> next(myiterator) 'Rohan' >>> next(myiterator) 'Sohan' >>> next(myiterator) 'Sita' >>>
Example 2
>>> web="salesforcedrillers" >>> myiter=iter(web) >>> next(myiter) 's' >>> next(myiter) 'a' >>> next(myiter) 'l' >>> next(myiter) 'e' >>> next(myiter) 's' >>> next(myiter) 'f' >>> next(myiter) 'o' >>>
Example 3
value = "salesforcedrillers" myobj= iter(value) while True: try: printing = next(myobj) print(printing,end=",") except StopIteration: break
Output
s,a,l,e,s,f,o,r,c,e,d,r,i,l,l,e,r,s,