इस Article में, आप Tuple के बारे में सब कुछ जानेंगे। विशेष रूप से, Tuple Kya Hai, Tuple in Python,Tuple Example in Python, Tuples in DBMS in hindi और साथ में जानेंगे Advantage of Tuples.
Tuple Kya Hai
Tuple, elements का एक ordered sequence है, जो immutable elements contain करता है, जिसका अर्थ है कि program के run करने पर Tuple के values को modify नहीं किया जा सकता है। Tuple के elements different data types के हो सकते हैं, example : mix of integers और strings.
Tuple, Data Items को access करने और use करने में easy बनाने के लिए उन्हें grouping और organise करने का एक useful तरीका है। Function से multiple values को return करने के लिए Tuple का use किया जा सकता है। इसी तरह, एक procedure या function में pass होने के लिए disparate items के एक set को एकसाथ Group बनाने के लिए tuple का use किया जा सकता है।
Tuple Functions and Methods (Syntax)
Method | Description | Syntax(example) | Output |
len() | यह tuple की length return करता है, यानी tuple में count of elements. Return करता है। | >>>tp=(5,6,7) >>>len(tp) | 3 |
max() | यह उस tuple से element return करता है जिसका maximum value हो। | >>>tp=(1,50,80) >>>max(tp) | 80 |
min() | यह उस tuple से element return करता है जिसका minimum value हो। | >>>tp =(7,2,5) >>>min(tp) | 2 |
index() | यह Tuple के existing element का index return करता है। | >>>p1=(3,4,5,6) >>>p1.index(5) | 2 |
count() | यह method किसी given tuple में element के member की count return करता है। | >>>p1=(2,3,4,5,6,2,7,8,2) >>>p1.count(2) | 3 |
tuple() | यह एक constructor है जिसका use different types के values से types बनाने के लिए किया जा सकता है। | >>>t= tuple(“xyz”) >>>t | (‘x’, ‘y’, ‘z’) |
Tuple Kya Hai – Tuple In Python:
“Python tuples एक data structure है जो values के ordered sequence को store करता है। Tuples immutable होते है। इसका मतलब है कि आप values को Tuples में नहीं change कर सकते। Tuples को parenthesis के साथ define किया गया है।”
Tuple Kya Hai – Creating a Tuple
सभी items (elements) को parentheses () के अंदर रखकर, commas से अलग करके एक tuple बनाया जाता है। parentheses optional होता है, हालांकि, उसका use करना ही सही होगा है।
एक tuple में कितने भी numbers के items हो सकते हैं और वे different types के हो सकते हैं (integer, float, list, string, etc.)
सभी items (elements) को parentheses () के अंदर रखकर, commas से अलग करके एक tuple बनाया जाता है। parentheses optional होता है, हालांकि, उसका use करना ही सही होगा है।
एक tuple में कितने भी numbers के items हो सकते हैं और वे different types के हो सकते हैं (integer, float, list, string, etc.)
Example :
# Different types of tuples
# Empty tuple
new_tuple = ()
print(new_tuple)
# Tuple having integers
new_tuple = (10, 20, 30)
print(new_tuple)
# tuple with mixed datatypes
new_tuple = (10, “Example”, 7.5)
print(new_tuple)
# nested tuple
new_tuple = (“cursor”, [80, 40, 60], (10, 20, 30))
print(new_tuple)
Output :
()
(10, 20, 30)
(10, ‘Example’, 7.5)
(‘cursor’, [80, 40, 60], (10, 20, 30))
NB : Parentheses का use किए बिना भी Tuple बनाया जा सकता है। इसे tuple packing के रूप में जाना जाता है।
Example:
# Different types of tuples
new_tuple = 30, 7.5, “python”
print(new_tuple)
# example of tuple unpacking
x, y, z = new_tuple
print(x) # 30
print(y) # 7.5
print(z) # python
OUTPUT :
(30, 7.5, ‘python’)
30
7.5
python
एक Element के साथ एक tuple बनाना थोड़ा tricky होता है।
Parentheses में एक element होना enough नहीं होता। हमें यह indicate करने के लिए एक trailing comma की आवश्यकता होती है कि यह वास्तव में एक tuple है।
Example :
new_tuple = (“Example”)
print(type(new_tuple)) # <class ‘str’>
# Creating a tuple with one element
new_tuple = (“hExample”,)
print(type(new_tuple)) # <class ‘tuple’>
# here Parentheses is optional
new_tuple = “Example”,
print(type(new_tuple)) # <class ‘tuple’>
OUTPUT :
<class ‘str’>
<class ‘tuple’>
<class ‘tuple’>
Tuple Kya Hai – Access of Tuple Elements:
बहुत सारे तरीके हैं जिनसे हम Tuple के elements को access कर सकते है। जैसे की :
1. Tuple Indexing (Tuple):
Index Operator [ ] का use हम किसी item को tuple में access करने के लिए कर सकते हैं, जहां भी Index 0 से शुरू होता है।
इसलिए , 6 तत्वों वाले Tuple में 0 से 5 तक के indices होंगे। Tuple index range (6,7, … as an Example) के बाहर एक Index को access करने का try करने से Index Error raise करेगा।
Index एक integer होना चाहिए, इसलिए हम float या other types का use नहीं कर सकते। It will result in – TypeError
इसी तरह, nested tuples को nested indexing का use करके access किया जाता है, जैसा कि नीचे दिए गए example में दिखाया गया है :
# Accessing of tuple elements using tuple indexing
new_tuple = (‘p’,’e’,’r’,’m’,’i’,’t’)
print(new_tuple[0]) # ‘p’
print(new_tuple[5]) # ‘t’
# IndexError: list index out of the range
# print(new_tuple[6])
# Index must be an integer
# TypeError: list indices only must be integers, not float
# new_tuple[2.0]
# nested tuple
n_tuple = (“cursor”, [8, 4, 6], (1, 2, 3))
# nested index
print(n_tuple[0][3]) # ‘s’
print(n_tuple[1][1]) # 4
OUTPUT :
p
t
s
4
2. Negative Indexing (Tuple) :
Python अपने sequences के लिए negative indexing की अनुमति देता है।
-1 का index last item को refer करता है, -2 से second last item और so on।
Example :
# Negative indexing for accessing tuple elements
new_tuple = (‘p’, ‘e’, ‘r’, ‘m’, ‘i’, ‘t’)
# Output: ‘t’
print(new_tuple[-1])
# Output: ‘p’
print(new_tuple[-6])
OUTPUT :
t
p
3. Slicing (Tuple):
आप slicing operator colon का USE करके Tuple में items के range को access कर सकते है।
Example :
# Accessing tuple elements using tuple slicing
new_tuple = (‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’i’,’z’)
# elements second to fourth
# Output: (‘r’, ‘o’, ‘g’)
print(new_tuple[1:4])
# elements beginning to second
# Output: (‘p’, ‘r’)
print(new_tuple[:-7])
# elements eight to end
# Output: (‘i’, ‘z’)
print(new_tuple[7:])
# elements beginning to end
# Output: (‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘i’, ‘z’)
print(new_tuple[:])
OUTPUT :
(‘r’, ‘o’, ‘g’)
(‘p’, ‘r’)
(‘i’, ‘z’)
(‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘i’, ‘z’)
नीचे दिखाए गए chart केअनुसार elements के बीच Index पर विचार करके Slicing की सबसे अच्छी considering की जा सकती है। इसलिए यदि हम किसी range को access करना चाहते हैं, तो हमें उस Index की आवश्यकता है जो tuple से portion को slice कर देगा।
p | r | o | g | r | a | m | i | z |
0 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
-9 -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Changing a Tuple:
Lists के विपरीत, tuples immutable होते हैं।
इसका मतलब है कि एक बार assign किए जाने के बाद tuple के elements को change नहीं किया जा सकता है। लेकिन, यदि element खुद ही list की तरह एक mutable data type बन जाए, तो इसके nested items को बदला जा सकता है।
हम different values (reassignment) के लिए एक tuple भी assign कर सकते हैं।
Example :
# Changing tuple values
new_tuple = (4, 2, 3, [6, 5])
# TypeError: ‘tuple’ object does not support item assignment
# new_tuple[1] = 9
# However, item of mutable elements can be changed
new_tuple[3][0] = 9 # Output: (4, 2, 3, [9, 5])
print(new_tuple)
# Tuples can be reassigned
new_tuple = (‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘i’, ‘z’)
# Output: (‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘i’, ‘z’)
print(new_tuple)
OUTPUT :
(4, 2, 3, [9, 5])
(‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘i’, ‘z’)
- हम दो Tuples को जोड़ने के लिए + operator का use कर सकते हैं। इसे CONCATENATION कहते हैं।
- हम * operator का use करके दी गई संख्या के लिए elements को tuple में दोहरा सकते हैं।
- दोनों + और * operations result एक नया tuple होता है।
Example :
# Concatenation
# Output: (1, 2, 3, 4, 5, 6)
print((1, 2, 3) + (4, 5, 6))
# Repetition
# Output: (‘Repetition ‘, ‘Repetition ‘, ‘Repetition ‘)
print((“Repetition”,) * 3)
OUTPUT :
(1, 2, 3, 4, 5, 6)
(‘Repetition’, ‘Repetition’, ‘Repetition’)
Tuple Kya Hai – Deleting a Tuple:
जैसा कि ऊपर discuss किया गया है, हम elements को tuple में नहीं change कर सकते हैं। इसका मतलब है कि हम tuple से item को delete या remove नहीं कर सकते हैं।
हालाँकि, keyword del का use करके tuple को पूरी तरह से delete करना possible है।
Example :
# Deleting tuples
new_tuple = (‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘i’, ‘z’)
# can’t delete items
# TypeError: ‘tuple’ object doesn’t support item deletion
# del new_tuple[3]
# Can delete an entire tuple
del new_tuple
# NameError: name ‘new_tuple’ is not defined
print(new_tuple)
OUTPUT :
Traceback (most recent call last):
File “main.py”, line 20, in <module>
print(new_tuple)
NameError: name ‘new_tuple’ is not defined.
Tuple Kya Hai – Tuple Methods:
add items या remove items वाली Methods, tuple के लिए available नहीं हैं। केवल निम्नलिखित two methods available हैं।
1. Tuple Membership Test :
keyword का use करके हम test कर सकते हैं कि कोई item Tuple में मौजूद है या नहीं।
Example :
# Membership test in tuple
new_tuple = (‘a’, ‘p’, ‘p’, ‘l’, ‘e’,)
# In operation
print(‘a’ in new_tuple)
print(‘b’ in new_tuple)
# Not in operation
print(‘g’ not in new_tuple)
OUTPUT :
True
False
True
2. Iterating Through a Tuple :
हम tuple में प्रत्येक item के माध्यम से iteration करने के लिए for loop का USE कर सकते हैं।
Example :
# Using a for loop to iterate through a tuple
for name in (‘Animesh’, ‘techbagz’):
print(“Hello”, name)
OUTPUT :
Hello Animesh
Hello techbagz
Check करें की Inserted Value Tuple में है भी या नहीं :
is_tuple(tuplevalues) method का use यह determine करने के लिए किया जाता है कि inserted value tuple में है भी या नहीं। यह True return करता है जब inserted value tuple में होता है, otherwise यह false return करेगा है। उदाहरण के लिए,
-module(helloworld).
-export([start/0]).
start() ->
K = {xyz,70,pqr,70,{abc,85}} , io:fwrite(“~w”,[is_tuple(K)]).
OUTPUT :
True
- Google Authenticator Kya Hai. Authenticator For Phone And PC.
- Best Apps For Android. Apps Jo Sabke Phone Pe Hona चाहिए.
- IBM Mainframe Kya Hai.
- Boolean Data Type Kya Hai.
- Composite Data Type Kya Hai.
Tuple Kya Hai – List To Tuples Conversion:
यहाँ हम एक method का USE करेंगे , जो LIST से tuple में convert करेगा. Example :
#Convert list to tuple
listy = [15, 10, 70, 45, 75, 33]
print(listy)
#use the tuple() function created in Python, passing as parameter the list
tupley = tuple(listy)
print(tupley)
OUTPUT :
[15, 10, 70, 45, 75, 33]
(15, 10, 70, 45, 75, 33)
TUPLE TO LIST Conversion :
यहाँ हम एक method का USE करेंगे using list(sequence), जो tuple से LIST में convert करेगा. Example :
#initialize tuple
aTuple = (True, 25, ‘Techbagz’)
#tuple to list
aList = list(aTuple)
#print list
print(type(aList))
print(aList)
OUTPUT :
<class ‘list’>
[True, 25, ‘Techbagz’]
Converting Tuple of Integers to the List :
यहाँ पर हम tuple of INTEGERS value को list में convert करेंगे :
Example :
#initialize tuple
aTuple = (80, 20, 55, 75)
#tuple to list
aList = list(aTuple)
#print list
print(type(aList))
print(aList)
Output :
<class ‘list’>
[80, 20, 55, 75]
Check Tuple Size :
यहाँ हम एक method tuple_size(tuplename) का use करेंगे using list(sequence) , जो tuple के size को check करेगा. Example :
-module(helloworld).
-export([start/0]).
start() ->
K = {xyz,25,rst,80,{abc,75}} ,
io:fwrite(“~w”,[tuple_size(K)]).
Output :
5
Tuple Transformation : Reverse a Tuple in Python
Python में एक Tuple को reverse करने के लिए, call reversed() builtin function और tuple object को argument के रूप में pass करें। reversed() reversed object को return करता है। इस reversed object को tuple() function के रूप में pass करें। अब Reversed tuple ready है।
Examples
निचे दिए गए program में, tuple x लिया गए है, और उसको reverse करने reversed() and tuple() functions use किया गया है।
t = (10, 9, 8)
result = reversed(t)
result = tuple(result)
print(result)
Output : (8, 9, 10)
अब, यहाँ एक tuple of strings लिया गया है, आइये उस string tuple का reverse एक example से समझते है :
y = (‘quick’, ‘web’, ‘access’)
result = reversed(y)
result = tuple(result)
print(result)
Output :
(‘access’, ‘web’, ‘quick’)
Tuple Kya Hai – Tuple Transformation: Sort a Tuple in Python
Python में एक tuple को sort करने के लिए, sorted() built in function का use करें। Tuple को sorted() function में as argument pass करें। यह Function ascending order में returns करता है items of tuple sorted। हम इस list को tuple() का use करके tuple में convert कर सकते हैं।
हम sorting order को reverse parameter के माध्यम से sorted() function में भी specify कर सकते हैं। यहाँ default sorting order पहले से ही ascending order में है। reverse=True sorts the items in descending order.
इसके अलावा, हम एक key function specify कर सकते हैं, जिसके returned values के base पर, items की sorting की जाती है।
Examples :
Sort a Tuple of Integers
निचे दिए गए program में, यहाँ integer values के साथ एक tuple x लिया गया हैं, और इस tuple को ascending order में sort किया गया करते हैं।
aTuple = (21, 55, 87, 16, 94, 32, 78)
result = sorted(aTuple)
result = tuple(result)
print(‘Sorted Tuple :’, result)
OUTPUT :
Sorted Tuple : (16, 21, 32, 55, 78, 87, 94)
Sort a Tuple in Descending Order
निचे दिए गए program में, descending order में sort करने के लिए, reverse=True to sorted() function को pass किया गया है। EXAMPLE :
aTuple = (21, 55, 87, 16, 94, 32, 78)
result = sorted(aTuple, reverse=True)
result = tuple(result)
print(‘Sorted Tuple :’, result)
Output :
Sorted Tuple : (94, 87, 78, 55, 32, 21, 16)
Slice a Tuple based on Key Function
key एक ऐसा function है जो एक value लेता है और एक value को return करता है। Tuple में हर एक item के लिए, यह key function लागू किया जाता है, और returned value का use, items को sort करने के लिए comparison के लिए किया जाता है।
निचे दिए गए program में, strings की length के base पर tuple of strings को sort करते हैं। इसके के लिए, key के रूप में len() builtin function का use कर सकते हैं।
Example :
aTuple = (‘mno’, ‘pq’, ‘wxyz’)
result = sorted(aTuple, key=len)
result = tuple(result)
print(‘Sorted Tuple :’, result)
Output :
Sorted Tuple : (‘pq’, ‘mno’, ‘wxyz’)
Tuple Kya Hai – Slice a Tuple:
Python में एक tuple को slice करने के लिए, slice() builtin function का use करें। यहाँ पर tuple को slice के लिए stop position दिया गया है; या slice() function के लिए start और stop positions दोनों दिया गया है।
slice() function indices return करता है। resulting sliced tuple बनाने के लिए original tuple के साथ इन indices का use किया गया हैं।
Examples
Slice a Tuple with Stop Position
निचे दिए गए program में, integer values के साथ tuple x लिया गया है, और इस tuple को stop=4 तक slice किया गया है।
aTuple = (9, 3, 7, 4, 2, 5, 8, 1)
stop = 4
indices = slice(stop)
result = aTuple[indices]
print(‘Sliced function Tuple :’, result)
Output :
Sliced function Tuple : (9, 3, 7, 4)
Slice a Tuple with Start and Stop
specific starting position से specific stop position तक एक tuple को slice करने के लिए slice() function के लिए start positions से stop positions दोनों specify कर सकते हैं।
aTuple = (3, 7, 8, 10, 9, 6, 7, 5)
start = 2
stop = 5
indices = slice(start, stop)
result = aTuple[indices]
print(‘Sliced Tuple :’, result)
Output :
Sliced Tuple : (8, 10, 9)
Slice a Tuple with Steps
start और stop positions के साथ, slice() function के लिए step value भी specify कर सकते हैं। दी गई शुरुआत से Indices specified stop तक steps में लिया जाता है।
aTuple = (20, 5, 18, 11, 8, 4, 9, 14, 21, 13, 30)
start = 2
stop = 8
step = 3
indices = slice(start, stop, step)
result = aTuple[indices]
print(‘Sliced function Tuple :’, result)
Output :
Sliced function Tuple : (18, 4)
Filter Items of Tuple:
Python में एक Tuple की items को filter करने के लिए, filter() built in function को call करें और filtering function और tuple को arguments के रूप में pass करें। filtering function को एक argument लेना होता है और अगर item result में हो तो item को true return करना होता है या यदि item को result में नहीं है तो False return करना होता है।
filter() function एक filter object return करता है, जिसे tuple() का use करके इसे tuple में बदल सकते है।
Examples :
Filter Even Numbers from Tuple of Integers
निचे दिए गए program में, Integers वाला एक tuple x लिया गया है और filter function का use करके केवल even numbers को filter किया गया है। Example :
aTuple = (2, 5, 8, 12, 14, 3, 16)
result = filter(lambda x: x % 4 == 0, aTuple)
result = tuple(result)
print(result)
Output :
(8, 12, 16)
Filter Only Integers from a Tuple :
अब, different data types के items के साथ एक tuple लेते हैं। filter() function का use करके, केवल integers को filter किया गया है।
aTuple = (23, 55, ‘Abc’, 94, ‘Mno’, True)
result = filter(lambda y: type(y) == int, aTuple)
result = tuple(result)
print(result)
Output :
(23, 55, 94)
Tuples in DBMS (Database management System):
एक tuple, tablespace के एक table में निहित एक row है। एक table में आमतौर पर columns और rows होती हैं जिनमें rows record के लिए होती हैं जबकि columns attributes के लिए को represent करते हैं। table की single row जिसमें इस तरह के relation के लिए single record होता है, tuple के रूप में जाना जाता है।
इसलिए, एक Tuple, table में एक single entry होता है; इसे एक row या record भी कहा जाता है। वे आम तौर पर set of related data का representation करते हैं, Maths में यह केवल elements की एक ordered list है।
आइये dbms में tuples को एक Employee table के example से समझते है :

Advantage of Tuples:
Tuples के बहुत सारे advantages होते है जैसे कि :
- Tuples, naturewise fined size के होते हैं यानी tuple के elements को add/delete …. to/from नहीं कर सकते हैं।
- हम किसी भी element को tuple में search कर सकते हैं।
- Tuples lists के comparison में faster होते हैं, क्योंकि उनके पास values का एक constant set होता है।
- Tuples को dictionary keys के रूप में use किया जा सकता है, क्योंकि उसमें strings, numbers, आदि जैसे immutable values होते हैं।
निष्कर्ष:
Python हो या DBMS दोनों में ही Tuples बहुत important होते हैं। Python में tuples heterogeneous (different) data types के लिए use होता है यानि की comparison to List [homogeneous (similar) data types], Python में tuples का use ज्यादा होता है।
यहाँ Tuples in Python और Tuples in DBMS दोनों ही explain किया गया है। Iteration के लिए List के जगह आप Tuples का use करके अपने programs के performance को और भी ज्यादा boost कर सकते है।
उम्मीद करते है आपको ये Article, Tuple Kya Hai अच्छे लगे हो. आपको आर्टिकल पसंद आया हो तो इसको शेयर करे.