Ko te koropiko tetahi ahuatanga nui o tetahi reo tuhi, tuhi reo tuhi ranei. Ko te kaha ki te whakahaere i tetahi mahi i nga wa maha he mea nui ki tetahi reo.
I te Python, ka tutuki te koropiko ma te whakamahi i te for
me while
koropiko ana kei roto i tenei tuhinga ka tirohia me pehea te whakamahi me nga tauira.
Ko te for
koropiko i roto i te python ka taea te whakamahi i roto i nga momo huarahi. Kotahi te maamaa me te huarahi noa hei huri i te kohinga.
for i in collection:
statement
Ka taea e te kohinga te raarangi, te huinga, te awhe, me era atu. He taurangi ahau e tango ana i te uara o te waahanga e tautuhi ana.
my_list = [1, 2, 3, 4, 5] for i in my_list:
print('Value is:', i)
Putanga:
Value is 1 Value is 2 Value is 3 Value is 4 Value is 5
Ka kite koe me pehea te raarangi i te raarangi mai i te tiimata ki te mutunga.
Ko te tikanga awhe i te python e whakamahia ana hei hanga raupapa i waenga i tetahi rohe. Whakaarohia kaore koe i te raarangi engari kei te hiahia koe ki te kopikopiko i tetahi mea kua tohua mo te maha o nga waa. Ka taea e koe te whakamahi i te range()
tikanga.
for i in range(5):
print(i)
Putanga:
0 1 2 3 4
Panui:ko nga uara mai i te 0 ki te 4, kaore i te 0 ki te 5.Ko te range()
ka hoki mai te mahi i te raupapa tau, ka tiimata mai i te 0 i te taunoa, me nga taapiri ma te 1 (na te taunoa), ka mutu ka tau i te tau kua whakaritea
Mena i hiahia maatau ki te whai i tetahi uara timatanga rereke me te uara whakarahi rereke, ka whakamahia e maatau:
for i in range(10, 30, 5): print(x)
Putanga:
10 15 20 25
I roto i te tauira i runga ake nei, ka tiimata mai i te 10, ka mutu i te 25 ka piki ake ma te 5.
Ko te else
kupu matua i roto i te for
ka tohu te koropiko he poraka waehere ka mahia i muri i te mutunga o te koropiko.
for i in range(6): print(i) else: print('Finished looping.')
Putanga:
0 1 2 3 4 5 Finished looping.
Panui:Kei te if
tauākī, ka kore te poraka e whakahaere i te wa e he ana te tikanga, engari kei te for
koropiko, te else
ka mahia tonu te poraka.Ka taea e taatau he _ _ + _ | koropiko ki roto i tetahi atu for
koropiko Ka kiia tenei ko te koropiko i ohanga.
Ko te 'koropiko o roto' ka whakahaerehia i te wa kotahi mo ia whakawhitinga o te 'koropiko o waho'.
Tauira:
for
Putanga:
numbers = [1, 2, 3] chars = ['a', 'b', 'c'] for i in numbers: for y in chars:
print(x, y)
Ko te 1 a 1 b 1 c 2 a 2 b 2 c 3 a 3 b 3 c
ka mahia e te koropiko etahi huinga korero mena ka pono he tikanga.
Hei tauira:
while
Panui:me whakanui e tatou te wariu o i, mena ka kore te koropiko e mahi mo ake tonu atu.Ko tetahi atu korero kei roto i te i = 1 while i < 5:
kotahi te koropiko ka kore e pono te tikanga.
print('Hello world')
i = i + 1
Hei tauira:
while
Putanga:
i = 1 while i < 5:
print('Hello world')
i = i + 1 else:
print('The execution has ended')
Ka whakamahia te korero taupatupatu mena kei te hiahia koe ki te whati i te mahi koropiko i tetahi waa.
I roto i te tauira e whai ake nei, e hiahia ana maatau ki te whakamutu i te koropiko ka tutaki ana ki te 'c' kiripuaki:
Hello world Hello world Hello world Hello world The execution has ended
Putanga:
my_list = ['a', 'b', 'c', 'd', 'e'] for i in my_list:
print(i)
if i == 'c':
print(''c' encountered. Breaking the loop')
break
Ko te kupu matua haere tonu ka whakamahia ki te peke i tetahi korero ka haere tonu me te toenga o te koropiko mo tetahi whitiuatanga.
I roto i te tauira i raro ake nei, e hiahia ana matou ki te haere tonu me te koropiko ka tutaki tatou ki te 'c':
a b c 'c' encountered. Breaking the loop
Putanga:
my_list = ['a', 'b', 'c', 'd', 'e'] for i in my_list:
if i == 'c':
continue
print(i)
Kia mahara kaore 'c' i taarua. Kei te taarua tonu te koropiko i te “d” me te “e”.
a b d e
me for
ka whakamahia nga koropiko mo te taaruawhile
ka taea hoki te whakamahi i te koropiko ki te python me te for
tikanga. Ka taea e koe te whakarato i nga rohe o raro me o runga ranei, ko te rohe nui anake. I te keehi whakamutunga, 0 ka kiia ko te rohe itirange()
koropiko ina mohio koe e hia nga wa ka mahia e te koropikofor
he mate to te koropiko ka rere ana kia he ana te tikangawhile
kia whai tikanga tonu te koropiko ki te wehe i te ahuatanga ka rere ranei te koropiko a ake akewhile
ka taea te whakamahi te poraka me nga else
me for
koropiko Ka mahia tonu.while
Whakamahia ai te kupu matua ki te whakamutu i te mahi. Kaore e tua atu ka mahia mena ka kitea te kupu whakataki.break
Ka hipa te kupu matua i te whitiauau o tenei wa ka peke totika atu ki te whainga e whai ake nei.