• No results found

while loopswhile loops

In document python for biologists (Page 137-139)

while loops

7ere

7eres one fina thing e !an %o s one fina thing e !an %o ith !on%itions: &se them to %etermine hen toith !on%itions: &se them to %etermine hen to eit a oop. 'n !hapter B e

eit a oop. 'n !hapter B e earne% abo&t oops thatearne% abo&t oops that iterate overiterate overa !oe!tion ofa !oe!tion of items i-e a ist, a string or a fie=. #ython has another type of oop !ae% a

items i-e a ist, a string or a fie=. #ython has another type of oop !ae% a hilehile oop. ather than r&nning a set n&mber of times, a

oop. ather than r&nning a set n&mber of times, a hilehile oop r&ns &nti some oop r&ns &nti some !on%ition is met. For eampe, here

!on%ition is met. For eampe, heres a bit of s a bit of !o%e that in!rements a !o%e that in!rements a !o&nt variabe!o&nt variabe by one ea!h time ro&n% the oop, stopping hen the !o&nt variabe rea!hes ten: by one ea!h time ro&n% the oop, stopping hen the !o&nt variabe rea!hes ten:

count B 0 count B 0 hile count;10! hile count;10! print(count# print(count# count B count C 1 count B count C 1

(e!a&se norma oops in #ython are so

(e!a&se norma oops in #ython are so poerf&poerf&11,,hile oops are &se% m&!h esshile oops are &se% m&!h ess fre<&enty than in

fre<&enty than in other ang&ages, so e ont %is!&ss them f&rther.other ang&ages, so e ont %is!&ss them f&rther.

 8uil"ing up comple! con"itions

 8uil"ing up comple! con"itions

hat if e ante% to epress a !on%ition that as ma%e &p

hat if e ante% to epress a !on%ition that as ma%e &p of severa partsEof severa partsE 'magine e ant to go thro&gh o&r ist of

'magine e ant to go thro&gh o&r ist of a!!essions an% print o&t ony the onesa!!essions an% print o&t ony the ones that start ith >a>

that start ith >a> an% en% ith >3>an% en% ith >3>. . e !o&% &se to neste% if stateme !o&% &se to neste% if statements:ents:

accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for accession in accs!

for accession in accs!

if accession.startsith(a#! if accession.startsith(a#! if accession.endsith(3#! if accession.endsith(3#! print(accession# print(accession#

b&t this brings

b&t this brings in an etra, &nnee%e% eve of in%ention. 4 better ay is to in an etra, &nnee%e% eve of in%ention. 4 better ay is to 9oin &p9oin &p the to !on%ition ith

the to !on%ition ith andand to ma-e  to ma-e a !ompe epression:a !ompe epression:

1

1

122@@ CChhaapptteerrGG:: CCoonn%%iittiioonnaatteessttss

accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for accession in accs!

for accession in accs!

if accession.startsith(a# and accession.endsith(3#! if accession.startsith(a# and accession.endsith(3#!

print(accession# print(accession#

$his version is ni!er in to ays: it %oesnt re<&ire

$his version is ni!er in to ays: it %oesnt re<&ire the etra eve of in%entation,the etra eve of in%entation, an% the !on%ition rea%s in a v

an% the !on%ition rea%s in a very nat&raery nat&ra ay.  ay. e !an aso &see !an aso &se oror to 9oin &p to to 9oin &p to !on%itions,

!on%itions, to pro%&!e a !ompe !on%ition that i be tr&e to pro%&!e a !ompe !on%ition that i be tr&e if either of if either of the tothe to simpe !on%itions are tr&e:

simpe !on%itions are tr&e:

accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for accession in accs!

for accession in accs!

if accession.startsith(a# or accession.startsith(-#! if accession.startsith(a# or accession.startsith(-#!

print(accession# print(accession#

e !an even 9oin &p !ompe !on%itions to ma-e more !ompe !on%itions 5 heres e !an even 9oin &p !ompe !on%itions to ma-e more !ompe !on%itions 5 heres an eampe hi!h prints a!!essions if they start ith either >a> or >b> an% en% ith an eampe hi!h prints a!!essions if they start ith either >a> or >b> an% en% ith >B>:

>B>:

accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for acc in accs!

for acc in accs!

if (acc.startsith(a# or acc.startsith(-## and acc.endsith('#! if (acc.startsith(a# or acc.startsith(-## and acc.endsith('#!

print(acc# print(acc#

;oti!e ho e have to in!&%e parentheses in

;oti!e ho e have to in!&%e parentheses in the above eampe to avoi%the above eampe to avoi%

ambig&ity. Finay, e !an negate any type of !on%ition by prefiing it ith the ambig&ity. Finay, e !an negate any type of !on%ition by prefiing it ith the  or%

 or% notnot. $his eampe i print o&t a!!essions that start ith >a> an%. $his eampe i print o&t a!!essions that start ith >a> an% don-tdon-t en% en%  ith G:

 ith G:

accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for acc in accs!

for acc in accs!

if acc.startsith(a# and not acc.endsith("#! if acc.startsith(a# and not acc.endsith("#!

print(acc# print(acc#

1

13300 CChhaapptteerrGG:: CCoonn%%iittiioonnaatteessttss

(y &sing a

(y &sing a !ombination of!ombination of andand,, oror an% an% notnot aong ith parentheses here aong ith parentheses here ne!essary= e !an

ne!essary= e !an b&i% &p arbitrariy !ompe !on%itions.b&i% &p arbitrariy !ompe !on%itions. $hese three

$hese three or%s are !oe!tivey -non asor%s are !oe!tivey -non asboolean operatorsboolean operators an% !rop &p in a  an% !rop &p in a otot of pa!es. For eampe, if yo& ante% to sear!h for information on &sing #ython in of pa!es. For eampe, if yo& ante% to sear!h for information on &sing #ython in bioogy

bioogy, b&t %i%nt ant to see , b&t %i%nt ant to see pages that ta-e% abo&t bioogy of pages that ta-e% abo&t bioogy of sna-es, ysna-es, yo& mighto& might %o a sear!h

%o a sear!h for >for >biology python Bsnakebiology python Bsnake>. $his is a!t&ay a !ompe !on%ition 9&st i-e>. $his is a!t&ay a !ompe !on%ition 9&st i-e the ones above 5 Rooge a&tomati!ay a%%s

the ones above 5 Rooge a&tomati!ay a%%s an" an"  beteen or%s, an% &ses the beteen or%s, an% &ses the hyphen to mean

hyphen to mean not not . "o yo&. "o yo&re as-ing for pages re as-ing for pages that mention pythonthat mention python an" an"  bioogy bioogy b&t

b&t not not  sna-es. sna-es.

In document python for biologists (Page 137-139)