Izinto eziguquguqukayo kwiPython- Indlela yokuBhengeza kunye nokuSebenzisa eziguquguqukayo kwiPython

Isikhokelo sendlela yokwenza kunye nokusebenzisa izinto eziguquguqukayo kwiPython.



Izinto eziguquguqukayo kwiPython

Izinto eziguquguqukayo zibizwa njengeendawo ezisetyenziselwa ukugcina izingqinisiso zento egcinwe kwimemori.

Xa sisenza izinto eziguquguqukayo kwiPython, kufuneka siqwalasele le mithetho ilandelayo:


  • Igama eliguqukayo kufuneka liqale ngonobumba okanye ngezantsi
  • Igama eliguqukayo alinakuqala ngenani
  • Igama eliguqukayo linokuqulatha kuphela oonobumba be-alpha kunye namanqaku angaphantsi (A-z, 0-9, kunye _)
  • Amagama aguqukayo athathelwa ingqalelo yimeko (umhla, umhla kunye nomhla zizinto ezintathu ezahlukeneyo)
  • Izinto eziguquguqukayo zinokuba nabuphi na ubude
  • Amagama aguqukayo awanakuba ngamagama aphambili ePython


Amagama aphambili ePython

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


Ukunikezela amaxabiso kwizinto eziguquguqukayo

Sisebenzisa isinikezeli somsebenzi = ukunika ixabiso okwahlukileyo.

Umzekelo amagama asebenzayo kunye nokungasebenziyo kunye nokunikezelwa:


#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' Phawula:kwiPython, awudingi kubhengeza iintlobo zezinto eziguquguqukayo ngaphambi kwexesha. Itoliki ibhaqa ngokuzenzekelayo uhlobo lokwahluka ngokwedatha enayo.

Imisebenzi emininzi

KwiPython, singabela amaxabiso kwizinto ezininzi eziguquguqukayo kumgca omnye:

Umzekelo:

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

Iziphumo:

200 300 500

Sinokwabela ixabiso elifanayo kwizinto ezininzi eziguquguqukayo:


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

Izinto eziguquguqukayo kwihlabathi

Izinto ezichazwe ngaphandle komsebenzi zaziwa njengeenguqu zehlabathi.

Izinto eziguquguqukayo kwihlabathi liphela zinokusetyenziswa ngaphakathi nangaphandle kwemisebenzi.

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

Ukuba wenza umahluko ngegama elifanayo ngaphakathi komsebenzi, okwahlukileyo kuya kuba kokwasekhaya kulo msebenzi. Umahluko wehlabathi uya kuligcina ixabiso lawo njengoko lalibhengeziwe.

Umzekelo:


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

Iziphumo:

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

Ukuba ufuna ukutshintsha ixabiso lokwahluka kwehlabathi ngaphakathi komsebenzi, kuya kufuneka usebenzise global igama elingundoqo.

Umzekelo:

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

Iziphumo


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