Python Zvatemwa Zvinyorwa - Kana, Zvimwe uye Elif

Muchidzidzo ichi tinotarisa mashandisiro if, else uye elif zvirevo muPython.

Kana tichinyora mune chero mutauro, pane nguva dzatinoda kutora danho nekuita imwe kodhi zvichibva pamhedzisiro yesarudzo.

MuPython, tinoshandisa if chirevo chekuyedza mamiriro.

Python Kana Chirevo

Iyo syntax ye if chirevo muPython ndechekuti:

if condition:
statement

Chengetedza zvakanyanya kune semi-colon : uye iyo induction .

Isu tinoshandisa vanoshanda zvine hungwaru kuongorora mamiriro. Iwo anoshanda anoshanda ndeaya:



  • Zvakaenzana: a == b
  • Kwete Vakaenzana: a != b
  • Zvishoma pane: a < b
  • Pasina kana kuenzana ne: a <= b
  • Kukura kupfuura: a > b
  • Yakakura kudarika kana kuenzana ne: a >= b

Iyo kodhi inotevera iyo if chirevo chinoitwa chete kana mamiriro acho akaongorora ku true.

Semuenzaniso if chirevo muPython:

password = 'Hello' if len(password) < 6:
print('password too weak - should be at least 6 characters')

Kubuda:

password too weak - should be at least 6 characters

Mune iri pamusoro kodhi, tiri kuongorora kureba kwepassword. Mamiriro acho ndeokuti, kureba hakufanirwe kunge kuri pasi pematanhatu mavara.

Izvi zvinoratidzwa neasinga pfuure opareta <.

Sezvo tambo 'Mhoro' iri pasi pevatanhatu mavara, ipapo mamiriro anoongorora ku ichokwadi uye nekudaro tinoona chirevo cheprinta.

Python Kana… Chimwe Chirevo

Kana mhedzisiro yekuongorora isiriyo uye isu tinoda kuita chiito pamhedzisiro, tobva tasanganisira else chirevo.

Iyo syntax ye if...else chirevo chinoratidzika senge:

if condition:
statement_1 else:
statement_2

Saka, tichienderera nemuenzaniso mumwe chete pamusoro, kana isu taida kuzivisa mushandisi kuti password yavo yasangana nehurefu hunodiwa, tinoisa izvo mu else bhuroka.

Muenzaniso:

password = 'Mission' if len(password) < 6:
print('password too weak - should be at least 6 characters') else:
print('your password was accepted')

Kubuda:

your password was accepted

Mune ino kesi, izwi 'Mission' rine mavara manomwe saka edu if mamiriro anoongorora kunhema. Nekuti isu tine else block, kozoti yechipiri print() chirevo chinoitwa.

Akawanda Kana… Zvimwe NeElif

Kana chirongwa chichifanira kubata anopfuura maviri kesi, tinofanirwa kushandisa akawanda if uye else mabhuroko. Izwi rakakosha elif zvinoreva zvimwe kana.

Semuenzaniso, isu tine chirongwa chinoda kuona mhando yetriangle inoenderana nematatu manhamba.

  • Kuyera katatu ndeimwe apo mativi ese matatu ane urefu hwakasiyana
  • Isosceles Triangle ine mativi maviri akaenzana kureba
  • Yakaenzana netriangle ndeimwe apo mativi ese akaenzana
a = 5 b = 5 c = 5 if a != b and b != c and a != c:
print('This is a scalene triangle') elif a == b and b == c:
print('This is an equilateral triangle') else:
print('This is an isosceles triangle')

Kubuda:

This is an equilateral triangle

Uyu muenzaniso unoratidza maitiro ekubata anopfuura maviri kesi. Sezvakamboita, rangarira : uye indentations.

Iko hakuna muganho pane nhamba ye elif s yatinogona kushandisa. Panofanira kunge paine chete else chirevo chinoshanda sekubata-kwese. Kana zvese if zvirevo zvinokundikana, ipapo else chirevo chinoitwa.

Python Ternary Operator (Shorthand Kana… Zvimwe)

Kana isu tine if...else block, tinogona kushandisa ternary opareta uye kunyora iyo if...else block mumutsetse mumwe.

Iyo syntax ndeiyi:

condition_if_true if condition else condition_if_false

Muenzaniso:

a = 100 b = 200 print('A') if a > b else print('B')

Kubuda:

B

Mhedziso

  • I if...else uye elif zvirevo zvinodzora kuyerera kwechirongwa.
  • Iyo if chirevo muchirongwa chinoshandiswa pakugadzirisa zvisarudzo.
  • Chirevo ichi if chinoongororwa pachishandiswa mamiriro (mamiriro).
  • Rimwe bhuroko rinogona kusunganidzwa ne if chirevo uye rinoitwa kana mamiriro acho ari enhema.
  • Icho chimwe chinogumbura hachigone kuvapo neshoko chirevo
  • Chirevo cheElif (s) chinogona kusungirirwa neye if chirevo kana paine akawanda mamiriro.