Posted By Naveen Singh on 14 Mar 2023
python-programPython में tuple का प्रयोग हम multiple data elements को store करने के लिए करते है। Python में tuple element ordered, unchangeable, और duplicate value allow करते है।
Ordered-: इसका मतलब है की tuple में सारे elements एक defined ordered में होते है, जो की change नही होते है।
Unchangeable-: unchangeable का मतलब है की tuple को एक बार create करने के बाद हम उसमे कोई changes नही कर सकते है।
Duplicate allow-: इसका मतलब है की tuple में element की value repeat हो सकती है।
Python में tuple एक immutable data type होता है, जिसे हम small braces() में लिखते है, इसमें हर एक element को हम comma(,) से separate करते है।
Example-:
newtuple= ("grapes", "mango", "cherry")
print(newtuple)
Output-:
("grapes", "mango", "cherry")
tuple एक non primitive data type होता है, मतलब की tuple different types के data को store करता है।
Example-:
t1 = ("apple", "banana", "cherry")
t2 = (1, 5, 7, 9, 3)
t3 = (True, False, False)
print(t1)
print(t2)
print(t3)
Output-:
('apple', 'banana', 'cherry')
(1, 5, 7, 9, 3)
(True, False, False)
Python में tuple को हम उसके index number के through access कर सकते है।
Example-:
mytuple = ("mango", "banana", "cherry")
print(mytuple[1])
Output-:
banana
Note-: tuple में elements की indexing हमेशा 0 index number से होती है।
Negative Indexing-: tuple में elements को हम उसके negative Indexing के through भी access कर सकते है। जिसका first index number -1 से start होता है।
Example-:
T1 = ("apple", "banana", "mango")
print(T1[-1])
Output-:
mango
Range of indexes-: python में tuple को हम उसके range of indexing के through भी access कर सकते है।
Example-:
mytuple = ("grapes", "pinapple", "guava", "orange", "kiwi", "melon", "mango")
print(mytuple[2:5])
Output-:
(guava", "orange", "kiwi")
जिसमे आपका index number 2 include पर index number 5 include नही होगा।
इसी तरह से हम tuple में elements की negative indexing भी कर सकते है।
Example-:
newtuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(newtuple[-4:-1])
Output-:
('orange', 'kiwi', 'melon')
जिसमे आपका index value -4 include होगा पर index value -1 नही होगा।
हम जानते है की tuple एक immutable data type होता है, जिसे हम change नही कर सकते। कुछ simple steps के through हम tuple को change कर सकते है
1) first step में हम tuple को list में convert करेंगे।
2) second step में हम list को change करेंगे।
3) फिर हम list को वापस से tuple में convert कर देंगे।
Example-:
a = ("apple", "banana", "cherry")
b = list(a)
b[1] = "kiwi"
a = tuple(b)
print(a)
Output-:
("apple", "kiwi", "cherry")
हम जानते है कि tuple एक immutable data type है, जिसमे कोइ built in append() function but python में कुछ techniques है जिसके through हम tuple में elements को add कर सकते है-
1) Convert into a list-: इस method में हम पहले tuple को list में convert करते है फिर हम list में element add करके फिर हम वापस से लिस्ट को tuple में convert कर देते है।
Example-:
addtuple= ("apple", "banana", "cherry")
y = list(addtuple)
y.append("orange")
tuple = tuple(y)
print(addtuple)
Output-:
('apple', 'banana', 'cherry', 'orange')
Add tuple to a tuple-: इस method में आप tuple में element को add करने के लिए एक new tuple create करते है with element और उस tuple को existing tuple में add कर देते है।
Example-:
mytuple = ("mango", "banana", "cherry")
y = ("orange",)
mytuple += y
print(mytuple)
Output-:
('mango', 'banana', 'cherry', 'orange')
Python में tuple unchangeable होते है, जिसमें आप elements को remove नहीं कर सकते पर हम इसमें changing method(tuple to list and list to tuple) use करके elements को remove कर सकते है।
Example-:
mytuple = ("mango", "banana", "cherry")
y = list(mytuple)
y.remove("mango")
mytuple = tuple(y)
print(mytuple)
Output-:
('banana', 'cherry')
del() function के through हम tuple को completely delete कर सकते है।Example-:
mytuple = ("apple", "banana", "cherry")
del mytuple
print(mytuple)
Output-:
Traceback (most recent call last):
File "demo_tuple_del.py", line 3, in <module>
print(mytuple) #this will raise an error because the tuple no longer exists
NameError: name 'mytuple' is not defined
जब भी हम tuple को create करते है, हम उसमे normally value को assign करते है, जिसे हम packing tuple कहते है।
Example-:
tuple = ("apple", "banana", "cherry") # packing tuple का उदाहरण
print(tuple)
Output-:
('apple', 'banana', 'cherry')
हम python में allowed करता है कि हम वापस से values को variable में extract कर सकते है। जिसे हम unpacking tuple कहते है।
Example-:
tuple = ("mango", "banana", "cherry")
(green, yellow, red) = tuple
print(green)
print(yellow)
print(red)
Output-:
mango
banana
cherry
Using Asterisk-: यदि variables की संख्या values की संख्या से कम है, तो आप variable name में एक * जोड़ सकते हैं और values को एक list के रूप में variable को assign कर सकते है।
Example-:
mytuple= ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = mytuple
print(green)
print(yellow)
print(red)
Output-:
apple
banana
['cherry', 'strawberry', 'raspberry']
Python में tuple elements को हम for loop के through access कर सकते है।
Example-:
newtuple = ("mango", "banana", "cherry")
for x in newtuple:
print(x)
Output-:
mango
banana
cherry
Loop through the index number-: loop में हम tuple को उसके index number के through भी access कर सकते है।
range() और len() function का प्रयोग करके iterable बना सकते है।
Example-:
mytuple = ("apple", "mango", "cherry")
for i in range(len(mytuple)):
print(mytuple[i])
Output-:
apple
mango
cherry
Python में हम tuple को while loop के through भी access कर सकते है। tuple की length determine करने के लिए len() function का उपयोग करें, फिर 0 से शुरू करें और tuple element के through अपने index के through loop को refer करें। याद रखे iteraction में index number को one by one increase करते रहे।
Example-:
mytuple = ("mango", "banana", "cherry")
i = 0
while i < len(mytuple):
print(mytuple[i])
i = i + 1
Output-:
mango
banana
cherry
Join two tuple-: python में दो tuples को हम + operator के through join कर सकते है।
Example-:
t1 = ("a", "b" , "c")
t2 = (1, 2, 3)
t3 = t1 + t2
print(t3)
Output-:
('a', 'b', 'c', 1, 2, 3)
Multiply tuple-: यदि आप किसी tuple की content को कई बार multiply करना चाहते हैं, तो आप (*) operator का उपयोग कर सकते हैं|
Example-:
mytuple = ("apple", "banana", "cherry")
newtuple = fruits * 2
print(newtuple)
Output-:
('apple','banana', 'cherry', 'apple', 'banana', 'cherry')
Python में tuple में हम basically two built-in functions का use करते है।
count()-: count()function tuple में कोई element कितनी बार repeat हो रहा है उसे count करके उसकी value को return करता है|
Example-:
mytuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
a = mytuple.count(5)
print(a)
Output-:
2
index()-: यह function tuple में specified element को search करके उसकी position को
return करता है|
Example-:
mytuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
a = mytuple.index(8)
print(a)
Output-:
3
Python में tuple एक immutable data type होता है, जिसे हम small braces() में लिखते है, इसमें हर एक element को हम comma(,) से separate करते है।
Example-:
newtuple= ("grapes", "mango", "cherry")
print(newtuple)
Output-:
("grapes", "mango", "cherry")
tuple एक non primitive data type होता है, मतलब की tuple different types के data को store करता है।
I Hope आपको Python में tuple क्या होता है? tuple python में कैसे work करता है, और tuple के functions के बारे में आपको जानकारी मिली होगी। यदि आपको कोई doubt है तो comment section में comment करके पूछ सकते हैं।