Python में 'Tuple' ये data structure होता है | Tuple ये Elements का sequence होता है और immutable(unchangeable) होता है |
Python में List और Tuple एक जैसे ही होते है लेकिन List के items को change या उसके items delete किये जा सकते है और Tuple के items को change या उसके items delete नहीं किये जा सकते है | Tuples सिर्फ read किये जा सकते है |
Python के tuple में जो elements होते है उसे 'items' कहते है |
Tuple के हर item को comma(,) से seperate किया जाता है और पूरे items को parenthesis(()) के अन्दर close किया जाता है | Tuple में mixed data types भी इस्तेमाल किये जा सकते है |
Syntax :
Source Code :
Slicing के लिए colon को 'slicing operator' कहा जाता है |
Syntax :
अगर colon(:) के left side का index invalid होता है तो blank tuple(()) return होती है और colon(:) के right side का index invalid होता है तो Tuple के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank tuple(()) return होता है |
For Example,
Python में List और Tuple एक जैसे ही होते है लेकिन List के items को change या उसके items delete किये जा सकते है और Tuple के items को change या उसके items delete नहीं किये जा सकते है | Tuples सिर्फ read किये जा सकते है |
Python के tuple में जो elements होते है उसे 'items' कहते है |
Tuple के हर item को comma(,) से seperate किया जाता है और पूरे items को parenthesis(()) के अन्दर close किया जाता है | Tuple में mixed data types भी इस्तेमाल किये जा सकते है |
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)Source Code :
Output :tuple = (1, 2, "H", 2.8) print(tuple[0]) print(tuple[1]) print(tuple[2]) print(tuple[3])
1 2 H 2.8
Invalid Indexing
अगर invalid index दिया जाता है तो 'indexError' का exception आ जाता है |Source Code :
Output :tuple = (1, 2, "H", 2.8) print(tuple[10])
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)Source Code :
Output :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]) #()
(1) (1, 2) (2, 'H', 2.8) ()
अगर colon(:) के left side का index invalid होता है तो blank tuple(()) return होती है और colon(:) के right side का index invalid होता है तो Tuple के left index से आखिरी तक index; return होता है | अगर दोनों ही invalid index होता है तो blank tuple(()) return होता है |
For 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 करता है | |







No comments: