logo

Tyhjä Tuple Python

Mitä ovat Tuples Pythonissa?

Tuple on muuttumattomien, tilattujen esineiden järjestely. Koska sekä monikot että Python-listat ovat sekvenssejä, ne ovat analogisia. Monikot ja listat vaihtelevat, koska emme voi muokata monikoita; voimme kuitenkin muuttaa luetteloita alustuksen jälkeen. Lisäksi rakennamme monikot suluilla, kun taas luetteloita hakasulkeilla.

Tuple luodaan laittamalla eri arvot sulkujen sisään pilkuilla erotettuina. Esimerkiksi,

Esimerkki Tuplesta

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

Voit luoda tyhjän tuple-objektin antamalla mitään elementtejä suluissa tehtäväkäskyssä. Pythonin sisäänrakennettu funktio tuple() luo myös tyhjän tuple-objektin, kun sitä kutsutaan ilman argumentteja.

Koodi

linkitetty lista
 # Python program to show how to create an empty tuple T1 = () print(T1) T2 = tuple() print(T2) 

Lähtö:

 () () 

Kuinka tarkistaa tyhjennys Pythonissa?

Voit luoda tyhjän monikon sijoittamalla mitään komponentteja suluihin tehtävälauseessa. Sisäänrakennettu menetelmä tuple() luo myös tyhjän tuple-objektin, kun sitä kutsutaan välittämättä argumentteja.

Ei operaattorin käyttäminen

Koodi

 # 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) 

Lähtö:

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

Koodi

täyden summaimen totuustaulukko
 # 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) 

Lähtö:

 The given tuple is empty () 

Tyhjä monikko nimeltä 'my tuple' alustettiin yllä olevassa tapauksessa. Tuplen pituus määritettiin sitten käyttämällä sisäänrakennettua Python-funktiota len() ja tallennettiin muuttujan nimellä 'len_tuple'. My_tuplen pituus tarkistettiin sitten if-lauseella nähdäkseen, oliko se nolla.

Monikko katsotaan tyhjäksi, jos ehto on totta. Muutoin monikko ei ole tyhjä.

Tuplen vaihtaminen tyhjäksi ketjuksi

Oletetaan, että meillä on monikko, jossa on elementtejä. Meidän on vaihdettava se tyhjäksi ketjuksi. Katsotaanpa, miten tämä tehdään.

Koodi

javascript trim alimerkkijono
 # 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_) 

Lähtö:

 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:- () 

Vertailu toiseen tyhjään tuplaan

Näemme tulokset, jos vertaamme kahta monikkoa

Koodi

 # 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') 

Lähtö:

 my_tuple1 is not empty