while loops
7ere
7eres one fina thing e !an %o s one fina thing e !an %o ith !on%itions: &se them to %etermine hen toith !on%itions: &se them to %etermine hen to eit a oop. 'n !hapter B e
eit a oop. 'n !hapter B e earne% abo&t oops thatearne% abo&t oops that iterate overiterate overa !oe!tion ofa !oe!tion of items i-e a ist, a string or a fie=. #ython has another type of oop !ae% a
items i-e a ist, a string or a fie=. #ython has another type of oop !ae% a hilehile oop. ather than r&nning a set n&mber of times, a
oop. ather than r&nning a set n&mber of times, a hilehile oop r&ns &nti some oop r&ns &nti some !on%ition is met. For eampe, here
!on%ition is met. For eampe, heres a bit of s a bit of !o%e that in!rements a !o%e that in!rements a !o&nt variabe!o&nt variabe by one ea!h time ro&n% the oop, stopping hen the !o&nt variabe rea!hes ten: by one ea!h time ro&n% the oop, stopping hen the !o&nt variabe 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 poerf&poerf&11,,hile oops are &se% m&!h esshile oops are &se% m&!h ess fre<&enty than in
fre<&enty than in other ang&ages, so e ont %is!&ss them f&rther.other ang&ages, so e ont %is!&ss them f&rther.
8uil"ing up comple! con"itions
8uil"ing up comple! con"itions
hat if e ante% to epress a !on%ition that as ma%e &p
hat if e ante% to epress 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 ony the onesa!!essions an% print o&t ony the ones that start ith >a>
that start ith >a> an% en% ith >3>an% en% ith >3>. . e !o&% &se to neste% if stateme !o&% &se to neste% if statements:ents:
accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for accession in accs!
for accession in accs!
if accession.startsith(a#! if accession.startsith(a#! if accession.endsith(3#! if accession.endsith(3#! print(accession# print(accession#
b&t this brings
b&t this brings in an etra, &nnee%e% eve of in%ention. 4 better ay is to in an etra, &nnee%e% eve of in%ention. 4 better ay is to 9oin &p9oin &p the to !on%ition ith
the to !on%ition ith andand to ma-e to ma-e a !ompe epression:a !ompe epression:
1
1
122@@ CChhaapptteerrGG:: CCoonn%%iittiioonnaatteessttss
accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for accession in accs!
for accession in accs!
if accession.startsith(a# and accession.endsith(3#! if accession.startsith(a# and accession.endsith(3#!
print(accession# print(accession#
$his version is ni!er in to ays: it %oesnt re<&ire
$his version is ni!er in to ays: it %oesnt re<&ire the etra eve of in%entation,the etra 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 aso &see !an aso &se oror to 9oin &p to to 9oin &p to !on%itions,
!on%itions, to pro%&!e a !ompe !on%ition that i be tr&e to pro%&!e a !ompe !on%ition that i be tr&e if either of if either of the tothe to simpe !on%itions are tr&e:
simpe !on%itions are tr&e:
accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for accession in accs!
for accession in accs!
if accession.startsith(a# or accession.startsith(-#! if accession.startsith(a# or accession.startsith(-#!
print(accession# print(accession#
e !an even 9oin &p !ompe !on%itions to ma-e more !ompe !on%itions 5 heres e !an even 9oin &p !ompe !on%itions to ma-e more !ompe !on%itions 5 heres an eampe hi!h prints a!!essions if they start ith either >a> or >b> an% en% ith an eampe hi!h prints a!!essions if they start ith either >a> or >b> an% en% ith >B>:
>B>:
accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for acc in accs!
for acc in accs!
if (acc.startsith(a# or acc.startsith(-## and acc.endsith('#! if (acc.startsith(a# or acc.startsith(-## and acc.endsith('#!
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 eampe to avoi%the above eampe to avoi%
ambig&ity. Finay, e !an negate any type of !on%ition by prefiing it ith the ambig&ity. Finay, e !an negate any type of !on%ition by prefiing it ith the or%
or% notnot. $his eampe i print o&t a!!essions that start ith >a> an%. $his eampe i print o&t a!!essions that start ith >a> an% don-tdon-t en% en% ith G:
ith G:
accs B $a-", -hG', h7", ayJ3, apJ7, -d72 accs B $a-", -hG', h7", ayJ3, apJ7, -d72 for acc in accs!
for acc in accs!
if acc.startsith(a# and not acc.endsith("#! if acc.startsith(a# and not acc.endsith("#!
print(acc# print(acc#
1
13300 CChhaapptteerrGG:: CCoonn%%iittiioonnaatteessttss
(y &sing a
(y &sing a !ombination of!ombination of andand,, oror an% an% notnot aong ith parentheses here aong ith parentheses here ne!essary= e !an
ne!essary= e !an b&i% &p arbitrariy !ompe !on%itions.b&i% &p arbitrariy !ompe !on%itions. $hese three
$hese three or%s are !oe!tivey -non asor%s are !oe!tivey -non asboolean operatorsboolean operators an% !rop &p in a an% !rop &p in a otot of pa!es. For eampe, if yo& ante% to sear!h for information on &sing #ython in of pa!es. For eampe, if yo& ante% to sear!h for information on &sing #ython in bioogy
bioogy, b&t %i%nt ant to see , b&t %i%nt ant to see pages that ta-e% abo&t bioogy of pages that ta-e% abo&t bioogy 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&ay a !ompe !on%ition 9&st i-e>. $his is a!t&ay a !ompe !on%ition 9&st i-e the ones above 5 Rooge a&tomati!ay a%%s
the ones above 5 Rooge a&tomati!ay a%%s an" an" beteen or%s, an% &ses the beteen 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" bioogy bioogy b&t
b&t not not sna-es. sna-es.