Python Iterator
---------------------
An iterator is an object that contains a countable number of values.
An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.
Iterator Methods:
---------------------
1. iter() - used to get an iterator
2. next()- next() function to manually iterate through all the items of an iterator
~~~~~~~~~~~~~~~~~~~~~~~~
#Program 1 : Example program for iterator
a=[10,20,5,7,90]
i=iter(a)
print(next(i))
print(next(i))
print(i.__next__())
print(i.__next__())
print(i.__next__())
print(next(i))
~~~~~~~~~~~~~~~~~~~~~~~~
#Program 2 : Example program for iterator
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)
print(next(myit))
print(next(myit))
print(next(myit))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 3 : Example program for iterator
mystr = "jahab"
myit = iter(mystr)
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
No comments:
Post a Comment