logo

Leeres Tupel-Python

Was sind Tupel in Python?

Ein Tupel ist eine Anordnung unveränderlicher, geordneter Elemente. Da sowohl Tupel als auch Python-Listen Sequenzen sind, sind sie analog. Tupel und Listen variieren jedoch, da wir Tupel nicht bearbeiten können; Allerdings können wir Listen nach der Initialisierung ändern. Darüber hinaus erstellen wir Tupel mithilfe von Klammern, während wir Listen mithilfe eckiger Klammern erstellen.

Ein Tupel wird erstellt, indem unterschiedliche Werte durch Kommas getrennt in die Klammern eingefügt werden. Zum Beispiel,

Beispiel eines Tupels

 1. tuple_1 = ('Tuples', 'Lists', 'immutable', 'Mutable') 2. tuple_2 = (3, 5, 7, 2, 6, 7) 3. tuple_3 = 'Tuples', 'Lists', 'immutable', 'Mutable' 

Sie können ein leeres Tupelobjekt erstellen, indem Sie in einer Zuweisungsanweisung keine Elemente in Klammern angeben. Die integrierte Funktion von Python, tuple(), erstellt auch ein leeres Tupelobjekt, wenn sie ohne Argumente aufgerufen wird.

Code

Prioritätswarteschlange C++
 # Python program to show how to create an empty tuple T1 = () print(T1) T2 = tuple() print(T2) 

Ausgabe:

 () () 

Wie überprüfe ich leere Tupel in Python?

Sie können ein leeres Tupel generieren, indem Sie in der Zuweisungsphrase keine Komponenten in Klammern setzen. Die eingebaute Methode tuple() erstellt auch ein leeres Tupelobjekt, wenn sie ohne Übergabe von Argumenten aufgerufen wird.

ml bis oz

Verwendung des Not-Operators

Code

 # Python program to check if the tuple is empty using not in operator # Creating an empty tuple my_tuple = () # Using the 'not' operator if not my_tuple: print ('The given tuple is empty') else: print ('The given tuple is not empty') # Printing our tuple print(my_tuple) 

Ausgabe:

 The given tuple is empty () Using the len() Function 

Code

 # Python program to check if the tuple is empty using the length function # Creating an empty tuple my_tuple = () # Using len() function len_tuple = len(my_tuple) # Using the if-else Statements if len_tuple == 0: print ('The given tuple is empty') else: print ('The given tuple is not empty') # Printing our tuple print(my_tuple) 

Ausgabe:

 The given tuple is empty () 

Im obigen Beispiel wurde ein leeres Tupel namens „mein Tupel“ initialisiert. Die Länge des Tupels wurde dann mit der eingebauten Python-Funktion len() ermittelt und im Variablennamen „len_tuple“ gespeichert. Anschließend wurde die Länge von my_tuple mithilfe einer if-Anweisung überprüft, um festzustellen, ob sie gleich Null war.

Das Tupel gilt als leer, wenn die Bedingung wahr ist. Ansonsten gilt das Tupel als nicht leer.

Array Java

Ein Tupel in ein leeres Tupel ändern

Nehmen wir an, wir haben ein Tupel, das Elemente enthält. Wir müssen es in ein leeres Tupel ändern. Lassen Sie uns sehen, wie das geht.

Code

vergleichbares Java
 # Python program to see how to convert a tuple to an empty tuple #creating a tuple tuple_ = 'a', 3, 'b', 'c', 'd', 'e', 'g', 's', 'k', 'v', 'l' print('Original tuple: ', tuple_) #tuples in Python are immutable objects; therefore, we cannot remove items from a tuple #We can use merging of the tuples to remove an element from the tuple tuple_ = tuple_[:4] + tuple_[5:] print('After removing a single item:- ', tuple_) # Method to remove all the elements from the tuple #Converting our tuple into a Python List list_ = list(tuple_) # Creating a for loop to delete all the elements of the list for i in range(len(list_)): list_.pop() #converting the list back to a tuple tuple_ = tuple(list_) print('New empty tuple:- ', tuple_) 

Ausgabe:

 Original tuple: ('a', 3, 'b', 'c', 'd', 'e', 'g', 's', 'k', 'v', 'l') After removing a single item:- ('a', 3, 'b', 'c', 'e', 'g', 's', 'k', 'v', 'l') New empty tuple:- () 

Vergleich mit einem anderen leeren Tupel

Wir werden die Ergebnisse sehen, wenn wir zwei Tupel vergleichen

Code

 # Python program to compare two tuples # Creating an empty tuple my_tuple = ( ) # Creating a second tuple my_tuple1 = ('Python', 'Javatpoint') # Comparing the tuples if my_tuple == my_tuple1: print('my_tuple1 is empty') else: print('my_tuple1 is not empty') 

Ausgabe:

 my_tuple1 is not empty