Imitya yePython - Ushwankathelo lweSiseko seNtambo yokuSebenza

Imitya yenye yeendlela ezisisiseko zedatha kwiPython. Imitya yePython yindibaniselwano yalo naliphi na inani labalinganiswa elenziwe ngoonobumba, amanani, kunye nabanye oonobumba abakhethekileyo. Kule tutorial, uya kufunda ukuba uyenza njani, uyenze ngobuqili kwaye uyifomathile ukuba bayisebenzise phantsi kweemeko ezahlukeneyo.



Yenza imitya emitsha kwiPython

Ukwenza umtya omtsha wePython, kuya kufuneka ubhengeze ulandelelwano lweempawu ezifakwe kumanqaku omnye okanye aphindwe kabini. Amanqaku okucaphula kathathu asetyenziselwa imitya emininzi enemigca emininzi.

double_quotes = 'My name is John!' single_quotes = 'My name is John!' multi_line_string = '''1. My name is John!



2. I am a programmer'''


Isalathiso somtya

Bonke abalinganiswa kumtya wePython banesalathiso senani elipheleleyo. Isalathiso siqala ku-0 kumlinganiswa wokuqala kunye nokunyuka kunye nomtya. Ungasebenzisa isalathiso somlinganiswa ngamnye ukufumana umlinganiswa kumtya njengolu hlobo lulandelayo lubonisa.


myPet = 'Dog not a cat' myPet[0] # 'D' myPet[5] # 'o' myPet[7] # ' ' myPet[12] # 't' # myPet[15] # IndexError

Ukuzama ukufikelela kumlinganiswa ongaphaya kwesalathiso seziphumo zomlinganiswa wokugqibela kwi Isalathiso seMpazamo .

Ungafikelela kumlinganiswa kumtya usebenzisa isalathiso esibi. Kule meko, isalathiso siqala -1 kuhlobo lokugqibela lomtya, kwaye unyuse kakubi njengoko ubuyela umva.


myPet = 'Dog not a cat' myPet[-1] # 't' myPet[-6] # ' ' myPet[-8] # 'o' myPet[-13] # 'D'

Ukucoca umtya

Ukucanda yindlela yokufumana umtya (inxalenye yomtya) kumtya. Lo msebenzi uphunyezwe ngoncedo lwesalathiso somtya.

myPet = 'Dog not a cat' myPet[5:7] # 'ot' myPet[1:12] # 'og not a ca'

Apha, ii-indices ezimbini zibonelelwa zahlulwe yikholoni, isalathiso sokuqala sibonisa apho kufuneka siqale khona ukusika kwaye isalathiso sesibini sibonisa apho kufuneka ume khona. Isiphelo esivelisayo sibandakanya oonobumba ukusuka kwisalathiso sokuqala ukuya kumlinganiswa ngaphambi kwesalathiso sokuphela, umlinganiswa okwisalathiso sokuphela akabandakanywanga kumgca ongezantsi.

Ukuba awunikezeli ngesalathiso sokuqala, ukusika kuqala kumlingiswa wokuqala womtya. Ukuba awunikezeli ngesalathiso sokuphela, ukusikwa kuphelela kumlinganiswa wokugqibela ngelixa uyifaka kwisiphumo esisezantsi.

myPet = 'Dog not a cat' myPet[:7] # 'Dog not' myPet[10:] # 'cat' myPet[:] # 'Dog not a cat'

Ungabonelela ngee-indices ezimbi njengezinto zokucheba ii-indices ngokunjalo.


myPet = 'Dog not a cat' myPet[10:-1] # 'ca'

Ubude bomtya

Indlela eyakhelwe-ngaphakathi yePython len() ikhupha ubude bomtya.

myPet = 'Dog not a cat' len(myPet) # 13

Iterate nge String

Ungayilungisa ngokusebenzisa uphawu ngalunye kumtya usebenzisa for loop.

Umzekelo:

name = 'John' for char in name:
print(char) # 'J', 'o', 'h', 'n'


Ukudityaniswa komtya

Umtya concatenation kukudibanisa imitya emibini okanye nangaphezulu ukwenza umtya omnye. KwiPython, zininzi iindlela zokwenza umtya odibeneyo.


Omnye usebenzisa + umqhubi.

str1 = 'Hello' str2 = 'World' concat_str = str1 + str2 # 'HelloWorld' concat_str = str1 + ' ' + str2 # 'Hello World'

Ungasebenzisa * opharetha ukuzenzela umtya kuwo naliphi na inani lamaxesha.

concat_str = str1*3 # 'HelloHelloHello'

Enye indlela yokudibanisa imitya nge join() indlela.

Eyakhelwe ngaphakathi join() indlela isetyenziselwa ukudibanisa uluhlu lweentambo kusetyenziswa isahluli esiqhelekileyo.


arr = [str1, str2] concat_str = (' ').join(arr) # 'Hello World' concat_str = (',').join(arr) # 'Hello,World'

Kule khowudi ingentla, eyokuqala join() indlela yongeza indawo emhlophe phakathi kwamagama onke kuludwe.

Eyesibini join() Indlela yokufaka ikoma phakathi kwamagama onke kuluhlu.



