Python zvishwe - Dzidza mashandisiro uye uye nepo zvishwe muPython

Zvishwe chinhu chakakosha chechero chirongwa kana mutauro wekunyora. Kuva nekwanisi yekuita basa kakati wandei kwakakosha kune chero mutauro.

MuPython, kusunungura kunowanikwa kuburikidza nekushandiswa kwe for uye while zvishwe uye muchinyorwa chino tinotarisa mashandisiro avo nemienzaniso.

Python yeLoop

I for chiuno mu python chinogona kushandiswa munzira dzakasiyana siyana. Imwe yakapusa uye yakajairika nzira kuti iterate pamusoro peunganidzwa.

Syntax

for i in collection:
statement

Iko kuunganidzwa kunogona kuve runyorwa, kumisikidzwa, renji, nezvimwewo i musiyano unotora kukosha kwechinhu icho chiri kuitirwa.

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

Kubuda:

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

Unogona kuona kuti rondedzero yacho inoitiswa sei kubva pakutanga kusvika kumagumo.

yeLoop Ine range ()

Iyo renji nzira mu python inoshandiswa kugadzira kuteedzana kuri pakati peimwe ganho. Ngatiti iwe hauna rondedzero asi iwe unoda kusunungura pamusoro pechimwe chinhu chakatarwa nhamba yenguva. Unogona kushandisa range() nzira.

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

Kubuda:

0 1 2 3 4 Ongorora:kukosha kwacho kuri kubva pa0 kusvika pa4, kwete 0 kusvika pa5.

I range() basa rinodzosa akateedzana manhamba, kutanga kubva pa0 nekukasira, uye kuwedzererwa na1 (nekusarudzika), uye kunopera panhamba yakataurwa.

Kana isu taida kuve neyakasiyana yekutanga kukosha uye imwe yekuwedzera kukosha, isu tinoshandisa:

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

Kubuda:

10 15 20 25

Mumuenzaniso wepamusoro, isu tinotanga kubva gumi, tinomira pa25 uye isu tinowedzera ne5.

for Loop Nezvimwewo

I else keyword mu for chiuno chinotsanangura bumbiro rekodhi rinofanira kuitwa mushure mekunge chiuno chapera.

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

Kubuda:

0 1 2 3 4 5 Finished looping. Ongorora:Mune if chirevo, zvimwe block inoita chete kana mamiriro acho ari enhema, asi mu for loop, iyo else block inogara ichiurayiwa.

Yakagadzirirwa Zvishongo

Tinogona kuva for loop mukati meimwe for chiuno. Izvi zvinodaidzwa kuti nested loop.

'Loop yemukati' ichaurayiwa kamwechete kune imwe neimwe iteration ye 'yekunze chiuno'.

Muenzaniso:

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

Kubuda:

1 a 1 b 1 c 2 a 2 b 2 c 3 a 3 b 3 c

Python Paunenge chiuno

I while chiuno chinoseta seti yezvitau chero bedzi mamiriro ari echokwadi.

Semuyenzaniso:

i = 1 while i < 5:
print('Hello world')
i = i + 1
Ongorora:isu tinoda kuwedzera kukosha kwe i, zvikasadaro chiuno chinoita nekusingaperi.

Ndichiri chiuno neimwe

Chimwe chirevo mukati me while chiuno chinoitisa kamwe kana mamiriro acho asisiri echokwadi.

Semuyenzaniso:

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

Kubuda:

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

Uchishandisa break in uchiri Loop

Chirevo chekutyora chinoshandiswa kana iwe uchida kutyora kuitiswa kwechiuno pane imwe nguva.

Mumuenzaniso unotevera, tinoda kumisa chiuno kana tasangana ne 'c' hunhu:

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

Kubuda:

a b c 'c' encountered. Breaking the loop

Uchishandisa enderera mukati muchiri Loop

Iyo rinoenderera kiyi rinoshandiswa kusvetuka chirevo uye kuenderera neakasara chiuno kune imwe iteration.

Mumuenzaniso uri pazasi, isu tinoda kuenderera ne loop patinosangana na 'c':

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

Kubuda:

a b d e

Ziva kuti 'c' haina kudhindwa. Loop inoenderera ichiprinta 'd' uye 'e'.

Pfupiso

  • I for uye while zvishwe zvinoshandiswa kuitisa
  • Ivo anoshandiswa kuita seti yemataurirwo kakawanda kana kuti iterate pamusoro peunganidzwa senge runyorwa
  • I for chiuno mu python chinogona zvakare kushandiswa ne range() nzira. Iwe unogona kana kupa zvese zvakaderera uye zvepamusoro miganhu kana chete yepamusoro muganho. Mune yekupedzisira kesi, 0 ichafungidzirwa semuganhu wezasi
  • Shandisa for chiuno kana iwe uchiziva kuti kangani chiuno chinofanira kuitisa
  • I while chiuno chine mamiriro uye chinomhanya kusvikira mamiriro acho ari enhema
  • I while chiuno chinofanira kugara chiine nzira yekutyora mamiriro kana chiuno chinomhanya zvachose
  • Shandisa iyo while chiuno kana iwe usingazive kakawanda iyo chiuno chinofanira kuitisa
  • I else block inogona kushandiswa nezvose for uye while chiuno. Inogara ichiurayiwa.
  • I break kiyi kiyi inoshandiswa kumisa kuuraya. Hapana imwezve iteration ichaitwa kana break izwi rakasangana.
  • I continue keyword inodarika iyo yazvino iteration uye inosvetuka yakananga kune inotevera iteration.