Python- tutoriaal

Python TUIS Python Intro Python Begin Python-sintaksis Python-kommentaar Python veranderlikes Python-datatipes Python-nommers Python Casting Python Strings Python Booleans Python-operateurs Python-lyste Python Tuples Python-stelle Python Woordeboeke Python As...Anders Python While Loops Python vir lusse Python-funksies Python Lambda Python-skikkings Python-klasse/-voorwerpe Python Erfenis Python Iterators Python-omvang Python-modules Python-datums Python Wiskunde Python JSON Python RegEx Python PIP Python Probeer ... Behalwe Python-gebruikersinvoer Python String Formatering

Lêerhantering

Python-lêerhantering Python Lees lêers Python Skryf/skep lêers Python verwyder lêers

Python-modules

NumPy Tutoriaal Panda Walkthrough Scipy Tutoriaal

Python Matplotlib

Matplotlib Intro Matplotlib Begin Matplotlib Pyplot Matplotlib Plotte Matplotlib Merkers Matplotlib-lyn Matplotlib-etikette Matplotlib-rooster Matplotlib Subplotte Matplotlib Scatter Matplotlib Bars Matplotlib Histogramme Matplotlib sirkeldiagramme

Masjienleer

Aan die gang kom Gemiddelde mediaanmodus Standaard afwyking Persentiel Dataverspreiding Normale dataverspreiding Strooi plot Lineêre regressie Polinoomregressie Meervoudige regressie Skaal Trein/toets Besluitboom

Python MySQL

MySQL Begin MySQL Skep databasis MySQL Skep tabel MySQL-insetsel MySQL Kies MySQL Waar MySQL Bestel deur MySQL verwyder MySQL Drop Table MySQL-opdatering MySQL-limiet MySQL Sluit aan

Python MongoDB

MongoDB Begin MongoDB Skep databasis MongoDB Skep versameling MongoDB-insetsel MongoDB Vind MongoDB-navraag MongoDB Sorteer MongoDB verwyder MongoDB Drop Collection MongoDB-opdatering MongoDB-limiet

Python-verwysing

Python Oorsig Python ingeboude funksies Python-stringmetodes Python Lys Metodes Python Woordeboek Metodes Python Tuple Metodes Python Stel metodes Python-lêermetodes Python sleutelwoorde Python-uitsonderings Python Woordelys

Moduleverwysing

Ewekansige module Versoeke Module Statistiek Module Wiskunde Module cMath-module

Python Hoe om

Verwyder lys duplikate Draai 'n snaar om Voeg twee getalle by

Python voorbeelde

Python voorbeelde Python-samesteller Python-oefeninge Python Vasvra Python-sertifikaat

Python - Dateer Tuples op


Tupels is onveranderlik, wat beteken dat jy nie items kan verander, byvoeg of verwyder sodra die tupel geskep is nie.

Maar daar is 'n paar oplossings.


Verander Tuple-waardes

Sodra 'n tupel geskep is, kan jy nie sy waardes verander nie. Tupels is onveranderlik , of onveranderlik soos dit ook genoem word.

Maar daar is 'n oplossing. Jy kan die tupel in 'n lys omskep, die lys verander en die lys weer in 'n tupel omskep.

Voorbeeld

Omskep die tupel in 'n lys om dit te kan verander:

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)

Voeg items by

Aangesien tupels onveranderlik is, het hulle nie 'n ingeboude append()metode nie, maar daar is ander maniere om items by 'n tupel te voeg.

1. Skakel om in 'n lys : Net soos die oplossing vir die verandering van 'n tupel, kan jy dit omskep in 'n lys, jou item(s) byvoeg en dit terug omskep in 'n tupel.

Voorbeeld

Skakel die tupel om in 'n lys, voeg "oranje" by en skakel dit terug in 'n tupel:

thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)

2. Voeg tupel by 'n tupel . Jy word toegelaat om tupels by tupels te voeg, so as jy een item, (of baie) wil byvoeg, skep 'n nuwe tupel met die item(s) en voeg dit by die bestaande tupel:

Voorbeeld

Skep 'n nuwe tupel met die waarde "oranje", en voeg daardie tupel by:

thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y

print(thistuple)

Let wel: Wanneer jy 'n tupel met net een item skep, onthou om 'n komma na die item in te sluit, anders sal dit nie as 'n tupel geïdentifiseer word nie.



Verwyder items

Let wel: Jy kan nie items in 'n tupel verwyder nie.

Tupels is onveranderlik , so jy kan nie items daaruit verwyder nie, maar jy kan dieselfde oplossing gebruik as wat ons gebruik het vir die verandering en byvoeging van tuple items:

Voorbeeld

Skakel die tupel om in 'n lys, verwyder "appel" en skakel dit terug in 'n tupel:

thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)

Of jy kan die tupel heeltemal uitvee:

Voorbeeld

Die delsleutelwoord kan die tupel heeltemal uitvee:

thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists