अजगर टपल (उदाहरणों के साथ)

इस लेख में, आप पायथन टुपल्स के बारे में सब कुछ जानेंगे। अधिक विशेष रूप से, टुपल्स क्या हैं, उन्हें कैसे बनाया जाए, उनका उपयोग कब करें और विभिन्न तरीकों से आपको परिचित होना चाहिए।

वीडियो: पायथन लिस्ट और टुपल्स

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

टुपल बनाना

()अल्पविराम द्वारा अलग किए गए कोष्ठकों के अंदर सभी वस्तुओं (तत्वों) को रखकर एक टपल बनाया जाता है । कोष्ठक वैकल्पिक हैं, हालांकि, उनका उपयोग करना एक अच्छा अभ्यास है।

टपल में किसी भी संख्या में आइटम हो सकते हैं और वे विभिन्न प्रकार (पूर्णांक, फ्लोट, सूची, स्ट्रिंग, आदि) के हो सकते हैं।

 # Different types of tuples # Empty tuple my_tuple = () print(my_tuple) # Tuple having integers my_tuple = (1, 2, 3) print(my_tuple) # tuple with mixed datatypes my_tuple = (1, "Hello", 3.4) print(my_tuple) # nested tuple my_tuple = ("mouse", (8, 4, 6), (1, 2, 3)) print(my_tuple)

आउटपुट

 () (1, 2, 3) (1, 'हैलो', 3.4) ('माउस', (8, 4, 6), (1, 2, 3))

कोष्ठक का उपयोग किए बिना एक टपल भी बनाया जा सकता है। इसे टपल पैकिंग के रूप में जाना जाता है।

 my_tuple = 3, 4.6, "dog" print(my_tuple) # tuple unpacking is also possible a, b, c = my_tuple print(a) # 3 print(b) # 4.6 print(c) # dog

आउटपुट

 (3, 4.6, 'कुत्ता') 3 4.6 कुत्ता

एक तत्व के साथ एक टपल बनाना थोड़ा मुश्किल है।

कोष्ठकों के भीतर एक तत्व होना पर्याप्त नहीं है। हमें यह इंगित करने के लिए एक अनुगामी अल्पविराम की आवश्यकता होगी कि यह वास्तव में एक टुपल है।

 my_tuple = ("hello") print(type(my_tuple)) # # Creating a tuple having one element my_tuple = ("hello",) print(type(my_tuple)) # # Parentheses is optional my_tuple = "hello", print(type(my_tuple)) # 

आउटपुट

 

टपल तत्वों तक पहुँचें

ऐसे कई तरीके हैं जिनसे हम एक ट्यूल के तत्वों तक पहुंच सकते हैं।

1. अनुक्रमण

हम ()किसी टुपल में किसी आइटम तक पहुंचने के लिए इंडेक्स ऑपरेटर का उपयोग कर सकते हैं , जहां इंडेक्स 0 से शुरू होता है।

तो, 6 तत्वों वाले एक ट्यूल में 0 से 5 तक के सूचकांक होंगे। ट्यूपल इंडेक्स रेंज (6,7, … इस उदाहरण में) के बाहर एक इंडेक्स तक पहुंचने की कोशिश कर रहा है IndexError

सूचकांक एक पूर्णांक होना चाहिए, इसलिए हम फ्लोट या अन्य प्रकार का उपयोग नहीं कर सकते हैं। इसका परिणाम यह होगा TypeError

इसी तरह, नेस्टेड ट्यूपल्स को नेस्टेड इंडेक्सिंग का उपयोग करके एक्सेस किया जाता है, जैसा कि नीचे दिए गए उदाहरण में दिखाया गया है।

 # Accessing tuple elements using indexing my_tuple = ('p','e','r','m','i','t') print(my_tuple(0)) # 'p' print(my_tuple(5)) # 't' # IndexError: list index out of range # print(my_tuple(6)) # Index must be an integer # TypeError: list indices must be integers, not float # my_tuple(2.0) # nested tuple n_tuple = ("mouse", (8, 4, 6), (1, 2, 3)) # nested index print(n_tuple(0)(3)) # 's' print(n_tuple(1)(1)) # 4

आउटपुट

 पीटी 4

2. नकारात्मक अनुक्रमण

पायथन अपने अनुक्रमों के लिए नकारात्मक अनुक्रमण की अनुमति देता है।

