I-Python Loops - Funda ukuthi ungayisebenzisela kanjani futhi ngenkathi i-Loops ePython

Amaluphu ayisici esibalulekile sanoma yiluphi uhlelo lohlelo noma lokubhala. Ukuba namandla okwenza umsebenzi amahlandla amaningi kubalulekile kunoma yiluphi ulimi.

EPython, ukuxegiselwa kutholakala ngokusetshenziswa kwe for futhi while izihibe futhi kule ndatshana sibheka ukuthi sizisebenzisa kanjani ngezibonelo.

IPython yeLoop

I- for iluphu ku-python ingasetshenziswa ngezindlela ezahlukahlukene. Indlela elula futhi ejwayelekile yokulinganisa ngaphezulu kweqoqo.

I-syntax

for i in collection:
statement

Iqoqo kungaba uhlu, isethi, ibanga, njll. I kuyinto eguquguqukayo ethatha inani lento esetshenziswayo.

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

Lokukhipha:

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

Uyabona ukuthi lolu hlu lubhalwa kanjani kusukela ekuqaleni kuze kube sekugcineni.



OkweLoop Nobubanzi ()

Indlela yebanga ku-python isetshenziselwa ukudala ukulandelana okuphakathi komkhawulo othile. Ake sithi awunalo uhlu kepha ufuna ukulengisa okuthile okuthile izikhathi ezithile. Ungasebenzisa range() indlela.

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

Lokukhipha:

0 1 2 3 4 Qaphela:amanani avela ku-0 kuye ku-4, hhayi u-0 kuye ku-5.

I- range() umsebenzi ubuyisa ukulandelana kwezinombolo, kusuka ku-0 ngokuzenzakalela, nokukhuphuka ngo-1 (ngokuzenzakalela), kuphele kunombolo ecacisiwe.

Uma besifuna ukuba nenani lokuqala elihlukile nenani lokukhuphuka elihlukile, sisebenzisa:

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

Lokukhipha:

10 15 20 25

Esibonelweni esingenhla, siqala kusuka ku-10, sinqamula ku-25 bese sikhuphuka ngo-5.

for Loop Ngokunye

I- else igama elingukhiye ku for ilup icacisa ibhulokhi lekhodi elizokwenziwa ngemuva kokuphela kwe-loop.

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

Lokukhipha:

0 1 2 3 4 5 Finished looping. Qaphela:Ku if isitatimende, okunye ukuvimba kusebenza kuphela uma isimo singamanga, kepha ku | _ _ _ _ | loop, | for block kwenziwa njalo.

I-Nested for Loops

Singaba ne- else loop ngaphakathi kwelinye for iluphu. Lokhu kubizwa ngokuthi iluphu enesidleke.

'I-loop yangaphakathi' izokwenziwa kanye ngesikhathi sokuphindaphindwa ngakunye kwe- 'loop yangaphandle'.

Isibonelo:

for

Lokukhipha:

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

IPython Ngenkathi iluphu

I- 1 a 1 b 1 c 2 a 2 b 2 c 3 a 3 b 3 c ilupu isebenzisa iqoqo lezitatimende inqobo nje uma isimo siyiqiniso.

Ngokwesibonelo:

whileQaphela:sidinga ukukhuphula inani le- i, ngaphandle kwalokho i-loop izosebenza ingunaphakade.

Ngenkathi ilula nokunye

Esinye isitatimende ngaphakathi kwe- i = 1 while i < 5:
print('Hello world')
i = i + 1
iluphu ikhipha kanye lapho isimo singaselona iqiniso.

Ngokwesibonelo:

while

Lokukhipha:

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

Isebenzisa break in ngenkathi iLoop

Isitatimende sekhefu siyasetshenziswa uma ufuna ukwephula ukwenziwa kweluphu endaweni ethile.

Esibonelweni esilandelayo, sifuna ukumisa iluphu lapho sihlangana nohlamvu 'c':

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

Lokukhipha:

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

Isebenzisa ukuqhubeka ku- ngenkathi I-Loop

Igama elingukhiye lokuqhubeka lisetshenziselwa ukweqa isitatimende bese uqhubeka nalo lonke iluphu ukuthola okuthile.

Esibonelweni esingezansi, sifuna ukuqhubeka ne-loop lapho sihlangana no- 'c':

a b c 'c' encountered. Breaking the loop

Lokukhipha:

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

Qaphela ukuthi u- “c” akaphrintiwe. I-loop iyaqhubeka nokuphrinta u- 'd' no- 'e'.

Isifinyezo

  • I- a b d e futhi for izihibe zisetshenziselwa iteration
  • Zisetshenziselwa ukwenza isethi yezitatimende kaningi noma ukunciphisa ngaphezulu kweqoqo elifana nohlu
  • I- while iluphu ku-python nayo ingasetshenziswa ne for indlela. Ungahlinzeka ngemikhawulo ephansi nangaphezulu noma umkhawulo ophezulu kuphela. Endabeni yokugcina, kuzothathwa u-0 njengomkhawulo ophansi
  • Sebenzisa i- range() loop lapho wazi ukuthi iluphu kufanele likwenze kangaki
  • I- for iluphu inesimo futhi isebenza kuze kube yilapho isimo singamanga
  • I- while iluphu kufanele lihlale linendlela yokuphula isimo noma iluphu lizohlala unomphela
  • Sebenzisa i-loop ngenkathi ungazi izikhathi eziningi ukuthi i-loop kufanele isebenzise
  • I- while ibhulokhi ingasetshenziswa ngakho kokubili i- else futhi for iluphu. Kuhlala kwenziwa.
  • I- while igama elingukhiye lisetshenziselwa ukuqeda ukwenziwa. Akukho okunye okuzokwenziwa okuzokwenziwa uma kuhlangana igama elingukhiye lekhefu.
  • I- break igama elingukhiye leqa i-iteration yamanje bese ligxumela ngqo ku-iteration elandelayo.