Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mustafamuratcoskun
GitHub Repository: mustafamuratcoskun/Sifirdan-Ileri-Seviyeye-Python-Programlama
Path: blob/master/Pythondaki Iteratorlar ve Generatorlar/Videolardaki Notebooklar/Iteratorlerin Oluşturulması ve Kullanılması.ipynb
765 views
Kernel: Python 3
liste = [1,2,3,4,5]
dir(liste)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
iterator = iter(liste)
print(iterator)
<list_iterator object at 0x000000792684FA90>
next(iterator)
1
next(iterator)
2
next(iterator)
3
next(iterator)
4
next(iterator)
5
next(iterator)
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-18-3733e97f93d6> in <module>() ----> 1 next(iterator) StopIteration:
liste = [1,2,3,4,5] for i in liste: print(i)
1 2 3 4 5
iterator = iter(liste) while True: try: print(next(iterator)) except StopIteration: break
1 2 3 4 5
class Kumanda(): def __init__(self,kanal_listesi): self.kanal_listesi = kanal_listesi self.index = -1 def __iter__(self): return self def __next__(self): self.index += 1 if self.index < len(self.kanal_listesi): return self.kanal_listesi[self.index] else: self.index = -1 raise StopIteration
kumanda = Kumanda(["Atv","Trt","Fox","Kanal D","Bloomberg"])
iterator = iter(kumanda)
next(iterator)
'Atv'
next(iterator)
'Trt'
next(iterator)
'Fox'
next(iterator)
'Kanal D'
next(iterator)
'Bloomberg'
next(iterator)
--------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-56-3733e97f93d6> in <module>() ----> 1 next(iterator) <ipython-input-48-e4c21cf75a4c> in __next__(self) 11 else: 12 self.index = -1 ---> 13 raise StopIteration 14 15 StopIteration:
for i in kumanda: print(i)
Atv Trt Fox Kanal D Bloomberg