What is a Tuple in Python

What is a Tuple in Python

In Python, a Tuple is a collection of Python objects separated by commas. Tuple is exactly same as list except that is immutable i.e., once we create tuple object, we cannot perform any changes in that object. Hence tuple is read only version of list. If our data is fixed and never changes then we should go for tuple.

पाइथन में, टपल अल्पविराम द्वारा अलग किए गए पाइथन ऑब्जेक्ट्स का एक संग्रह है। टपल बिल्कुल वैसी ही है जैसे कि लिस्ट अपरिवर्तनीय है यानी, एक बार जब हम टपल ऑब्जेक्ट बनाते हैं, तो हम उस ऑब्जेक्ट में कोई बदलाव नहीं कर सकते हैं। इसलिए टपल को लिस्ट में रीड ओनली वर्जन कहा जाता है। यदि हमारा डेटा निश्चित है और कभी नहीं बदलता है तो हमें टपल के लिए जाना चाहिए।

The main properties of Python tuple:
पायथन टपल के मुख्य गुणः

  • Insertion order is preserved

इंसर्शन आर्डर संरक्षित है

  • Duplicates are allowed

डुप्लिकेट अलाउड है

  • Heterogeneous objects are allowed

विशेष ऑब्जेक्ट्स अलाउड है

  • We can preserve insertion order and we can differentiate duplicate objects by using index. Hence index will play very important role in tuple also.

हम प्रविष्टि ऑर्डर को संरक्षित कर सकते हैं और हम इंडेक्स का उपयोग करके डुप्लिकेट ऑब्जेक्ट को अलग कर सकते हैं। इसलिए टपल में भी इंडेक्स बहुत महत्वपूर्ण भूमिका निभाएगा।

  • Tuple support both +Ve and -Ve index.

टपल +Ve और -Ve इंडेक्स दोनों का समर्थन करते हैं।

  • We can represent tuple elements within parenthesis and with comma separator. Parentheses are optional but recommended to use.

हम कोष्ठक के भीतर और अल्पविराम विभाजक के साथ टपल एलीमेंट का प्रतिनिधित्व कर सकते हैं। कोष्ठक

वैकल्पिक हैं लेकिन उपयोग करने के लिए अनुशंसित हैं।

Example: To Create A Tuple with Different Inputs

विभिन्न इनपुट्स के साथ एक ट्यूपल बनाने के लिए

# create an empty tuple
tpl = ()
print (“A blank tuple : “, tpl)
# create a tuple without using round brackets
tpl = 100, 200, 300
print (“A tuple set without parenthesis : “, tpl)
print(“type : “, type (tpl))
# create a tuple of numbers using parenthesis
tpl = (111, 222,333)
print (“A tuple of numbers : “, tpl)
# create a tuple of integer, float, imaginary and string
tpl = (1, 2.2, 3+3j,’four’)
print(“A tuple of mixed elements : “, tpl)
# create a tuple of mixed data types such as numbers, strings, lists
tpl = (33, “33”, [3, 3])
print(“A tuple of mixed data types : “, tpl)
# create a nested tuple
tpl = ((‘xxx’, ‘yyyy’, ‘zzz’), (‘aaa’, ‘bbb’, ‘ccc’))
print (“A tuple of tuples:”, tpl)

Output:
A blank tuple :  ()
A tuple set without parenthesis :  (100, 200, 300)
type :  <class ‘tuple’>
A tuple of numbers :  (111, 222, 333)
A tuple of mixed elements :  (1, 2.2, (3+3j), ‘four’)
A tuple of mixed data types :  (33, ’33’, [3, 3])
A tuple of tuples: ((‘xxx’, ‘yyyy’, ‘zzz’), (‘aaa’, ‘bbb’, ‘ccc’))

Note: We have to take special care about single valued tuple. Compulsory the value should ends with comma (,), otherwise it is not treated as tuple. Let us take an example to understand.

