-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Iterators.py
More file actions
83 lines (68 loc) · 763 Bytes
/
Python_Iterators.py
File metadata and controls
83 lines (68 loc) · 763 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#Example1
mylist=[1,2,3,4,5]
print(mylist)
x=iter(mylist)
print(next(x))
print(next(x))
print(next(x))
print(next(x))
print(next(x))
#Output1
'''
[1, 2, 3, 4, 5]
1
2
3
4
5
'''
print()
#Example2
myname="Atish Chandra"
iter_obj=iter(myname)
while True:
try:
item=next(iter_obj)
print(item)
except StopIteration:
break
#output1
'''
A
t
i
s
h
C
h
a
n
d
r
a
'''
print()
#Example3
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
x = self.a
self.a += 1
return x
myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
#output3
'''
1
2
3
4
5
'''