logo

Tuple Python gol

Ce sunt tuplurile în Python?

Un tuplu este un aranjament de elemente imuabile, ordonate. Deoarece atât tuplurile, cât și listele Python sunt secvențe, ele sunt analoge. Cu toate acestea, tuplurile și listele variază, deoarece nu putem edita tupluri; cu toate acestea, putem schimba listele după inițializarea acestora. În plus, construim tupluri folosind paranteze, în timp ce facem liste folosind paranteze drepte.

Un tuplu este creat prin introducerea unor valori diferite în paranteză, separate prin virgule. De exemplu,

Exemplu de tuplu

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

Puteți crea un obiect tuplu gol fără a nu adăuga elemente între paranteze într-o instrucțiune de atribuire. Funcția încorporată a lui Python, tuple(), creează, de asemenea, un obiect tuplu gol atunci când este apelat fără niciun argument.

Cod

parcurgerea în prealabil a unui arbore
 # Python program to show how to create an empty tuple T1 = () print(T1) T2 = tuple() print(T2) 

Ieșire:

 () () 

Cum se verifică tuplele goale în Python?

Puteți genera un tuplu gol prin plasarea niciunei componente între paranteze în fraza de atribuire. Metoda încorporată tuple() creează, de asemenea, un obiect tuple gol atunci când este apelată fără a trece niciun argument.

npm curat cache

Folosind not Operator

Cod

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

Ieșire:

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

Cod

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

Ieșire:

lista java la matrice
 The given tuple is empty () 

Un tuplu gol numit „tuplu meu” a fost inițializat în instanța de mai sus. Lungimea tuplului a fost apoi determinată folosind funcția Python încorporată len() și salvată în numele variabilei „len_tuple”. Lungimea my_tuple a fost apoi verificată folosind o instrucțiune if pentru a vedea dacă este egală cu zero.

Tuplu este considerat gol dacă condiția este adevărată. În caz contrar, tuplul este considerat ca nu este gol.

Schimbarea unui tuplu în tuplu gol

Să presupunem că avem un tuplu care are elemente în el. Trebuie să-l schimbăm într-un tuplu gol. Să vedem cum să facem asta.

Cod

cum functioneaza un calculator
 # 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_) 

Ieșire:

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

Compararea cu un alt tuplu gol

Vom vedea rezultatele dacă comparăm două tupluri

Cod

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

Ieșire:

 my_tuple1 is not empty