नोट: हमें सिंगल वैल्यू टपल के बारे में विशेष ध्यान रखना है। अनिवार्य वैल्यू कॉमा () के साथ समाप्त होना चाहिए, अन्यथा इसे टपल नहीं माना जाता है। हम इसे समझने के लिए एक उदाहरण लेते हैं।

Example:

# create a single valued tuple without comma
tpl = (10)
print (tpl)
print (“type of tpl is : “, type (tpl))
# create an single valued tuple with comma
tpl = (10,)
print (tpl)
print (“type of tpl is : “, type (tpl))

Output:
10
type of tpl is :  <class ‘int’>
(10,)
type of tpl is :  <class ‘tuple’>

Example: which of the following are valid tuples?
निम्नलिखित में से कौन सा वैल्यू टपल्स हैं?

tpl = ()
print (tpl, ” “, type (tpl))
tpl = 10
print (tpl, ” “, type (tpl))
tpl = 1,2,3,4
print (tpl, ” “, type (tpl))
tpl = (1,2,3,4)
print (tpl, ” “, type (tpl))
tpl = 100,
print (tpl, ” “, type (tpl))
tpl = (100)
print (tpl, ” “, type (tpl))
tpl = (2,)
print (tpl, ” “, type (tpl))

Output:
()   <class ‘tuple’>
10   <class ‘int’>
(1, 2, 3, 4)   <class ‘tuple’>
(1, 2, 3, 4)   <class ‘tuple’>
(100,)   <class ‘tuple’>
100   <class ‘int’>
(2,)   <class ‘tuple’>

By using of tuple () function
tuple () फंक्शन का उपयोग करके

#we can convert list object to tuple object while using tuple ()function
list = [1,2,3,4]
print (list)
print (type (list))
print (“converting list to tuple”)
tpl = tuple (list)
print (“after convertion”)
print (tpl)
print (type (tpl))
# create a tuple using range operator
tpl = tuple (range (100, 120, 2))
print (tpl)

Output:
[1, 2, 3, 4]
<class ‘list’>
converting list to tuple
after convertion
(1, 2, 3, 4)
<class ‘tuple’>
(100, 102, 104, 106, 108, 110, 112, 114, 116, 118)

Accessing elements of tuples
Example:

tpl = (10,20,30,40)
print(“tpl : “, tpl)
print(“tpl[0] : “, tpl[0])
print (“tpl[1] : “, tpl[1])
print (“tpl(-1) : “, tpl[-1])
print (“tpl[(-len (tpl))] : “, tpl[(-len (tpl))])
print(“Using for loop printing tuple elements”)
for i in tpl:
    print(i)

Output:
tpl :  (10, 20, 30, 40)
tpl[0] :  10
tpl[1] :  20
tpl(-1) :  40
tpl[(-len (tpl))] :  10
Using for loop printing tuple elements
10
20
30
40

Tuple is immutable i.e., once we create tuple object, we cannot perform any changes in that object.

टपल अपरिवर्तनीय है, अर्थात्, एक बार जब हम टपल ऑब्जेक्ट बनाते हैं, तो हम उसे ऑब्जेक्ट में कोई परिवर्तन नहीं कर ।

Example:

tpl = (10,20,30,40)
tpl[1] = 100
print (tpl)

Output:
Traceback (most recent call last):
  File “C:\Users\HP\Desktop\1.py”, line 2, in <module>
    tpl[1] = 100
TypeError: ‘tuple’ object does not support item assignment

Mathematical operators for tuple

टपल के लिए मैथमेटिकल ऑपरेटर्स

We can appli ‘+’ and ‘*’ operators for tuple – हम टपल के लिए ‘+’ और ‘*’ appli का उपयोग कर सकते हैं।

Example:

tpl = (10,20,30,40)
print (tpl+tpl)
print (tpl*3)

