PYTHON PROGRAMMING - Iterate
PYTHON PROGRAMMING - Iterate
PROGRAMMING
TOPIC: ITERATORS
G. MAHALAKSHMI MALINI,
ASSISTANT PROFESSOR/ECE
SCHOOL OF ENGINEERING,
AVINASHILINGAM INSTITUTE FOR HOME SCIENCE AND
HIGHER EDUCATION FOR WOMEN
ITERABLE:
– The next method returns the next value for the iterable.
numbers=[1,4,9]
value=numbers.__iter__()
item1=value.__next__()
print(item1)
item2=value.__next__()
print(item2)
item3=value.__next__()
print(item3)
OUTPUT 1
– 1
– 4
– 9
PROGRAM 2:
– iterable_value = 'Geeks'
– iterable_obj = iter(iterable_value)
–
– while True:
– try:
–
– # Iterate by calling next
– item = next(iterable_obj)
– print(item)
– except StopIteration:
–
– # exception will happen when iteration will over
– break
OUTPUT 2:
– G
– e
– e
– k
– s
THANK YOU