Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet50/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   46   47   48   49   50   51   52   53   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

The ‘

for

’ loop

In this section we will illustrate the for loop using the following list:

values = [10, -5, 3, -1, 7]

Then an example of a for loop is:

for v in values:

print(v) # block of code

What this means is that the variable v is set to each item from the list values in turn, and

every time this happens the block of code is executed. So here v is first set to 10, and the

block of code is executed, then v is set to -5, and the block of code is executed again etc.,

until finally it is set to 7 and the block of code is executed for the last time. The situation

shown for a list also applies if values is a tuple or set or dictionary. For a dictionary the

variable  v  would  be  assigned  to  the  keys  of  the  dictionary,  one  after  the  other.  Also,

remember that sets and dictionaries are not ordered so the looping will not happen in any

particular order.

Note  that  although  values  is  defined  before  the  for  statement,  the  item  variable  v  is

introduced anew at this point. It does not matter if a variable named v already exists before

the loop, but its original associated value will be overwritten. The item variable name v is



arbitrary, and any other name could be used instead. Though it would be most confusing if

it were called values …

As  with  conditional  statements,  the  block  has  to  have  at  least  one  line  of  code  in  it,

although it can naturally have more, and the colon (‘:’) at the end of the for statement is

mandatory.  As  another  example,  suppose  you  wanted  to  calculate  the  summation  of  the

items in the list values. Then you could do:

total = 0

for v in values:

total = total + v

# At the end total is 14

Following this in detail, doing total + v  each  time,  we  are  doing  the  operations  0+10,

10-5, 5+3, 8-1, 7+7, and updating s each time, so at the end total is 14. It would not matter

here whether values were a list, a tuple or a set, this loop would still result in the sum of

the items in values being calculated. Note that v is not forgotten about after the loop ends,

it holds the value of the last item it was set to, in this case 7.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   46   47   48   49   50   51   52   53   ...   514




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