Output:
(10, 20, 30, 40, 10, 20, 30, 40)
(10, 20, 30, 40, 10, 20, 30, 40, 10, 20, 30, 40)

Advantage of tuples – टपल्स के लाभ

  • Tuples are faster than lists.

टपल लिस्ट्स की तुलना में तेज है।

  • If you know that some data doesn’t have to be changed, you should use tuples instead of lists, because this protects your data against accidental changes.

यदि आप जानते हैं कि कुछ डेटा को बदलना नहीं है, तो आपको लिस्ट्स के बजाय टपल्स का उपयोग करना चाहिए, क्योंकि यह आपके डेटा को आकस्मिक परिवर्तनों से बचाता है।

  • The main advantage of tuples consists in the fact that tuples can be used as keys in dictionaries, while lists can’t.

टपल्स का मुख्य लाभ इस तथ्य में है कि टपल्स को डिक्शनरी में की के रूप में इस्तेमाल किया जा सकता है,

जबकि लिस्ट्स नहीं कर सकती हैं।

Tuple packing and unpacking
टपल पैकिंग और अनपैकिंग

In Python there is a very powerful tuple assignment feature that assigns right hand side of values into left hand side. In other way it is called unpacking of a tuple of values into a variable. In packing, we put values into a new tuple while in unpacking we extract those values into a single variable.

पायथन में एक बहुत ही शक्तिशाली टपल असाइनमेंट फीचर है जो राइट हैंड साइड के वैल्यू को लेफ्ट हैंड साइड में असाइन करता है। दूसरे शब्दो मे, इसे टपल के वैयुज को किसी अन्य वैरिएबल मे अनपैकिंग करना कहा जाता है। पैकिंग में, हम वैल्यूज को एक नए टपल में रखते हैं जबकि अनपैकिंग में हम उन वैल्यूज को एक सिंगल वैरिएबल मे निकालते हैं।

We can create a tuple by packing a group of variables.

वैरियेबल के समूह को पैक करके हम एक टपल बना सकते हैं।

Example:

a = 10
b = 2.2
c = ‘hello’
d = 1+2j
e = True
tpl = (a, b, c,d,e)
print (tpl)

Output:
(10, 2.2, ‘hel10’, (1+2j),True)

Here a, b, c, d and e are packed into a tuple ‘tpl’. This is nothing but tuple packing.

यहाँ a, b, c, d और e को टपल ‘tpl’ में पैक किया गया है। यह और कुछ नहीं बल्कि टपल पैकिंग है।

Tuple unpacking is the reverse process of tuple packing. We can unpack a tuple and assign its values to different variables.

टपल अनपैकिंग टपल पैकिंग की रिवर्स प्रोसेस है। हम एक टपल को अनपैक कर सकते हैं और विभिन्न वैरिएबल्स को इसके वैल्यू निर्दिष्ट कर सकते हैं।

Example:

tpl = 10, 2.2, ‘hello’, 1+2j, True
a, b, c, d, e = tpl
print ( ‘a = ‘, a, ‘ b = ‘, b,  ‘c = ‘, c, ‘ d = ‘, d, ‘ e = ‘,e)

Output:
a =  10  b =  2.2 c =  hello  d =  (1+2j)  e =  True

Note: At the time of tuple unpacking the no. of variables and no. of values should be same. Otherwise we will get ValueError message.

नोट: टपल अनपैकिंग के समय में वैरियेबल की संख्या और वैल्यू की संख्या समान होने चाहिए। अन्यथा हमें वैल्यूएरर संदेश मिलेगा।

Example:
tpl = 10, 20, 30
a, b = tpl
print (‘a = ‘, a, ‘ b = ‘, b)

Output:
Traceback (most recent call last):
  File “C:\Users\HP\Desktop\1.py”, line 2, in <module>
    a, b = tpl
ValueError: too many values to unpack (expected 2)

Difference between list and tuple
लिस्ट और टपल के बीच अंतर

