Uluhlu lwePython

Kule tutorial siza kufunda malunga nePython Lists; indlela yokwenza uluhlu, ukufikelela kwizinto, ukususa izinto, ukucima uluhlu njalo njalo.

KwiPython, uLuhlu lwakhiwe kusetyenziswa iibrakethi zesikwere [] kwaye into nganye kuluhlu yahlulwe ngekoma ,.

Uluhlu lwePython lungaqulatha iintlobo ezahlukeneyo zezinto, ngenxa yoko azidingi ukuba yimitya, okanye iinamba. Umzekelo, sinokuba noluhlu oluqulathe iintlobo ezixubeneyo:


mixedTypesList = ['a', True, 1, 1.0]

Uyenza njani uluhlu

colorsList = ['red', 'green', 'blue'] print(colorsList)

Iziphumo:

['red', 'green', 'blue']

Ungazifumana njani izinto zoluhlu

Khumbula: into yokuqala kuluhlu ikwisalathiso 0.


colorsList = ['red', 'green', 'blue'] print(colorsList[2])

Iziphumo:

blue

Ukufikelela kuLuhlu lweZinto (usikiwe)

Singachaza uluhlu lwezinto ezivela kuluhlu ngokuchaza isalathiso sokuqala kunye nesalathiso sokuphela. Sisebenzisa : umqhubi.

Phawula: Kumzekelo olandelayo, iziphumo zivela kwisalathiso 1 (sifakiwe) kwisalathiso 4 (ngaphandle)

colorsList = ['red', 'green', 'blue', 'orange', 'yellow', 'white'] print(colorsList[1:4])

Iziphumo:


['green', 'blue', 'orange']

Isalathiso esingalunganga

Singafikelela kwizinto ezikuluhlu ukusuka esiphelweni ngokuchaza ixabiso lesalathiso esibi. Umzekelo -1 Kuthetha into yokugqibela kunye -2 kuthetha into yesibini yokugqibela.

colorsList = ['red', 'green', 'blue', 'orange'] print(colorsList[-1])

Iziphumo:

orange

Guqula Ixabiso lento

colorsList = ['red', 'green', 'blue', 'orange'] colorsList[3] = 'yellow' print(colorsList)

Iziphumo:

['red', 'green', 'blue', 'yellow']

Ungalujikeleza njani uLuhlu

Singavula uluhlu ngokusebenzisa i- for loop.


colorsList = ['red', 'green', 'blue', 'orange'] for i in colorsList:
print(i)

Iziphumo:

red green blue orange

Ungazongeza njani izinto kuluhlu

Zimbini iindlela zokongeza uluhlu kuLuhlu kwiPython, _ _ _ _ | kunye append()

I insert() indlela yongeza izinto ukuphela koluhlu:

append()

Iziphumo:


colorsList = ['red', 'green', 'blue', 'orange'] colorsList.append('yellow') print(colorsList)

I ['red', 'green', 'blue', 'orange', 'yellow'] indlela yongeza into kwisalathiso esichaziweyo:

insert()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.insert(2, 'yellow') print(colorsList)

Ungazisusa njani izinto kuluhlu

Ungasusa izinto kuluhlu usebenzisa izinto ezininzi:

['red', 'green', 'yellow', 'blue', 'orange'] isusa into echaziweyo


remove()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.remove('orange') print(colorsList)

['red', 'green', 'blue'] isusa into kwisalathiso esikhankanyiweyo okanye isuse into yokugqibela ukuba akukho salathiso sinikezelweyo

pop()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.pop(1) print(colorsList) ['red', 'blue', 'orange']

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.pop() print(colorsList)

['red', 'grenn', 'blue'] isusa into kwisalathiso esikhankanyiweyo okanye isuse lonke uluhlu

del()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] del colorList[1] print(colorsList) ['red', 'blue', 'orange']

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] del colorList print(colorsList)

Traceback (most recent call last): File 'pythonList.py', line 30, in
print(colorsList) NameError: name 'colorsList' is not defined
ukhupha uluhlu

clear()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorList.clear() print(colorsList)

Ufumana njani ubude boludwe

Ungabufumana ubude boluhlu ngokubiza [] umsebenzi, o.k.t.

len()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] print(len(colorsList))

Bala inani lezinto ezichaziweyo

Singasebenzisa 4 Umsebenzi kuluhlu ukufumana inani lezinto ezichaziweyo kuluhlu. Umzekelo:

count()

Iziphumo:

colorsList = ['red', 'green', 'red', 'orange'] print(colorsList.count('red'))

Uhlela njani izinto zoluhlu

Kule meko, 2 Umsebenzi udwelisa uluhlu ngokwee-alfabhethi.

sort()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.sort() print(colorsList)

Hlela ngokulandelelana

['blue', 'green', 'orange', 'red']

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.sort(reverse=True) print(colorsList)

Ungazibuyisela njani umva izinto kuLuhlu

Singasebenzisa, i ['red', 'orange', 'green', 'blue'] Umsebenzi wokuguqula uluhlu, o.k.t.

reverse()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] colorsList.reverse() print(colorsList)

UngaluKhuphela njani uluhlu kuLunye uLuhlu

Singasebenzisa ['orange', 'blue', 'green', 'red'] Umsebenzi wokukopa imixholo yoluhlu ukuya kolunye uluhlu.

copy()

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] newList = colorsList.copy() print(newList)

Indlela yokujoyina uLuhlu ezimbini kunye

Eyona ndlela ilula yokudibanisa uluhlu ezimbini kukusebenzisa ['red', 'green', 'blue', 'orange'] umqhubi. Umzekelo:

+

Iziphumo:

colorsList = ['red', 'green', 'blue', 'orange'] numbersList = [1, 2, 3, 4] numbersAndColors = colorsList + numbersList print(numbersAndColors)