forked from Dexter-99/Hacktober-Fest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIterator.py
26 lines (25 loc) · 901 Bytes
/
Iterator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
num=[7,8,9,5]
# print(num[0]) #1st way of printing elements of list
# for i in num:
# print(i) #2nd way of printing elements of list
# #The other way of printing the values of a list is iterator.
# #Behind the scenes,for loop also uses iterator.
# it=iter(num)
# # __iter__() converts list into iterator
# print(next(it)) #first it prints 7
# print(next(it))#then 8 and so on
class Top10:
def __init__(self):
self.num=1
def __iter__(self): #iter gives object of iterator
return self
def __next__(self):
if self.num<=10:
val=self.num
self.num+=1
return val
else:
raise StopIteration #to stop for loop raise an exception
values=Top10()
for i in values:
print(i)