I-Python Loops-Funda ukuba uyisebenzisela njani kwaye ngelixa uyifaka kwiPython

Imilambo yinto ebalulekileyo kuyo nayiphi na inkqubo okanye ulwimi lokubhala. Ukuba nesakhono sokwenza umsebenzi amaxesha amaninzi kubalulekile kulo naluphi na ulwimi.

KwiPython, ukukhangela kufezekiswa ngokusetyenziswa kwe for kunye while ii-loops kwaye kweli nqaku sijonga indlela yokuzisebenzisa ngemizekelo.



IPython yeLoop

I for loop kwi-python inokusetyenziswa ngeendlela ezahlukeneyo. Enye indlela elula neqhelekileyo yokuyilungisa ngaphezulu kwengqokelela.


Isintaksi

for i in collection:
statement

Ingqokelela inokuba luluhlu, iseti, uluhlu, njl njl., Umahluko othatha ixabiso lezinto ezihanjiswayo.

my_list = [1, 2, 3, 4, 5] for i in my_list:
print('Value is:', i)

Iziphumo:


Value is 1 Value is 2 Value is 3 Value is 4 Value is 5

Ungayibona indlela oluhlu olwenziwa ngayo ukusuka ekuqaleni ukuya esiphelweni.



kwiluphu enoluhlu ()

Indlela yoluhlu kwi-python isetyenziselwa ukwenza ukulandelelana phakathi komda othile. Masithi awunalo uluhlu kodwa ufuna ukuloba ngaphezulu kwento ethile ngamaxesha athile. Ungasebenzisa range() indlela.

for i in range(5):
print(i)

Iziphumo:

0 1 2 3 4 Phawula:amaxabiso aqala ku-0 kuye ku-4, hayi ku-0 kuye ku-5.

I range() Umsebenzi ubuyisela ukulandelelana kwamanani, ukuqala ngo-0 ngokungagqibekanga, kunye nokunyusa ngo-1 (ngokungagqibekanga), kwaye kuphele kwinani elichaziweyo.


Ukuba sifuna ukuba nexabiso lokuqala elahlukileyo kunye nexabiso lokunyuka elahlukileyo, sisebenzisa:

for i in range(10, 30, 5): print(x)

Iziphumo:

10 15 20 25

Kumzekelo ongentla, siqala kwi-10, siphelisa ngo-25 kwaye sonyusa ngo-5.



for Loop Kunye nokunye

I else igama eliphambili kwi for loop ichaza ibhloko yekhowudi ekufuneka yenziwe emva kokuba iluphu igqityiwe.


for i in range(6): print(i) else: print('Finished looping.')

Iziphumo:

0 1 2 3 4 5 Finished looping. Phawula:Kwi if Isiteyitimenti, enye ibhloko iyasebenza kuphela xa imeko ingeyonyani, kodwa kwi | _ _ _ _ | loop, | for Ibhlokhi ihlala isenziwa.

Eyenzelwe iiLuphu

Singaba else loop ngaphakathi kwenye for loop. Oku kubizwa ngokuba ngumtya okhohlakeleyo.

'I-loop yangaphakathi' iya kwenziwa ngexesha elinye ukulungiselela iteration nganye ye 'loop engaphandle'.

Umzekelo:


for

Iziphumo:

numbers = [1, 2, 3] chars = ['a', 'b', 'c'] for i in numbers: for y in chars:
print(x, y)


IPython Ngelixa iluphu

I 1 a 1 b 1 c 2 a 2 b 2 c 3 a 3 b 3 c loop iphumeza iseti yeengxelo ukuba nje imeko iyinyani.

Umzekelo:

whilePhawula:kufuneka sonyuse ixabiso lika i, ngapha koko iluphu iya kuqhuba ngonaphakade.

Ngelixa ukujikeleza kunye nokunye

Enye ingxelo ngaphakathi i = 1 while i < 5:
print('Hello world')
i = i + 1
loop iphumeza kube kanye xa imeko ingeyonyani.


Umzekelo:

while

Iziphumo:

i = 1 while i < 5:
print('Hello world')
i = i + 1 else:
print('The execution has ended')


Sebenzisa break in while Loop

Isiteyitimenti sekhefu sisetyenziswa ukuba ufuna ukwaphula ukwenziwa kweluphu kwindawo ethile.

Kumzekelo olandelayo, sifuna ukumisa iluphu xa sihlangana no 'c' umlinganiswa:

Hello world Hello world Hello world Hello world The execution has ended

Iziphumo:

my_list = ['a', 'b', 'c', 'd', 'e'] for i in my_list:
print(i)
if i == 'c':
print(''c' encountered. Breaking the loop')
break


Sebenzisa ukuqhubeka ngaphakathi ngelixa uLoop

Igama eliphambili eliqhubayo lisetyenziselwa ukutsiba ingxelo kwaye uqhubeke nayo yonke iluphu yendlela ethile.

Kumzekelo ongezantsi, sifuna ukuqhubeka nelog xa sidibana no 'c':

a b c 'c' encountered. Breaking the loop

Iziphumo:

my_list = ['a', 'b', 'c', 'd', 'e'] for i in my_list:
if i == 'c':
continue
print(i)

Qaphela ukuba u 'c' akaprintwanga. Loop iyaqhubeka nokuprinta u 'd' kunye no 'e'.



Isishwankathelo

  • I a b d e kunye for ii-loops zisetyenziselwa ukwenza iteration
  • Zisetyenziselwa ukwenza iseti yeengxelo amaxesha amaninzi okanye ukuphinda uqokelele ingqokelela efana noluhlu
  • I while loop kwi python inokusetyenziswa kunye for indlela. Unokubonelela ngemida ephantsi kunye ephezulu okanye umda ophezulu kuphela. Kwimeko yokugqibela, u-0 uyakuthathwa njengomda ongezantsi
  • Sebenzisa range() loop xa usazi ukuba maluphindele kangaphi ixesha lokwenza
  • I for iluphu inemeko kwaye iqhuba de imeko ibubuxoki
  • I while iluphu kufuneka ihlale inendlela yokuphula imeko okanye iluphu iya kusebenza ngonaphakade
  • Sebenzisa iluphu yexeshana xa ungazi amaxesha amaninzi ukuba iluphu mayiphumeze
  • I while ibhlokhi inokusetyenziswa kunye | else kunye for loop. Ihlala isenziwa.
  • I while igama eliphambili lisetyenziselwa ukuphelisa ukwenziwa. Ayizukuphinda yenziwe into eyenziweyo xa kufikwa kwigama eliphambili lekhefu.
  • I break igama eliphambili litsiba iteration yangoku kwaye litsibe ngqo kwi-iteration elandelayo.