-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_iterator.py
More file actions
79 lines (58 loc) · 1.66 KB
/
python_iterator.py
File metadata and controls
79 lines (58 loc) · 1.66 KB
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
# Hello World program in Python
print("Example of Python Iterators")
# An iterator is an object that contains a countable number of values.
# Lists, tuples, dictionaries, and sets are all iterable objects.
# They are iterable containers which you can get an iterator from.
# Return a iterator from a tuple, and print each value:
mytuple = ("India", "Africa", "Japan")
myiter = iter(mytuple)
print(next(myiter))
print(next(myiter))
print(next(myiter))
# Strings are also iterable objects, containing a sequence of characters:
mystr = "Africa"
myiter = iter(mystr)
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
# Looping Through an Iterator
for x in mytuple:
print(x)
print("Create an iterator")
# Create an iterator that returns numbers, starting with 1, and each sequence will increase by one (returning 1,2,3,4,5 etc.):
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))
print(next(myiter))
print("StopIteration")
# The example above would continue forever if you had enough next() statements, or if it was used in a for loop.
class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
if self.a <= 10:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass = MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)