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



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

CHALLENGE QUESTION


As a programmer you must think about the time it takes to execute a program. Even a program that will give you 100% accurate answers can be useless if it doesn’t give the answer to you in time. Without looking it up, do you think lists or dictionaries are more efficient when needing to retrieve and store information?

Monday: Generators and Iterators
In previous sections of this book, you may have seen the words generators or iterators mentioned. Without knowing, you’ve been using them the entire time. Today, we’ll dive into what each of these concepts are and how to use them.
To follow along with the content for today, let’s open up Jupyter Notebook from our
python_bootcamp” folder. Once it’s open, create a new file, and rename it to “Week_09.” Next, make the first cell markdown that has a header saying: “Generators and Iterators.” We’ll begin working underneath that cell.

Iterators vs. Iterables


An iterator is an object that contains items which can be iterated upon, meaning you can traverse through all values. An iterable is a collection like lists, dictionaries, tuples, and sets. The major difference is that iterables are not iterators; rather they are containers for data. In Python, iterator objects implement the magic methods iter and next that allow you to traverse through its values.

Creating a Basic Iterator


We can create iterators easily from iterables. You can simply use the iter() function to do so:

1| # creating a basic iterator from an iterable
3| sports = [ "baseball", "soccer", "football", "hockey", "basketball" ]
5| my_iter = iter(sports)
7| print( next(my_iter) ) # outputs first item
8| print( next(my_iter) ) # outputs second item 10| for item in my_iter:
11| print(item)
13| print( next(my_iter) ) # will produce error

Go ahead and run the cell. Iterators will always remember the last item that they returned, which is why we get an error on line 13. Using the next() method, we’re able to output the next item within the iterator. Once all the items within the iterator have been used, however, we can no longer traverse through the iterator, as there are no more items left. Iterators are great for looping as well, and like lists and dictionaries, we can simply use the in keyword (see line 10). You can still loop over the list like we normally do, and it will always begin from index 0, but once our iterator is out of items, we can no longer use it.

Download 2,61 Mb.

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