Umtya kunye ne-Int Concatenation

KwiPython, sinokuphinda sihambise umtya kwinani elipheleleyo kodwa hayi nge + umqhubi. Ukuba sizama ukusebenzisa le khowudi ilandelayo:

name = 'John' age = 35 print(a + b)

Siza kufumana:


Traceback (most recent call last): File 'concat.py', line 5, in
print(a + b) TypeError: can only concatenate str (not 'int') to str
Phawula:Awunakho ukudibanisa umtya kunye nenani elipheleleyo usebenzisa + umqhubi.

Ukuthintela le mpazamo, singasebenzisa str() indlela yokuguqula inani elipheleleyo libe ngumtya, umzekelo:

name = 'John ' age = '35' print(a + str(b)) #John 35

Uzahlula njani umtya

Eyakhelwe ngaphakathi split() indlela isetyenziselwa ukwahlula umtya omnye kuluhlu lweentambo.

string = 'My name is John' split_arr = string.split(' ') # ['My', 'name', 'is', 'John'] We can also split a string using a separator: string = 'John, Rose, Jack, Mary' split_arr = string.split(', ') # ['John', 'Rose', 'Jack', 'Mary']

Strip-Susa iZithuba eziMhlophe

strip(), indlela eyakhelwe-ngaphakathi yomtya isetyenziselwa ukususa izithuba ezimhlophe ekuqaleni nasekupheleni komtya.

string = ' Hello, World ' stripper_str = string.strip() # 'Hello, World'

Njengoko ubona, strip() ayizisusi izithuba ezimhlophe eziphakathi kwabanye oonobumba kodwa kuphela kwiziphelo ezibini.

Zimbini izinto ezahlukileyo ze strip() indlela, Strip yasekhohlo kunye ne-Strip yasekunene:

  • lstrip()
  • rstrip()

Ezi ndlela zisusa izithuba ezimhlophe kwicala lasekhohlo nakwicala lasekunene lomtya, ngokulandelelana.

Umzekelo:

lsplit_str = string.lstrip() # 'Hello, World ' rsplit_str = string.rstrip() # ' Hello, World'

Iindlela ze-Strip ziluncedo ngakumbi xa kufundwa igalelo lomsebenzisi, apho iindawo ezongezelelekileyo ezimhlophe zinokugqithiswa ngabasebenzisi.



Ukufomatha umtya

IPython's format() indlela isetyenziselwa ukufomatha umtya. Izihlangu ezisongekileyo {} zisetyenziswa ngaphakathi komtya ofuna ukuba ufomathiwe njengendawo yokugcina indawo ekufuneka ithathelwe indawo ziimpikiswano ezinikezelwe kwi format() indlela.

Umzekelo:

'Hello, {}'.format('John') # 'Hello, John'

Kumzekelo ongentla {} ithathelwe indawo 'nguJohn' kumtya ofomathiweyo.

Ungasebenzisa ngaphezulu kwesisongelo esinye ngaphakathi ngaphakathi komtya ukufomatha. Bathatyathelwe indawo ziimpikiswano ezinikezelwe format() indlela nokuba yeyiphi kulungelelwaniso olunikezelweyo (ukuba akukho zalathiso zezikhundla zikhankanyiweyo ngaphakathi kubakaki abagobileyo) okanye ngokulandelelana.

Umzekelo:

'I have a {}, {}, and a {}'.format('dog', 'cat', 'rabbit') # 'I have a dog, cat, and a rabbit' 'I have a {1}, {0}, and a {2}'.format('dog', 'cat', 'rabbit') # 'I have a cat, dog, and a rabbit'

Endaweni yokusebenzisa ii-indices, ungabonelela ngegama eliphambili kwi format() | ukuze la magama asetyenzisiweyo asetyenziswe ngaphakathi kwi-curly brace.

Umzekelo:

print('{friend} is my friend and {enemy} is my enemy'.format(friend='John', enemy='Jack')) # 'John is my friend and Jack is my enemy'

I format() Le ndlela ibhetyebhetye njengoko inokusetyenziselwa iimeko ezininzi zokusetyenziswa.

Nazi ezinye izicelo ze format() | indlela:

arr = [3, 5] 'I have {0[0]} dogs and {0[1]} cats'.format(arr) # 'I have 3 dogs and 4 cats' #convert numbers to different bases 'int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}'.format(42) # 'int: 42; hex: 2a; oct: 52; bin: 101010'

Guqula umtya ube ngamaNqaku amancinci

Sebenzisa iPython's _ _ + _ | indlela, unokuguqula umtya ube ngamagama amancinci.

Umzekelo:

lower()

Guqula umtya ube ngunobumba omkhulu

Ngokufanayo, usebenzisa iPython's _ _ + _ | indlela, unokuguqula umtya ube ngunobumba abakhulu.

Umzekelo:

string = 'Hello, World!' string.lower() # 'hello, world!'

Isiphelo

Ndiyathemba ukuba ngoncedo lwesi sifundo, ngoku uqhelene nentambo yePython kunye nendlela yokusebenzisa iindlela ezahlukeneyo zokwenza umtya.

Isalathiso: Amaxwebhu omtya kaPython