logo

Prazni tuple Python

Što su tuple u Pythonu?

Torka je raspored nepromjenjivih, uređenih stavki. Budući da su i tuple i Python liste nizovi, oni su analogni. Tuple i liste, međutim, variraju jer ne možemo uređivati ​​tuples; međutim, popise možemo promijeniti nakon što ih inicijaliziramo. Osim toga, gradimo torke pomoću zagrada, dok radimo liste pomoću uglatih zagrada.

Torka se stvara stavljanjem različitih vrijednosti unutar zagrada, odvojenih zarezima. Na primjer,

Primjer torke

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

Prazan objekt tuple možete stvoriti tako da u izjavi o dodjeli ne date elemente u zagradama. Ugrađena funkcija Pythona, tuple(), također stvara prazan objekt tuple kada se pozove bez ikakvih argumenata.

Kodirati

veličina teksta od lateksa
 # Python program to show how to create an empty tuple T1 = () print(T1) T2 = tuple() print(T2) 

Izlaz:

 () () 

Kako provjeriti praznu tuple u Pythonu?

Možete generirati praznu torku tako da u izrazu dodjele ne postavite nijednu komponentu u zagradu. Ugrađena metoda tuple() također stvara prazan objekt tuple kada se pozove bez prosljeđivanja argumenata.

string java

Korištenje operatora not

Kodirati

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

Izlaz:

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

Kodirati

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

Izlaz:

 The given tuple is empty () 

Prazna tuple pod nazivom 'moja tuple' inicijalizirana je u gornjoj instanci. Duljina tuple je zatim određena pomoću ugrađene Python funkcije len() i spremljena u nazivu varijable 'len_tuple.' Duljina my_tuple je zatim provjerena pomoću if naredbe da se vidi je li jednaka nuli.

Torka se smatra praznom ako je uvjet istinit. U protivnom se torka smatra praznom.

pandino stožer

Promjena tuple u praznu tuple

Pretpostavimo da imamo torku koja ima elemente u sebi. Moramo ga promijeniti u praznu torku. Pogledajmo kako to učiniti.

Kodirati

vrste spajanja u rdbms
 # 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_) 

Izlaz:

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

Uspoređivanje s još jednom praznom torkom

Rezultate ćemo vidjeti ako usporedimo dvije torke

Kodirati

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

Izlaz:

 my_tuple1 is not empty