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



Download 2,61 Mb.
bet61/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   57   58   59   60   61   62   63   64   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

How Loops Work


Loops are how programmers rerun the same lines of code several times. Loops will always run until a condition is met. Take a first-person shooter, the game will continue to run until either you’ve won, or your health reaches zero. Once either of those conditions occur, the game ends.
Note It’s always important to condense your code down to as few lines as possible, as it is more efficient for the program.
Whether you know it or not, loops are everywhere in life. Every day we wake up, go to work, and go to bed, we know it as a routine, but it’s simply a loop. We repeat the same process each day until we reach the weekend. The same concept is applied to the loops in our programs.

Writing a For Loop


For loops are primarily used to loop a set number of times. Take Figure 4-1, for instance, this syntax suggests that the loop will run five times. Let’s break this down further. Every for loop begins with the keyword “for”. Then you define a temporary variable, sometimes known as a counter or index. Next is the “in” keyword, followed by the range function (which will be explained later). Lastly, we have a colon to end the statement. All for loops will follow this exact structure of keyword, variable, keyword, function, and colon.
ChApter 4 LIsts And Loops

Figure 4-1. For loop syntax
Now that we’ve talked about the structure of writing a for loop, let’s write one:

# writing your first for loop using range for num in range(5): print( "Value: { }".format(num) )

Go ahead and run that cell. This will output “0, 1, 2, 3, 4” for our values. This loop is essentially counting to five and printing out each number. So how does it print out each number? When the for loop is created, the range function begins at zero by default and assigns the value of zero into our temporary variable num. Each time through the loop is what we call an iteration. For each iteration, once all the code within the block runs, the current iteration is finished, and the loop starts over again at the top. Except this time, it increments the value of num, which by default is 1. Our temporary variable is assigned the value of 1 and continues to run the lines of code inside the for loop, which is simply printing out the value of num. It will continue to do this until we reach the number 5. To give you an idea of the values assigned for each iteration, reference Table 4-2.
Table 4-2. Values assigned for each iteration using range( )


Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   57   58   59   60   61   62   63   64   ...   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