logo

Tuščias „Tuple Python“.

Kas yra „Tuples“ Python?

Kortelė – tai nekintančių, užsakytų daiktų išdėstymas. Kadangi ir eilės, ir Python sąrašai yra sekos, jie yra analogiški. Tačiau eilės ir sąrašai skiriasi, nes negalime redaguoti kortelių; tačiau sąrašus galime pakeisti juos inicijavę. Be to, eilutes sudarome naudodami skliaustus, o sąrašus sudarome naudodami laužtinius skliaustus.

Eilė sukuriama įdedant skirtingas reikšmes į skliaustelius, atskirtas kableliais. Pavyzdžiui,

Tupelės pavyzdys

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

Galite sukurti tuščią kortelių objektą priskyrimo sakinyje nepateikdami jokių elementų skliausteliuose. Integruota Python funkcija tuple() taip pat sukuria tuščią kortelių objektą, kai jis iškviečiamas be jokių argumentų.

Kodas

skirtumas tarp tigro ir liūto
 # Python program to show how to create an empty tuple T1 = () print(T1) T2 = tuple() print(T2) 

Išvestis:

 () () 

Kaip patikrinti tuščią stulpelį „Python“?

Tuščią seką galite sugeneruoti priskyrimo frazėje nepadėdami jokių komponentų skliausteliuose. Integruotas metodas tuple() taip pat sukuria tuščią kortelių objektą, kai iškviečiamas neperduodant jokių argumentų.

Naudojant ne operatorių

Kodas

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

Išvestis:

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

Kodas

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

Išvestis:

 The given tuple is empty () 

Aukščiau pateiktame pavyzdyje buvo inicijuota tuščia seka, vadinama „mano seka“. Tada rinkinio ilgis buvo nustatytas naudojant integruotą Python funkciją len() ir įrašytas kintamojo pavadinimu „len_tuple“. Tada my_tuple ilgis buvo patikrintas naudojant if teiginį, siekiant išsiaiškinti, ar jis lygus nuliui.

Eilė laikoma tuščia, jei sąlyga yra teisinga. Kitu atveju korta nelaikoma tuščia.

Kortelės keitimas į tuščią eilutę

Tarkime, kad turime seką, kurioje yra elementų. Turime jį pakeisti į tuščią eilutę. Pažiūrėkime, kaip tai padaryti.

Kodas

apibrėžti kompiuterį
 # 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_) 

Išvestis:

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

Palyginimas su kita tuščia kortele

Rezultatus pamatysime, jei palyginsime dvi korteles

Kodas

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

Išvestis:

 my_tuple1 is not empty