Tuple भी list की तरह एक order of sequence item है Tuple और list में difference यह है कि tuple immutable होते हैं immutable का मतलब यह है कि हम data type के items को एक बार value देने के बाद user उन values को update नहीं कर सकता है Tuple के सभी items को parenthesis(()) के अन्दर close किये जाता है।
Creating Tuple in Python
Tuple के आखिर में semi-colon(;) लगाये ना लगाये इससे किसी भी प्रकार का कोई भी फर्क नहीं पड़ता है |
tuple1 = (1, 2, 3, 4, 5) #Integer Tuple tuple2 = (1.5, 2.4, 3.8, 4.4, 5.7); #Float Tuple tuple3 = (1, 2, "Hello", 4.5); #Mixed Data Types Tuple tuple4 = (1, 2, (1, 2), 4.5) #Nested Tuple
Tuple Without Parenthesis(())
Tuples बिना parenthesis के भी हो सकते है |
tuple = "H", "e", "l", "l", "o" print(type(tuple)) #Output: <class 'tuple'>
Tuple Positive Indexing
सभी Programming Language में index की शुरुआत ‘0’ से होती है उसी तरह से Python में Tuple के पहले item का index ‘0’ होता है |
strTuple = ("H", "e", "l", "l", "o")
ItemsNumber | item1 | item2 | item3 | item4 | item5 |
---|---|---|---|---|---|
Index | 0 | 1 | 2 | 3 | 4 |
Items | “H” | “e” | “l” | “l” | “o” |
Tuple Negative Indexing
Python में Tuple के आखिरी index ‘-1’ से शुरू होता है |
strTuple = ("H", "e", "l", "l", "o")
ItemsNumber | item1 | item2 | item3 | item4 | item5 |
---|---|---|---|---|---|
Index | -5 | -4 | -3 | -2 | -1 |
Items | “H” | “e” | “l” | “l” | “o” |
Accesing Tuple
index से Tuple के items को access किया जा सकता है |
Syntax :
tuple(index)
Example:
tuple = (1, 2, "H", 2.8) print(tuple[0]) print(tuple[1]) print(tuple[2]) print(tuple[3])
Output:
1 2 H 2.8
Invalid Indexing
अगर invalid index दिया जाता है तो ‘indexError’ का exception आ जाता है |
Example:
tuple = (1, 2, "H", 2.8) print(tuple[10])
Output :
print(tuple(10)) IndexError: tuple index out of range
Accessing Nested Tuple
tuple = (1, 2, "Hello", ("R", "U")) print(tuple[3][0]) #Output : R print(tuple[3][1]) #Output : U
Tuples Cannot Change But Reassign
tuple को हम change नहीं कर सकते लेकिन उसे re-assign किया जा सकता है |
tuple = (1, 2, 3, 4, 5) print(tuple) #Output : (1, 2, 3, 4, 5) tuple = (6, 7, 8, 9, 10) print(tuple) #Output : (6, 7, 8, 9, 10)
Deleting Tuple
‘del’ Operator से पूरा tuple delete किया जा सकता है लेकिन tuple के एक-एक item को delete नहीं किया जा सकता |
tuple = (1, 2, 3, 4, 5) del tuple[0] #Output : # del tuple[0] #TypeError: 'tuple' object doesn't support item deletion
tuple = (1, 2, 3, 4, 5) del tuple print(tuple) #Output : <class 'tuple'>
Convert Tuple to Sub-Tuple or Tuple Slicing
colon(:) के left में sub-Tuple कहा से start करना है और colon(:) के right में कहा पर end करना है वो दिया जाता है |
Slicing के लिए colon को ‘slicing operator’ कहा जाता है |
Syntax :
tuple(start:end)
Example:
tuple = (1, 2, "H", 2.8) print(tuple[0:1]) # (1) print(tuple[0:2]) # (1, 2) print(tuple[1:3]) # (2, 'H', 2.8) print(tuple[3:1]) #()
Output:
(1) (1, 2) (2, 'H', 2.8) ()
अगर colon(:) के left side का index invalid होता है तो blank tuple(()) return होतe है और colon(:) के right side का index invalid होता है तो Tuple के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank tuple(()) return होता है |
Example:
tuple = (1, 2, "H", 2.8) print(tuple[0:1]) # (1) print(tuple[0:2]) # (1, 2) print(tuple[1:3]) # (2, 'H') print(tuple[1:15]) # (2, 'H', 2.8) print(tuple[10:4]) # ()
Check Length of Tuple’s Items
tuple = (1, 2, "H", 2.8) print (len(tuple)) #Output : 4
Tuple Concatenation
tuple = (1, 2, "H", 2.8) + ("Hello", (2, 8)) print (tuple) #Output : (1, 2, 'H', 2.8, 'Hello', (2, 8))
Tuple Repetition
tuple = (1, 2, "H", 2.8) * 3 print (tuple) #Output : (1, 2, 'H', 2.8, 1, 2, 'H', 2.8, 1, 2, 'H', 2.8)
Iterating Tuple Items using For Loop
tuple = (1, 2, "H", 2.8) for i in tuple: print(i) #Output : #1 #2 #H #2.8
Tuple Functions in Python
Tuple Function | Description |
---|---|
all() | sequence के सभी items True होते है तो ये तो ये True return करता है | |
any() | sequence का एक या सभी items True होते है तो ये तो ये True return करता है | |
enumerate() | दिए गए start से index और उसकी value की pair return करता है | |
len() | दिए गए tuple की length; number में return करता है | |
max() | दिए गए tuple से max value को return करता है | |
min() | दिए गए tuple से min value को return करता है | |
sorted() | दिए गए sequence को sort करके return करता है | |
sum() | दिए गए sequence के items को add करके उनका sum return करता है | |
tuple() | sequence को tuple में convert करता है | |
Python Programs के लिये यहाँ click करें।