Python-Tupel ()

Mit dem integrierten tuple () können Tupel in Python erstellt werden.

In Python ist ein Tupel ein unveränderlicher Sequenztyp. Eine Möglichkeit zum Erstellen von Tupeln ist die Verwendung des tuple()Konstrukts.

Die Syntax von tuple()lautet:

 Tupel (iterierbar)

tuple () Parameter

  • iterable (optional) - ein iterable (Liste, Bereich usw.) oder ein Iteratorobjekt

Wenn die Iterable nicht übergeben wird, tuple()gibt die Funktion ein leeres Tupel zurück.

Beispiel: Erstellen Sie Tupel mit 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)

Ausgabe

 t1 = () t2 = (1, 4, 6) t1 = ('P', 'y', 't', 'h', 'o', 'n') t1 = (1, 2)

Empfohlene Lektüre: Python Tuples

Interessante Beiträge...