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



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

Creating a Range Generator


Although the range function is not a generator, we can make our own version that’s created from a generator using the yield keyword. Let’s try it out:

1| # creating our own range generator with start, stop, and step parameters 3| def myRange(stop, start=0, step=1):
4| while start < stop:
5| print( "Generator Start Value: { }".format(start) )
6| yield start
7| start += step # increment start, otherwise infinite loop 9| for x in myRange(5):
10| print( "For Loop X Value: { }".format(x) )

Go ahead and run the cell. The two print statements are used to show you when the generator myRange is accessed, compared to when the for loop outputs the result. We’re able to call myRange like we would a normal range function because of the way that generators operate. On line 3 we declare the function like we would any other, accepting the same arguments as range would. We begin a while loop within the function on line 4 that will yield back the start value. Once the information is yielded back to the for loop, it’s able to use that value for the current iteration. Once the for loop completes its code block, it returns to the generator as the while loop condition has not been met. Normally, once a function has returned information, it is not called upon again; however, generators continue to return and store information until their condition is met. If we didn’t increment the start value with step, we would create an infinite loop. Generators, like iterators, can be useful when you need a specific sequence for iterating. Generators are useful when you need to be memory aware. Although they are not as efficient when it comes to performance, they are memory efficient when storing information. They’re useful in situations when you need to create a data pipeline, which is when you need to perform a set of executions on pieces of data.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   149   150   151   152   153   154   155   156   ...   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