'magine e ante% to ta-e o&r ist of apes:
apes B $*omo sapiens*, *Pan troglodytes*, *%orilla gorilla*
an% print o&t ea!h eement on a separate ine, i-e this:
omo sapiens is an ape Pan troglodytes is an ape %orilla gorilla is an ape
I0 ChapterB:+istsan%oops
?ne ay to %o it o&% be to 9&st print ea!h eement separatey:
print(apes$0 C * is an ape*# print(apes$1 C * is an ape*# print(apes$2 C * is an ape*#
b&t this is very repetitive an% reies on &s -noing the n&mber of eements in the ist. hat e nee% is a ay to say something aong the ines of > for each element in the list of apes0 print out the element0 followe" by the wor"s 7 is an ape7>. #ythons oop synta aos &s to epress those instr&!tions i-e this:
for ape in apes!
print(ape C * is an ape*#
+ets ta-e a moment to oo- at the %ifferent parts of this oop. e start by riting for x in y, here y is the name of the ist e ant to pro!ess an% x is the name e ant to &se for the !&rrent eement ea!h time ro&n% the oop.
x is 9&st a variabe name so it foos a the r&es that eve area%y earne% abo&t variabe names=, b&t it behaves sighty %ifferenty to a the other variabes eve
seen so far. 'n a previo&s eampes, e !reate a variabe an% store something in it, an% then the va&e of that variabe %oesnt !hange &ness e !hange it o&rseves. 'n !ontrast, hen e !reate a variabe to be &se% in a oop, e %ont set its va&e 5 the va&e of the variabe i be a&tomati!ay set to ea!h eement of the ist in t&rn,
an% it i be %ifferent ea!h time ro&n% the oop.
'mportanty, the oop variabe x ony eists insi%e the oop 5 it gets !reate% at the start of ea!h oop iteration, an% %isappears at the en%. $his means that on!e the oop has finishe% r&nning for the ast time, that variabe is gone forever. hen a variabe is restri!te% to a bo!- of !o%e i-e this, e !a it the variabes scope 5 e i see severa more eampes ater in the boo-.
I1 ChapterB:+istsan%oops
$his first ine of the oop en%s ith a !oon, an% a the s&bse<&ent ines 9&st one, in this !ase= are in%ente%. 'n%ente% ines !an start ith any n&mber of tab or spa!e !hara!ters, b&t they m&st a be in%ente% in the same ay. $his pattern 5 a ine hi!h en%s ith a !oon, fooe% by some in%ente% ines 5 is very !ommon in
#ython, an% e see it in severa more pa!es thro&gho&t this boo-. 4 gro&p of in%ente% ines is often !ae% a block of !o%e1.
'n this !ase, e refer to the in%ente% bo!- as the bo"y of the oop, an% the ines insi%e it i be ee!&te% on!e for ea!h eement in the ist. $o refer to the !&rrent eement, e &se the variabe name that e rote in the first ine. $he bo%y of the oop !an !ontain as many ines as e i-e, an% !an in!&%e a the f&n!tions an% metho%s that eve earne% abo&t, ith one important e!eption: ere not aoe% to !hange the ist hie insi%e the bo%y of the oop2.
7eres an eampe of a oop ith a more !ompi!ate% bo%y:
apes B $*omo sapiens*, *Pan troglodytes*, *%orilla gorilla* for ape in apes!
name/length B len(ape# first/letter B ape$0
print(ape C * is an ape. Lts name starts ith * C first/letter# print(*Lts name has * C str(name/length# C * letters*#
$he bo%y of the oop in the !o%e above has fo&r statements, to of hi!h are
print statements, so ea!h time ro&n% the oop e get to ines of o&tp&t. 'f e oo- at the o&tp&t e !an see a si ines:
1 'f yo&re famiiar ith any other programming ang&ages, yo& might -no !o%e bo!-s as things that are s&rro&n%e% ith !&ry bra!-ets 5 the in%entation %oes the same 9ob in #ython
2 Changing the ist hie ooping !an !a&se #ython to be!ome !onf&se% abo&t hi!h eements have area%y been pro!esse% an% hi!h are yet to !ome.
I2 ChapterB:+istsan%oops
omo sapiens is an ape. Lts name starts ith Lts name has 12 letters
Pan troglodytes is an ape. Lts name starts ith P Lts name has 1 letters
%orilla gorilla is an ape. Lts name starts ith % Lts name has 1 letters
hy is the above approa!h better than printing o&t these si ines in si separate statementsE e, for one thing, theres m&!h ess re%&n%an!y 5 here e ony nee%e% to rite to print statements. $his aso means that if e nee% to ma-e a !hange to the !o%e, e ony have to ma-e it on!e rather than three separate times. 4nother benefit of &sing a oop here is that if e ant to a%% some eements to the ist, e %ont have to to&!h the oop !o%e at a. Conse<&enty, it %oesnt matter ho many eements are in the ist, an% its not a probem if e %ont -no ho many are going to be in it at the time hen e rite the !o%e.
n"entation errors
*nfort&natey, intro%&!ing toos i-e oops that re<&ire an in%ente% bo!- of !o%e aso intro%&!es the possibiity of a ne type of error 5 an ;ndentationError. ;oti!e hat happens hen the in%entation of one of the ines in the bo!- %oes not mat!h the others:
apes B $*omo sapiens*, *Pan troglodytes*, *%orilla gorilla* for ape in apes!
name/length B len(ape# first/letter B ape$0
print(ape C * is an ape. Lts name starts ith * C first/letter# print(*Lts name has * C str(name/length# C * letters*#
hen e r&n this !o%e, e get an error message before the program even starts to r&n:
I3 ChapterB:+istsan%oops
Lndentation9rror! unindent does not match any outer indentation leel
hen yo& en!o&nter an ;ndentationError, go ba!- to yo&r !o%e an% %o&be !he!- that a the ines in the bo!- mat!h &p. 4so %o&be!he!- that yo& are &sing either tabs or spa!es for in%entation,not bot+. $he easiest ay to %o this, as
mentione% in !hapter 1, is to enabe tab emulation in yo&r tet e%itor.