-1 का सूचकांक अंतिम आइटम को संदर्भित करता है, -2 को दूसरे अंतिम आइटम और इसी तरह।

 # Negative indexing for accessing tuple elements my_tuple = ('p', 'e', 'r', 'm', 'i', 't') # Output: 't' print(my_tuple(-1)) # Output: 'p' print(my_tuple(-6))

आउटपुट

 tp

3. कटाक्ष

हम टुकड़ा करने वाले ऑपरेटर बृहदान्त्र का उपयोग करके एक टपल में कई मदों तक पहुंच सकते हैं :

 # Accessing tuple elements using slicing my_tuple = ('p','r','o','g','r','a','m','i','z') # elements 2nd to 4th # Output: ('r', 'o', 'g') print(my_tuple(1:4)) # elements beginning to 2nd # Output: ('p', 'r') print(my_tuple(:-7)) # elements 8th to end # Output: ('i', 'z') print(my_tuple(7:)) # elements beginning to end # Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') print(my_tuple(:))

आउटपुट

 ('आर', 'ओ', 'जी') ('पी', 'आर') ('आई', 'जेड') ('पी', 'आर', 'ओ', 'जी', 'आर' ',' ए ',' एम ',' आई ',' जेड ')

नीचे दिखाए गए तत्वों के बीच अनुक्रमणिका पर विचार करके स्लाइसिंग को सर्वश्रेष्ठ रूप से देखा जा सकता है। इसलिए यदि हम एक सीमा तक पहुंचना चाहते हैं, तो हमें सूचकांक की आवश्यकता है जो कि टपल से भाग को काट देगा।

पाइथन में तत्व स्लाइसिंग

टुपल बदलना

सूचियों के विपरीत, टुपल्स अपरिवर्तनीय हैं।

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

हम अलग-अलग मूल्यों (पुनर्मूल्यांकन) के लिए एक टपल भी दे सकते हैं।

 # Changing tuple values my_tuple = (4, 2, 3, (6, 5)) # TypeError: 'tuple' object does not support item assignment # my_tuple(1) = 9 # However, item of mutable element can be changed my_tuple(3)(0) = 9 # Output: (4, 2, 3, (9, 5)) print(my_tuple) # Tuples can be reassigned my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') # Output: ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') print(my_tuple)

आउटपुट

 (4, 2, 3, (9, 5)) ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

We can use + operator to combine two tuples. This is called concatenation.

We can also repeat the elements in a tuple for a given number of times using the * operator.

Both + and * operations result in a new tuple.

 # Concatenation # Output: (1, 2, 3, 4, 5, 6) print((1, 2, 3) + (4, 5, 6)) # Repeat # Output: ('Repeat', 'Repeat', 'Repeat') print(("Repeat",) * 3)

Output

 (1, 2, 3, 4, 5, 6) ('Repeat', 'Repeat', 'Repeat')

Deleting a Tuple

As discussed above, we cannot change the elements in a tuple. It means that we cannot delete or remove items from a tuple.

Deleting a tuple entirely, however, is possible using the keyword del.

 # Deleting tuples my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z') # can't delete items # TypeError: 'tuple' object doesn't support item deletion # del my_tuple(3) # Can delete an entire tuple del my_tuple # NameError: name 'my_tuple' is not defined print(my_tuple)

Output

 Traceback (most recent call last): File "", line 12, in NameError: name 'my_tuple' is not defined

Tuple Methods

Methods that add items or remove items are not available with tuple. Only the following two methods are available.

Some examples of Python tuple methods:

 my_tuple = ('a', 'p', 'p', 'l', 'e',) print(my_tuple.count('p')) # Output: 2 print(my_tuple.index('l')) # Output: 3

Output

 2 3

Other Tuple Operations

1. Tuple Membership Test

We can test if an item exists in a tuple or not, using the keyword in.

 # Membership test in tuple my_tuple = ('a', 'p', 'p', 'l', 'e',) # In operation print('a' in my_tuple) print('b' in my_tuple) # Not in operation print('g' not in my_tuple)

Output

 True False True

2. Iterating Through a Tuple

We can use a for loop to iterate through each item in a tuple.

 # Using a for loop to iterate through a tuple for name in ('John', 'Kate'): print("Hello", name)

Output

 Hello John Hello Kate

Advantages of Tuple over List

Since tuples are quite similar to lists, both of them are used in similar situations. However, there are certain advantages of implementing a tuple over a list. Below listed are some of the main advantages:

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

दिलचस्प लेख...