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



Download 2,61 Mb.
bet65/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   61   62   63   64   65   66   67   68   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

While vs. For


I’ve explained a few times now why we would use each loop; however, it’s always good to reiterate concepts. For loops are generally used when you need to count or iterate over a collection of elements. While loops are generally used when doing condition-based looping. When using a while loop, often you’ll use boolean variables. Each loop has their use cases; in most cases it’s personal preference, but the general rule of thumb is counting with for loops, conditions with while loops.
Note the pass, break, and continue statements all work the same way for while loops as well.

Infinite Loops


In a previous cell, I mentioned that infinite loops were bad. An infinite loop will continue to run until the program breaks, the computer is shut down, or until time stops. Knowing this, stay away from creating infinite loops. Here is an example of an infinite loop:
ChApter 4 LIsts And Loops
>>> game_over = False >>> while not game_over:
>>> print(game_over)
If you were to run this within a cell, eventually you would have to shut down Jupyter Notebook and restart it (or at least the kernel). This is because the game_over variable never becomes True, and the condition is running until game_over becomes True. Always make sure you have a way to exit your loops, whether it be by a break or by a condition.

Nested Loops


The concept of a loop within a loop is what we call a nested loop. The concepts of a loop still apply. When using nested loops, the inner loop must always finish running, before the outer loop can continue. Let’s see an example:

# using two or more loops together is called a nested loop for i in range(2): # outside loop for j in range(3): # inside loop print( i, j )

Go ahead and run that cell. At first, this may seem a bit confusing, since there’s a lot going on here. Let’s break the output down with Table 4-4.
Table 4-4. Tracking nested loop values


Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   61   62   63   64   65   66   67   68   ...   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