Tuple
- Tuple Python का एक ordered (क्रमबद्ध) और immutable (अपरिवर्तनीय) data type है। इसका उपयोग एक ही variable में कई values को store करने के लिए किया जाता है।
- Tuple में data round brackets
()के अंदर लिखा जाता है। - Tuple के elements का क्रम (order) हमेशा बना रहता है।
- Tuple बनने के बाद उसके elements को बदला, जोड़ा या हटाया नहीं जा सकता है।
Syntax :
tuple_name = (item1, item2, item3)
Characteristics of Tuple
| विशेषता | विवरण |
|---|---|
| Ordered | Elements का क्रम हमेशा एक जैसा रहता है। |
| Immutable | Tuple बनने के बाद उसमें बदलाव नहीं किया जा सकता। |
| Allows Duplicate Values | एक ही value कई बार हो सकती है। |
| Indexed | प्रत्येक element का index होता है, जो 0 से शुरू होता है। |
| Faster | List की तुलना में Tuple थोड़ा तेज़ होता है। |
| Multiple Data Types | एक ही Tuple में अलग-अलग data types रख सकते हैं। |
Creating Tuple in Python
Tuple के आखिरी में semi-colon दे या न दे इससे कोई फर्क नहीं पड़ता है |
Source Code :
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 ( () )
Tuple बिना parenthesis के भी हो सकते है |
Source Code :
tuple = "H", "e", "l", "l", "o"
print(type(tuple))
#Output : <class 'tuple'>
Tuple Positive indexing
Positive Indexing में indexing 0 (शून्य) से शुरू होती है। और पहले element का index 0, दूसरे का 1, तीसरे का 2 होता है।
Example :
fruits = ("Apple", "Banana", "Mango", "Orange", "Grapes")
print(fruits[0]) # Apple
print(fruits[1]) # Banana
print(fruits[2]) # Mango
print(fruits[3]) # Orange
print(fruits[4]) # Grapes
Negative Indexing
Negative Indexing में indexing -1 से शुरू होती है। अर्थात, आखिरी (Last) element का index -1, उससे पहले वाले का -2, फिर -3 होता है।
Example :
fruits = ("Apple", "Banana", "Mango", "Orange", "Grapes")
print(fruits[-1]) # Grapes
print(fruits[-2]) # Orange
print(fruits[-3]) # Mango
print(fruits[-4]) # Banana
print(fruits[-5]) # Apple
Accessing Tuple
Accessing Tuple में Tuple के items को access किया जा सकता है |
Syntax :
tuple(index)
Source Code :
tuple = (1, 2, "H", 2.8)
print(tuple[0])
print(tuple[1])
print(tuple[2])
print(tuple[3])
Invalid Indexing
अगर invalid index दिया जाता है तो ‘indexError’ का exception आ जाता है।
Source Code :
tuple = (1, 2, "Η", 2.8)
print(tuple[10])
Output :
print(tuple(10))
IndexError: tuple index out of range
Accessing Nested Tuple
Nested Tuple वह Tuple होता है जिसके अंदर एक या अधिक Tuple मौजूद होते हैं।
अर्थात, जब एक Tuple के भीतर दूसरा Tuple रखा जाता है, तो उसे Nested Tuple कहा जाता है।
Source Code :
tuple = (1, 2, "Hello", ("R", "U"))
print(tuple[3][0]) #Output : R
print(tuple[3][1]) #Output : U
Tuple Cannot change but Reassign
Tuple को change नहीं कर सकते हैं लेकिन उसे re-assign किया जा सकता है |
Source Code :
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 नहीं किया जा सकता है |
Source Code :
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 :
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]) #()
अगर colon (:) के left side का index invalid होता है तो blank tuple (()) return होती है और 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 items
Python में Tuple की Length ज्ञात करने के लिए len() Function का उपयोग किया जाता है।
len() Function Tuple के अंदर मौजूद सभी Elements की संख्या (Count) Return करता है।
Source Code :
tuple = (1, 2, "H", 2.8)
print (len(tuple))
#Output : 4
Tuple Concatenation
Tuple Concatenation का अर्थ है दो या दो से अधिक Tuples को जोड़कर (Combine करके) एक नया Tuple बनाना के लिए किया जाता है।
Python में Tuple Concatenation के लिए Addition Operator (+) का उपयोग किया जाता है।
जब दो Tuples को + Operator से जोड़ा जाता है, तो उनके सभी Elements क्रम (Order) में मिलकर एक नया Tuple बनाते हैं।
Source Code :
tuple = (1, 2, "H", 2.8) + ("Hello", (2, 8))
print (tuple)
#Output : (1, 2, 'H', 2.8, 'Hello', (2, 8))
Tuple Repetition
Tuple Repetition का अर्थ है किसी Tuple के Elements को एक या अधिक बार दोहराना (Repeat करना) के लिए किया जाता है।
Python में Tuple को Repeat करने के लिए Multiplication Operator (*) का उपयोग किया जाता है।
यदि किसी Tuple को n से गुणा (*) किया जाता है, तो उसके सभी Elements n बार दोहराए जाते हैं।
Source Code :
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
Iterating Tuple का अर्थ है Tuple के प्रत्येक Element को एक-एक करके Access करने के लिए किया जाता है।
Python में Tuple के सभी Elements को पढ़ने के लिए for Loop का उपयोग किया जाता है।
for Loop प्रत्येक Element को क्रम (Order) से Access करता है और Loop समाप्त होने तक सभी Elements पर चलता है।
Source Code :
tuple = (1, 2, "H", 2.8)
for i in tuple:
print(i)
#Output :
#1
#2
#H
#2.8
Tuple function in Python
len() | Tuple में कुल Elements की संख्या बताता है। | len(tuple) | len((10,20,30)) | 3 |
max() | Tuple की सबसे बड़ी Value Return करता है। (केवल समान प्रकार के Data पर) | max(tuple) | max((10,20,30,40)) | 40 |
min() | Tuple की सबसे छोटी Value Return करता है। | min(tuple) | min((10,20,30,40)) | 10 |
sum() | सभी Numeric Values का योग (Sum) Return करता है। | sum(tuple) | sum((10,20,30)) | 60 |
sorted() | Tuple को Sort करके List Return करता है। | sorted(tuple) | sorted((30,10,20)) | [10, 20, 30] |
tuple() | किसी Iterable (List, String आदि) को Tuple में Convert करता है। | tuple(iterable) | tuple([1,2,3]) | (1, 2, 3) |
any() | यदि Tuple में कम से कम एक Value True है, तो True Return करता है। | any(tuple) | any((0, False, 5)) | True |
all() | यदि Tuple की सभी Values True हैं, तो True Return करता है। | all(tuple) | all((1,2,3)) | True |
enumerate() | प्रत्येक Element के साथ उसका Index Return करता है। | enumerate(tuple) | list(enumerate(("A","B","C"))) | [(0,'A'), (1,'B'), (2,'C')] |
reversed() | Tuple के Elements को उल्टे क्रम (Reverse Order) में Return करता है। | reversed(tuple) | tuple(reversed((10,20,30))) | (30,20,10) |
