Tuple () inbyggd kan användas för att skapa tuples i Python.
I Python är en tuple en oföränderlig sekvenstyp. Ett av sätten att skapa tuple är att använda tuple()
konstruktionen.
Syntaksen för tuple()
är:
tuple (iterabel)
tuple () Parametrar
- iterabel (valfritt) - en iterabel (lista, intervall etc.) eller ett iteratorobjekt
Om den iterabla inte skickas till tuple()
, returnerar funktionen en tom tupel.
Exempel: Skapa tuplar med tuple ()
t1 = tuple() print('t1 =', t1) # creating a tuple from a list t2 = tuple((1, 4, 6)) print('t2 =', t2) # creating a tuple from a string t1 = tuple('Python') print('t1 =',t1) # creating a tuple from a dictionary t1 = tuple((1: 'one', 2: 'two')) print('t1 =',t1)
Produktion
t1 = () t2 = (1, 4, 6) t1 = ('P', 'y', 't', 'h', 'o', 'n') t1 = (1, 2)
Rekommenderad läsning: Python Tuples