python iterator and generator code example
Example 1: what is iterator in python
An iterator is an object that can be iterated upon, meaning that you can traverse through all the values.
#EXAMPLE OF ITERATOR
nums=[2,3,4,5,6,7]
it=iter(nums)
print(it.__next__())
for i in it:
print(i)
Example 2: python iterator
pies = ["cherry", "apple", "pumpkin", "pecan"]
iterator = iter(pies)
print(next(iterator))
#prints "cherry" because it's the current one being iterated over