Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet152/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   148   149   150   151   152   153   154   155   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Creating Our Own Iterator


Now that we’ve seen how to create an iterator from a Python iterable, let’s create our own iterator class that will output each letter in the alphabet. To create an iterator, we’ll need to implement the magic methods __iter__() and __next__():

1| # creating our own iterator 3| class Alphabet( ):
4| def __iter__(self):
5| self.letters = "abcdefghijklmnopqrstuvwxyz"
6| self.index = 0
7| return self 9| def __next__(self):
10| if self.index <= 25:
11| char = self.letters[ self.index ]
12| self.index += 1
13| return char 14| else:
15| raise StopIteration 17| for char in Alphabet( ):
18| print(char)

Go ahead and run the cell. The output results in the entire alphabet being printed one letter at a time. We begin by creating an iterator with the name “Alphabet.” We then use the iter method to declare the attributes associated with this iterator. Think of the iter method as the initialization method for iterators. At the end of the iter method, you must always return self. The next method is declared so that when called upon, the iterator can return the next character in the string of letters. We stored an attribute called index in order to track which item was supposed to be returned next. Lastly, we added a condition on line 14 so that it raises a StopIteration error if it has already output all the letters. Iterators are useful when you’re in need of traversing through Python collections in a specific way.
What Are Generators?
Generators are functions that yield back information to produce a sequence of results rather than a single value. They’re a way to simplify the creation of an iterator. Normally, when a function has completed its task and returned information, the variables declared inside of the function will be deleted. With generators, however, they use the “yield” keyword to send information back to the location it was called without terminating the function. Generators don’t always have to yield back integers though you can yield any information you’d like. Let’s look at a couple examples with both numbers and single characters.
Note Generators are simplified iterators.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   148   149   150   151   152   153   154   155   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish