Python Tuples

MuPython, Tuple muunganidzwa uri akarayira uye isingachinjiki . Izvi zvinoreva kuti hatigone kuwedzera kana kubvisa zvinhu kubva kuTuple.

Isu tinogadzira tuples tichishandisa mabereki () uye imwe chete komma ( , ).

Tuples inogona kuiswa indexed uye yakatemwa senge zvinyorwa, kunze kwekuti mhedzisiro yechidimbu ichave zvakare Tuple.

Maitiro Ekugadzira Tuple

colorsTuple = ('red', 'green', 'blue') print(colorsTuple)

Kubuda:

('red', 'green', 'blue')

Kugadzira Chidimbu Chine Chinhu Chete Chete

Tuples inoda imwe chete komma, saka kuti ugadzire Tuple ine chinhu chimwe chete, wawedzera comma mushure mechinhu. Semuyenzaniso:

colorsTuple = ('red',)

Maitiro Ekuwana Zvinhu zveTuple

Isu tinokwanisa kuwana Tuple zvinhu nekureva kune iyo index nhamba:



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

Kubuda:

blue

Kuwana Range reZvinhu (Kuchekerera)

Tinogona kudoma huwandu hwezvinhu kubva kuTuple nekutsanangudza iyo yekutanga index uye yekupedzisira index. Isu tinoshandisa iyo : Anoshanda.

Ongorora:Iko kukosha kwekudzoka kuchave zvakare Tuple nezvinhu zvakatsanangurwa. colorsTuple = ('red', 'green', 'blue', 'yellow', 'orange', 'white') print(colorsTuple[1:4])

Kubuda:

('green', 'blue', 'yellow')

Yakashata Indexing

Isu tinokwanisa kuwana izvo zvinhu paTuple kubva kumagumo nekutsanangura isina kunaka index index. Semuenzaniso -1 zvinoreva chinhu chekupedzisira uye -2 zvinoreva chinhu chechipiri chekupedzisira.

colorsTuple = ('red', 'green', 'blue', 'yellow', 'orange', 'white') print(colorsTuple[-2])

Kubuda:

orange

Maitiro Ekusunungura kuburikidza neTuple

Tinogona kusunungura kuburikidza neTuple tichishandisa for chiuno.

colorsTuple = ('red', 'green', 'blue', 'orange') for c in colorsTuple:
print(c)

Kubuda:

red green blue orange

Maitiro Ekubvisa Chidimbu

Kudzima Tuple zvachose, shandisa del kiyi

colorsTuple = ('red', 'green', 'blue', 'orange') del colorsTuple print(colorsTuple)

Kubuda

Traceback (most recent call last): File 'pythonTuples.py', line 98, in
print(colorsTuple) NameError: name 'colorsTuple' is not defined

Maitiro Ekuwana Kureba kweTuple

Iwe unogona kuwana iyo Tuple kureba nekudana iyo len() basa, i.k.

colorsTuple = ('red', 'green', 'blue', 'orange') print(len(colorsTuple))

Kubuda:

4

Verenga Nhamba Yezvinhu Zvakatsanangurwa

Tinogona kushandisa count() shanda pane izvo zviviri kuti uwane huwandu hwezviitiko zvechinhu chakatsanangurwa muTuple. Semuyenzaniso:

colorsTuple = ('red', 'green', 'blue', 'orange', 'red') print(colorsTuple.count('red'))

Kubuda:

2

Maitiro Ekujoinha Mapupu maviri Pamwechete

Nzira iri nyore yekubatanidza maviri mapupu pamwechete kushandisa + Anoshanda. Semuyenzaniso:

colorsTuple = ('red', 'green', 'blue', 'orange') numbersTuple = (1, 2, 3, 4) numbersAndColors = colorsTuple + numbersTuple print(numbersAndColors)

Kubuda:

('red', 'green', 'blue', 'orange', 1, 2, 3, 4)