Python Variables - Maitiro ekuzivisa uye kushandisa akasiyana muPython

Nongedzo pamaitiro ekugadzira uye kushandisa akasiyana muPython.

Python Variables

Misiyano yakatumidzwa nzvimbo dzinoshandiswa kuchengetedza mareferenzi echinhu chakachengetwa mundangariro.

Patinogadzira akasiyana muPython, isu tinofanirwa kufunga nezve inotevera mitemo:

  • Zita rinoshanduka rinofanira kutanga netsamba kana pasi
  • Zita rinoshanduka harigone kutanga nenhamba
  • Zita rinoshanduka rinongogona kunge riine mavara alpha-manhamba uye ezasi (A-z, 0-9, uye _)
  • Mazita akasarudzika ane mhosva-yakatarwa (zuva, Zuva uye DATE matatu matatu akasiyana)
  • Misiyano inogona kuve chero yehurefu
  • Mazita anoshanduka haagone kuve Python keywords

Python Mazwi Akakosha

False
class
finally is
return None
continue for
lambda
try True
def
from
nonlocal while and
del
global
not
with as
elif
if
or
yield pass
else
import
assert break
except
in
raise

Kugovera Maitiro kune akasiyana

Isu tinoshandisa iyo yekupa opareta = kupa kukosha kune musiyano.

Semuenzaniso anoshanda uye asingashande mazita akasiyana uye kupihwa mabasa:

#Legal variable names: name = 'John' error_404 = 404 _status_200 = 'OK' mySurname = 'Doe' SURNAME = 'Doe' surname2 = 'Doe' #Illegal variable names: 200_OK = 200 error-500 = 'Server Error' my var = 'John' $myname = 'John' Ongorora:muPython, haufanire kuzivisa mhando dzemhando dzakasiyana nguva isati yasvika. Muturikiri anozvionera ega mhando yeshanduko nedata rayo.

Akawanda Mabasa

MuPython, isu tinogona kugovera kukosha kune akawanda akasiyana mumutsara mumwe:



Muenzaniso:

ok, redirect, server_error = 200, 300, 500 print(ok) print(redirect) print(server_error)

Kubuda:

200 300 500

Isu tinogona zvakare kupa iwo iwo kukosha kwakasiyana kune akawanda akasiyana.

err_500 = err_501 = err_502 = 'server_error' print(err_500) print(err_501) print(err_502)

Mhando dzepasi rose

Misiyano inotsanangurwa kunze kwechiitiko inozivikanwa semusiyano wepasirese.

Misiyano yepasi rose inogona kushandiswa zvese mukati nekunze kwemabasa.

status_ok = 200 def status_code():
print('Status code is ', status_ok) status_code()

Kana iwe ukagadzira musiyano une zita rimwe chete mukati mekuita basa, ipapo musiyano uchave wemuno kune basa. Musiyano wepasi rose unozochengetedza kukosha kwawo sepazvakaziviswa.

Muenzaniso:

status = 200 def status_code():
status = 401
print('Status code is ', status) status_code() print('Status code is ', status)

Kubuda:

Status code is 401 // first print statement Status code is 200 // second print statement

Kana iwe uchida kuti ushandure kukosha kwepasirese kwepasi mukati mebasa, unofanirwa kushandisa global kiyi.

Semuyenzaniso:

status = 200 def status_code():
global status
status = 401
print('Status code is ', status) status_code() print('Status code is ', status)

Kubuda

Status code is 401 // first print statement Status code is 401 // second print statement