List लिस्टTuple टपल
List is a group of comma separated values within square brackets and square brackets are mandatory. List = [1,2,3,4] लिस्ट स्क्वैयर ब्रैकेट के भीतर अल्पविराम से अलग किए गए वैल्यू का एक समूह है और स्क्वैयर ब्रैकेट अनिवार्य हैं। लिस्ट = [1,2,3,4]Tuple is group of comma separated values within parenthesis and parentheses are optional. Tuple = 1,2,3,4 (or) tuple = (1,2,3,4) टपल कॉमा और ब्रैकेट के भीतर अल्पविराम द्वारा अलग किए गए वैल्यू का समूह वैकल्पिक है। टपल = 1,2,3,4 (या) टपल = (1,2,3,4)
List objects are mutable लिस्ट ऑब्जेक्ट्स परस्पर हैंTuple objects are immutable. टपल ऑब्जेक्ट्स अपरिवर्तनीय हैं।
If the content is not fixed and keep on changing, then then we go for list. यदि कंटेंट निश्चित नहीं है और बदलती रहती है तो हम लिस्ट के लिए जाते हैं।If the content is fixed and never changes, we should go for Tuple. यदि कंटेंट तय हो गई है और कभी नहीं बदलती है तो हमें टपल के लिए जाना चाहिए।
List objects cannot used as keys for dictionaries because keys should be immutable. लिस्ट ऑब्जेक्ट्स के डिक्शनरी के लिए की के रूप में उपयोग नहीं किया जा सकता है क्योंकि की अपरिवर्तनीय होनी चाहिए।Tuple objects can be used as keys for dictionaries because keys should be immutable. टपल ऑब्जेक्ट्स को डिक्शनरी की कुंजी के रूप में उपयोग किया जा सकता है क्योंकि की अपरिवर्तनीय होनी चाहिए।

Tuple comprehension – टपल कांप्रिहेंसन

A comprehension works by looping or iterating over items and assigning them into a container. Since, a tuple is unable to receive assignments. Once a Tuple is created, it can not be appended, assigned to or extended. The only way to modify a Tuple is if one of its objects can itself be assigned to (is a non-tuple container).

कांप्रिहेंसन किसी ऑब्जेक्ट या आईटम्स पर लूपिंग या पुनरावृत्ति करके और उन्हें कंटेनर में असाइन करके काम करता है। चूंकि, टपल असाइनमेंट प्राप्त करने में असमर्थ होता है। एक बार टपल बनने के बाद, इसे जोड़ा, सौंपा या बढ़ाया नहीं जा सकता है। टपल को संशोधित करने का एकमात्र तरीका यह है कि इसके किसी ऑब्जेक्ट को खुद को सौंपा जा सकता है (यह एक नॉन-टपल कंटेनर है)।

In Python, Tuple comprehension is not supported because in the latter case you are creating and executing functions and functions are expensive in Python. A tuple comprehension would not be useless. Tuple (thing for thing in .things) has latency issues and tuple ([thing for thing in things]) could have memory issues.

पायथन में, टपल कंप्रीहेन्शन को सपोर्ट नहीं किया जाता है क्योंकि कुछ मामले जिसमे आप फंक्शन को क्रिएट और एक्जिक्यूट करते है जोकि पायथन में एक्सपेंसिव होते हैं। टपल कंप्रीहेनशन यूजलेस नहीं होता है। टपल (thing for thing in .things) में विलंबता के इशू होते हैं और टपल ([thing for thing in things]) में मेमोरी के इशू होते हैं।

Example: Here we are not getting tuple object and we are getting generator object.
यहां हमें टपल ऑब्जेक्ट नहीं मिल रहा है और हमें जेनरेटर ऑब्जेक्ट मिल रहा है।

tpl = (x*2 for x in range (1,5))
for x in tpl:
print (x)
print (type (tpl))

Output:
2
4
6
8
<class ‘generator’>

Chapter wise Model Paper Link

About Me